1:
45:
46: package ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54:
55: import ;
56:
57:
63: public class ActionRadioButton extends JRadioButton
64: {
65:
66: private Action action;
67:
68:
69: private ActionEnablePropertyChangeHandler propertyChangeHandler;
70:
71:
75: private class ActionEnablePropertyChangeHandler implements PropertyChangeListener
76: {
77:
82: public void propertyChange(final PropertyChangeEvent event)
83: {
84: try
85: {
86: if (event.getPropertyName().equals("enabled"))
87: {
88: setEnabled(getAction().isEnabled());
89: }
90: else if (event.getPropertyName().equals(Action.SMALL_ICON))
91: {
92: setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
93: }
94: else if (event.getPropertyName().equals(Action.NAME))
95: {
96: setText((String) getAction().getValue
97: (Action.NAME));
98: }
99: else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION))
100: {
101: ActionRadioButton.this.setToolTipText((String)
102: getAction().getValue(Action.SHORT_DESCRIPTION));
103: }
104:
105: final Action ac = getAction();
106: if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY))
107: {
108: final KeyStroke oldVal = (KeyStroke) event.getOldValue();
109: if (oldVal != null)
110: {
111: unregisterKeyboardAction
112: (oldVal);
113: }
114: final Object o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY);
115: if (o instanceof KeyStroke && o != null)
116: {
117: final KeyStroke k = (KeyStroke) o;
118: registerKeyboardAction(ac, k, WHEN_IN_FOCUSED_WINDOW);
119: }
120: }
121: else if (event.getPropertyName().equals(ActionDowngrade.MNEMONIC_KEY))
122: {
123: final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY);
124: if (o != null)
125: {
126: if (o instanceof Character)
127: {
128: final Character c = (Character) o;
129: setMnemonic(c.charValue());
130: }
131: else if (o instanceof Integer)
132: {
133: final Integer c = (Integer) o;
134: setMnemonic(c.intValue());
135: }
136: }
137: }
138: }
139: catch (Exception e)
140: {
141: Log.warn("Error on PropertyChange in ActionButton: ", e);
142: }
143: }
144: }
145:
146:
149: public ActionRadioButton()
150: {
151: super();
152: }
153:
154:
159: public ActionRadioButton(final String text)
160: {
161: super(text);
162: }
163:
164:
170: public ActionRadioButton(final String text, final Icon icon)
171: {
172: super(text, icon);
173: }
174:
175:
176:
181: public ActionRadioButton(final Icon icon)
182: {
183: super(icon);
184: }
185:
186:
191: public ActionRadioButton(final Action action)
192: {
193: setAction(action);
194: }
195:
196:
201: public Action getAction()
202: {
203: return this.action;
204: }
205:
206:
207:
213: private ActionEnablePropertyChangeHandler getPropertyChangeHandler()
214: {
215: if (this.propertyChangeHandler == null)
216: {
217: this.propertyChangeHandler = new ActionEnablePropertyChangeHandler();
218: }
219: return this.propertyChangeHandler;
220: }
221:
222:
228: public void setEnabled(final boolean b)
229: {
230: super.setEnabled(b);
231: if (getAction() != null)
232: {
233: getAction().setEnabled(b);
234: }
235: }
236:
237:
250: public void setAction(final Action newAction)
251: {
252: final Action oldAction = getAction();
253: if (oldAction != null)
254: {
255: removeActionListener(oldAction);
256: oldAction.removePropertyChangeListener(getPropertyChangeHandler());
257:
258: final Object o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
259: if (o instanceof KeyStroke && o != null)
260: {
261: final KeyStroke k = (KeyStroke) o;
262: unregisterKeyboardAction(k);
263: }
264: }
265: this.action = newAction;
266: if (this.action != null)
267: {
268: addActionListener(newAction);
269: newAction.addPropertyChangeListener(getPropertyChangeHandler());
270:
271: setText((String) (newAction.getValue(Action.NAME)));
272: setToolTipText((String) (newAction.getValue(Action.SHORT_DESCRIPTION)));
273: setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
274: setEnabled(this.action.isEnabled());
275:
276: Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
277: if (o != null)
278: {
279: if (o instanceof Character)
280: {
281: final Character c = (Character) o;
282: setMnemonic(c.charValue());
283: }
284: else if (o instanceof Integer)
285: {
286: final Integer c = (Integer) o;
287: setMnemonic(c.intValue());
288: }
289: }
290: o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
291: if (o instanceof KeyStroke && o != null)
292: {
293: final KeyStroke k = (KeyStroke) o;
294: registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW);
295: }
296: }
297: }
298: }