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 ActionButton extends JButton {
64:
65:
68: private Action action;
69:
70:
73: private ActionEnablePropertyChangeHandler propertyChangeHandler;
74:
75:
79: private class ActionEnablePropertyChangeHandler implements PropertyChangeListener {
80:
81:
84: public ActionEnablePropertyChangeHandler() {
85: }
86:
87:
92: public void propertyChange(final PropertyChangeEvent event) {
93: try {
94: if (event.getPropertyName().equals("enabled")) {
95: setEnabled(getAction().isEnabled());
96: }
97: else if (event.getPropertyName().equals(Action.SMALL_ICON)) {
98: setIcon((Icon) getAction().getValue(Action.SMALL_ICON));
99: }
100: else if (event.getPropertyName().equals(Action.NAME)) {
101: setText((String) getAction().getValue
102: (Action.NAME));
103: }
104: else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION)) {
105: ActionButton.this.setToolTipText((String)
106: getAction().getValue(Action.SHORT_DESCRIPTION));
107: }
108:
109: final Action ac = getAction();
110: if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY)) {
111: final KeyStroke oldVal = (KeyStroke) event.getOldValue();
112: if (oldVal != null) {
113: unregisterKeyboardAction(oldVal);
114: }
115: final Object o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY);
116: if (o instanceof KeyStroke) {
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: final Object o = ac.getValue(ActionDowngrade.MNEMONIC_KEY);
123: if (o != null) {
124: if (o instanceof Character) {
125: final Character c = (Character) o;
126: setMnemonic(c.charValue());
127: }
128: else if (o instanceof Integer) {
129: final Integer c = (Integer) o;
130: setMnemonic(c.intValue());
131: }
132: }
133: }
134: }
135: catch (Exception e) {
136: Log.warn("Error on PropertyChange in ActionButton: ", e);
137: }
138: }
139: }
140:
141:
144: public ActionButton() {
145: super();
146: }
147:
148:
153: public ActionButton(final String text) {
154: super(text);
155: }
156:
157:
163: public ActionButton(final String text, final Icon icon) {
164: super(text, icon);
165: }
166:
167:
168:
173: public ActionButton(final Icon icon) {
174: super(icon);
175: }
176:
177:
182: public ActionButton(final Action action) {
183: setAction(action);
184: }
185:
186:
191: public Action getAction() {
192: return this.action;
193: }
194:
195:
196:
202: private ActionEnablePropertyChangeHandler getPropertyChangeHandler() {
203: if (this.propertyChangeHandler == null) {
204: this.propertyChangeHandler = new ActionEnablePropertyChangeHandler();
205: }
206: return this.propertyChangeHandler;
207: }
208:
209:
215: public void setEnabled(final boolean b) {
216: super.setEnabled(b);
217: if (getAction() != null) {
218: getAction().setEnabled(b);
219: }
220: }
221:
222:
235: public void setAction(final Action newAction) {
236: final Action oldAction = getAction();
237: if (oldAction != null) {
238: removeActionListener(oldAction);
239: oldAction.removePropertyChangeListener(getPropertyChangeHandler());
240:
241: final Object o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
242: if (o instanceof KeyStroke) {
243: final KeyStroke k = (KeyStroke) o;
244: unregisterKeyboardAction(k);
245: }
246: }
247: this.action = newAction;
248: if (this.action != null) {
249: addActionListener(newAction);
250: newAction.addPropertyChangeListener(getPropertyChangeHandler());
251:
252: setText((String) (newAction.getValue(Action.NAME)));
253: setToolTipText((String) (newAction.getValue(Action.SHORT_DESCRIPTION)));
254: setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
255: setEnabled(this.action.isEnabled());
256:
257: Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
258: if (o != null) {
259: if (o instanceof Character) {
260: final Character c = (Character) o;
261: setMnemonic(c.charValue());
262: }
263: else if (o instanceof Integer) {
264: final Integer c = (Integer) o;
265: setMnemonic(c.intValue());
266: }
267: }
268: o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
269: if (o instanceof KeyStroke) {
270: final KeyStroke k = (KeyStroke) o;
271: registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW);
272: }
273: }
274: }
275: }