package org.osbl.client.action;
import javax.swing.*;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.HashMap;
public abstract class DelegatingActionProvider
implements ActionProvider
{
protected ActionProvider parent;
Map<String,Action> cache = new HashMap<String, Action>();
protected DelegatingActionProvider(ActionProvider parent) {
this.parent = parent;
}
public Action getAction(String command) {
Action action = cache.get(command);
if (action == null) {
action = parent.getAction(command);
if (action == null)
throw new IllegalArgumentException("No action for command " + command);
action = clone(action);
cache.put(command, action);
configure(action);
}
return action;
}
public static Action clone(Action action) {
try {
Method clone = action.getClass().getMethod("clone");
return (Action)clone.invoke(action);
}
catch (Exception e) {
//e.printStackTrace();
throw new RuntimeException("Action is not clonable");
}
/*
if (action instanceof NavigationAction) {
NavigationAction cloneable = (NavigationAction)action;
return (Action)cloneable.clone();
}
else
throw new RuntimeException("action is not an instance of " + NavigationAction.class.getName());
*/
}
}
|