📄 actionmethodmapper.java
字号:
/*******************************************************************************
* Copyright (c) 2004 Stefan Zeiger and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.novocode.com/legal/epl-v10.html
*
* Contributors:
* Stefan Zeiger (szeiger@novocode.com) - initial API and implementation
*******************************************************************************/
package com.novocode.naf.gui.event;
import java.lang.reflect.Method;
import com.novocode.naf.app.NAFException;
import com.novocode.naf.model.ModelMap;
/**
* Utility class which created ActionListeners that invoke methods of an
* object which are marked with an ActionMethod annotation.
*
* @author Stefan Zeiger (szeiger@novocode.com)
* @since Dec 19, 2004
* @version $Id: ActionMethodMapper.java,v 1.2 2005/01/14 21:14:48 szeiger Exp $
*/
public class ActionMethodMapper
{
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
private ActionMethodMapper() {}
public static void createModels(Object target, ModelMap models) throws NAFException
{
createModels(target, target.getClass(), models);
}
public static void createModels(Class clazz, ModelMap models) throws NAFException
{
createModels(null, clazz, models);
}
private static void createModels(final Object target, Class clazz, ModelMap models) throws NAFException
{
for(final Method method : clazz.getMethods())
{
ActionMethod am = method.getAnnotation(ActionMethod.class);
if(am == null) continue;
IActionListener al = actionListenerFor(target, clazz, method);
String modelName = am.value();
if(modelName == null || modelName.length() == 0) modelName = method.getName();
Object old = models.get(modelName);
if(old == null) models.put(modelName, al);
else if(old instanceof IActionBroadcaster) ((IActionBroadcaster)old).addActionListener(al);
else if(old instanceof IActionListener)
{
DefaultActionBroadcaster bc = new DefaultActionBroadcaster();
bc.addActionListener((IActionListener)old);
bc.addActionListener(al);
models.put(modelName, bc);
}
else throw new NAFException("Model "+modelName+" already has a non-IActionListener object attached");
}
}
public static final IActionListener actionListenerFor(final Object target, Class clazz, final Method method) throws NAFException
{
final String qname = clazz.getName()+"."+method.getName();
Class[] paramTypes = method.getParameterTypes();
if(paramTypes.length == 0)
{
return new IActionListener()
{
public void performAction(ActionEvent e)
{
try { method.invoke(target, EMPTY_OBJECT_ARRAY); }
catch(Exception ex) { throw new NAFException("Error invoking method "+qname, ex); }
}
};
}
else if(paramTypes.length == 1 && paramTypes[0] == ActionEvent.class)
{
return new IActionListener()
{
public void performAction(ActionEvent e)
{
try { method.invoke(target, new Object[] { e }); }
catch(Exception ex) { throw new NAFException("Error invoking method "+qname, ex); }
}
};
}
else throw new NAFException("ActionMethod "+qname+" must have a signature () or (ActionEvent)");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -