01: package org.osbl.client.action;
02:
03: import javax.swing.*;
04: import java.lang.reflect.Method;
05: import java.util.Map;
06: import java.util.HashMap;
07:
08: public abstract class DelegatingActionProvider implements
09: ActionProvider {
10: protected ActionProvider parent;
11: Map<String, Action> cache = new HashMap<String, Action>();
12:
13: protected DelegatingActionProvider(ActionProvider parent) {
14: this .parent = parent;
15: }
16:
17: public Action getAction(String command) {
18: Action action = cache.get(command);
19: if (action == null) {
20: action = parent.getAction(command);
21: if (action == null)
22: throw new IllegalArgumentException(
23: "No action for command " + command);
24: action = clone(action);
25: cache.put(command, action);
26: configure(action);
27: }
28: return action;
29: }
30:
31: public static Action clone(Action action) {
32: try {
33: Method clone = action.getClass().getMethod("clone");
34: return (Action) clone.invoke(action);
35: } catch (Exception e) {
36: //e.printStackTrace();
37: throw new RuntimeException("Action is not clonable");
38: }
39:
40: /*
41: if (action instanceof NavigationAction) {
42: NavigationAction cloneable = (NavigationAction)action;
43: return (Action)cloneable.clone();
44: }
45: else
46: throw new RuntimeException("action is not an instance of " + NavigationAction.class.getName());
47: */
48: }
49: }
|