Refactored handleAction() to allow the method lookup magic to be used with
non-Swing toolkits. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1775 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -198,20 +198,33 @@ public abstract class Controller
|
|||||||
* propagated up to the next controller in scope.
|
* propagated up to the next controller in scope.
|
||||||
*/
|
*/
|
||||||
public boolean handleAction (ActionEvent action)
|
public boolean handleAction (ActionEvent action)
|
||||||
|
{
|
||||||
|
Object arg = null;
|
||||||
|
if (action instanceof CommandEvent) {
|
||||||
|
arg = ((CommandEvent)action).getArgument();
|
||||||
|
}
|
||||||
|
return handleAction(action.getSource(), action.getActionCommand(), arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A version of {@link #handleAction(ActionEvent)} with the parameters
|
||||||
|
* broken out so that it can be used by non-Swing interface toolkits.
|
||||||
|
*/
|
||||||
|
public boolean handleAction (Object source, String action, Object arg)
|
||||||
{
|
{
|
||||||
Method method = null;
|
Method method = null;
|
||||||
Object[] args = null;
|
Object[] args = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// look for the appropriate method
|
// look for the appropriate method
|
||||||
String targetName = "handle" + action.getActionCommand();
|
String targetName = "handle" + action;
|
||||||
Method[] methods = getClass().getMethods();
|
Method[] methods = getClass().getMethods();
|
||||||
int mcount = methods.length;
|
int mcount = methods.length;
|
||||||
|
|
||||||
for (int i = 0; i < mcount; i++) {
|
for (int i = 0; i < mcount; i++) {
|
||||||
if (methods[i].getName().equals(targetName)) {
|
if (methods[i].getName().equals(targetName)) {
|
||||||
// see if we can generate the appropriate arguments
|
// see if we can generate the appropriate arguments
|
||||||
args = generateArguments(methods[i], action);
|
args = generateArguments(methods[i], source, arg);
|
||||||
// if we were able to, go ahead and use this method
|
// if we were able to, go ahead and use this method
|
||||||
if (args != null) {
|
if (args != null) {
|
||||||
method = methods[i];
|
method = methods[i];
|
||||||
@@ -222,7 +235,7 @@ public abstract class Controller
|
|||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.warning("Error searching for action handler method " +
|
Log.warning("Error searching for action handler method " +
|
||||||
"[controller=" + this + ", event=" + action + "].");
|
"[controller=" + this + ", action=" + action + "].");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +250,7 @@ public abstract class Controller
|
|||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.warning("Error invoking action handler [controller=" + this +
|
Log.warning("Error invoking action handler [controller=" + this +
|
||||||
", event=" + action + "].");
|
", action=" + action + "].");
|
||||||
Log.logStackTrace(e);
|
Log.logStackTrace(e);
|
||||||
// even though we choked, we still "handled" the action
|
// even though we choked, we still "handled" the action
|
||||||
return true;
|
return true;
|
||||||
@@ -278,7 +291,8 @@ public abstract class Controller
|
|||||||
* Used by {@link #handleAction} to generate arguments to the action
|
* Used by {@link #handleAction} to generate arguments to the action
|
||||||
* handler method.
|
* handler method.
|
||||||
*/
|
*/
|
||||||
protected Object[] generateArguments (Method method, ActionEvent action)
|
protected Object[] generateArguments (
|
||||||
|
Method method, Object source, Object argument)
|
||||||
{
|
{
|
||||||
// figure out what sort of arguments are required by the method
|
// figure out what sort of arguments are required by the method
|
||||||
Class[] atypes = method.getParameterTypes();
|
Class[] atypes = method.getParameterTypes();
|
||||||
@@ -287,19 +301,16 @@ public abstract class Controller
|
|||||||
return new Object[0];
|
return new Object[0];
|
||||||
|
|
||||||
} else if (atypes.length == 1) {
|
} else if (atypes.length == 1) {
|
||||||
return new Object[] { action.getSource() };
|
return new Object[] { source };
|
||||||
|
|
||||||
} else if (atypes.length == 2) {
|
} else if (atypes.length == 2) {
|
||||||
if (action instanceof CommandEvent) {
|
if (argument != null) {
|
||||||
CommandEvent command = (CommandEvent)action;
|
return new Object[] { source, argument };
|
||||||
return new Object[] { action.getSource(),
|
|
||||||
command.getArgument() };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.warning("Unable to map non-command event to " +
|
Log.warning("Unable to map argumentless event to handler method " +
|
||||||
"handler method that requires extra " +
|
"that requires an argument [controller=" + this +
|
||||||
"argument [controller=" + this +
|
", method=" + method + ", source=" + source + "].");
|
||||||
", action=" + action + "].");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we would have handled it, but we couldn't
|
// we would have handled it, but we couldn't
|
||||||
|
|||||||
Reference in New Issue
Block a user