1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.services.gui.swing.cookswing.action;
18
19 import javax.swing.AbstractButton;
20 import javax.swing.Action;
21
22 import org.jdesktop.application.Application;
23 import org.jdesktop.application.ApplicationContext;
24
25 import ch.elca.el4j.services.gui.swing.ActionsContext;
26
27 import cookxml.cookswing.CookSwing;
28 import cookxml.core.DecodeEngine;
29 import cookxml.core.exception.SetterException;
30 import cookxml.core.interfaces.Setter;
31
32
33
34
35
36
37
38
39
40 public class ButtonActionSetter implements Setter {
41
42
43
44 private Object m_actionHolder;
45
46
47
48
49
50 public ButtonActionSetter() { }
51
52
53
54
55 public ButtonActionSetter(Object actionHolder) {
56 m_actionHolder = actionHolder;
57 }
58
59
60 public void setAttribute(String ns, String tag,
61 String attrNS, String attr, Object obj, Object value,
62 DecodeEngine decodeEngine) throws SetterException {
63
64 Object actionHolder = m_actionHolder;
65 if (actionHolder == null) {
66 actionHolder = decodeEngine.getVariable("this");
67 }
68
69 Action action = null;
70
71 String actionName = (String) value;
72
73 String[] parsedValues = actionName.split("#");
74 Class<?> cls = null;
75 if (parsedValues.length == 2) {
76 try {
77 cls = Class.forName(parsedValues[0]);
78 ApplicationContext ac = Application.getInstance().getContext();
79 action = ac.getActionMap(cls, actionHolder).get(parsedValues[1]);
80 } catch (ClassNotFoundException e) {
81 action = null;
82 }
83 }
84
85 if (action == null) {
86 ActionsContext actionsContext;
87 if (actionHolder instanceof ActionsContextAware) {
88 actionsContext = ((ActionsContextAware) actionHolder).getActionsContext();
89 } else {
90
91 actionsContext = ActionsContext.extendDefault(actionHolder);
92 }
93
94 action = actionsContext.getAction(actionName);
95 if (action == null) {
96 throw new ActionNotFoundException(actionName);
97 }
98 }
99
100 AbstractButton button = (AbstractButton) obj;
101 button.setAction(action);
102 }
103 }