001: /* ========================================================================
002: * JCommon : a free general purpose class library for the Java(tm) platform
003: * ========================================================================
004: *
005: * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jcommon/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * -----------------
028: * ActionButton.java
029: * -----------------
030: * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: ActionButton.java,v 1.6 2006/11/02 13:10:35 taqua Exp $
036: *
037: * ChangeLog
038: * ---------
039: * 30-Aug-2002 : Initial version
040: * 01-Sep-2002 : Documentation
041: * 10-Dec-2002 : Minor Javadoc updates (DG);
042: * 07-Jun-2004 : Corrected source headers (DG);
043: *
044: */
045:
046: package org.jfree.ui.action;
047:
048: import java.beans.PropertyChangeEvent;
049: import java.beans.PropertyChangeListener;
050:
051: import javax.swing.Action;
052: import javax.swing.Icon;
053: import javax.swing.JButton;
054: import javax.swing.KeyStroke;
055:
056: import org.jfree.util.Log;
057:
058: /**
059: * The ActionButton is used to connect an Action and its properties to a Button. This functionality
060: * is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
061: *
062: * @author Thomas Morgner
063: */
064: public class ActionButton extends JButton {
065:
066: /**
067: * The action.
068: */
069: private Action action;
070:
071: /**
072: * The property change handler.
073: */
074: private ActionEnablePropertyChangeHandler propertyChangeHandler;
075:
076: /**
077: * Helperclass to handle the property change event raised by the action. Changed properties in
078: * the action will affect the button.
079: */
080: private class ActionEnablePropertyChangeHandler implements
081: PropertyChangeListener {
082:
083: /**
084: * Default constructor.
085: */
086: public ActionEnablePropertyChangeHandler() {
087: }
088:
089: /**
090: * Receives notification of a property change event.
091: *
092: * @param event the property change event.
093: */
094: public void propertyChange(final PropertyChangeEvent event) {
095: try {
096: if (event.getPropertyName().equals("enabled")) {
097: setEnabled(getAction().isEnabled());
098: } else if (event.getPropertyName().equals(
099: Action.SMALL_ICON)) {
100: setIcon((Icon) getAction().getValue(
101: Action.SMALL_ICON));
102: } else if (event.getPropertyName().equals(Action.NAME)) {
103: setText((String) getAction().getValue(Action.NAME));
104: } else if (event.getPropertyName().equals(
105: Action.SHORT_DESCRIPTION)) {
106: ActionButton.this
107: .setToolTipText((String) getAction()
108: .getValue(Action.SHORT_DESCRIPTION));
109: }
110:
111: final Action ac = getAction();
112: if (event.getPropertyName().equals(
113: ActionDowngrade.ACCELERATOR_KEY)) {
114: final KeyStroke oldVal = (KeyStroke) event
115: .getOldValue();
116: if (oldVal != null) {
117: unregisterKeyboardAction(oldVal);
118: }
119: final Object o = ac
120: .getValue(ActionDowngrade.ACCELERATOR_KEY);
121: if (o instanceof KeyStroke) {
122: final KeyStroke k = (KeyStroke) o;
123: registerKeyboardAction(ac, k,
124: WHEN_IN_FOCUSED_WINDOW);
125: }
126: } else if (event.getPropertyName().equals(
127: ActionDowngrade.MNEMONIC_KEY)) {
128: final Object o = ac
129: .getValue(ActionDowngrade.MNEMONIC_KEY);
130: if (o != null) {
131: if (o instanceof Character) {
132: final Character c = (Character) o;
133: setMnemonic(c.charValue());
134: } else if (o instanceof Integer) {
135: final Integer c = (Integer) o;
136: setMnemonic(c.intValue());
137: }
138: }
139: }
140: } catch (Exception e) {
141: Log
142: .warn(
143: "Error on PropertyChange in ActionButton: ",
144: e);
145: }
146: }
147: }
148:
149: /**
150: * Creates a Button without any text and without an assigned Action.
151: */
152: public ActionButton() {
153: super ();
154: }
155:
156: /**
157: * Creates a Button and set the given text as label.
158: *
159: * @param text the label for the new button.
160: */
161: public ActionButton(final String text) {
162: super (text);
163: }
164:
165: /**
166: * Creates an ActionButton and sets the given text and icon on the button.
167: *
168: * @param text the label for the new button.
169: * @param icon the icon for the button.
170: */
171: public ActionButton(final String text, final Icon icon) {
172: super (text, icon);
173: }
174:
175: /**
176: * Creates an ActionButton and sets the given icon on the button.
177: *
178: * @param icon the icon for the button.
179: */
180: public ActionButton(final Icon icon) {
181: super (icon);
182: }
183:
184: /**
185: * Nreates an ActionButton and assigns the given action with the button.
186: *
187: * @param action the action.
188: */
189: public ActionButton(final Action action) {
190: setAction(action);
191: }
192:
193: /**
194: * Returns the assigned action or null if no action has been assigned.
195: *
196: * @return the action (possibly null).
197: */
198: public Action getAction() {
199: return this .action;
200: }
201:
202: /**
203: * Returns and initializes the PropertyChangehandler for this ActionButton.
204: * The PropertyChangeHandler monitors the action and updates the button if necessary.
205: *
206: * @return the property change handler.
207: */
208: private ActionEnablePropertyChangeHandler getPropertyChangeHandler() {
209: if (this .propertyChangeHandler == null) {
210: this .propertyChangeHandler = new ActionEnablePropertyChangeHandler();
211: }
212: return this .propertyChangeHandler;
213: }
214:
215: /**
216: * Enables and disables this button and if an action is assigned to this button the
217: * propertychange is forwarded to the assigned action.
218: *
219: * @param b the new enable-state of this button
220: */
221: public void setEnabled(final boolean b) {
222: super .setEnabled(b);
223: if (getAction() != null) {
224: getAction().setEnabled(b);
225: }
226: }
227:
228: /**
229: * Assigns the given action to this button. The properties of the action will be assigned to
230: * the button. If an previous action was set, the old action is unregistered.
231: * <p/>
232: * <ul>
233: * <li>NAME - specifies the button text
234: * <li>SMALL_ICON - specifies the buttons icon
235: * <li>MNEMONIC_KEY - specifies the buttons mnemonic key
236: * <li>ACCELERATOR_KEY - specifies the buttons accelerator
237: * </ul>
238: *
239: * @param newAction the new action
240: */
241: public void setAction(final Action newAction) {
242: final Action oldAction = getAction();
243: if (oldAction != null) {
244: removeActionListener(oldAction);
245: oldAction
246: .removePropertyChangeListener(getPropertyChangeHandler());
247:
248: final Object o = oldAction
249: .getValue(ActionDowngrade.ACCELERATOR_KEY);
250: if (o instanceof KeyStroke) {
251: final KeyStroke k = (KeyStroke) o;
252: unregisterKeyboardAction(k);
253: }
254: }
255: this .action = newAction;
256: if (this .action != null) {
257: addActionListener(newAction);
258: newAction
259: .addPropertyChangeListener(getPropertyChangeHandler());
260:
261: setText((String) (newAction.getValue(Action.NAME)));
262: setToolTipText((String) (newAction
263: .getValue(Action.SHORT_DESCRIPTION)));
264: setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
265: setEnabled(this .action.isEnabled());
266:
267: Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
268: if (o != null) {
269: if (o instanceof Character) {
270: final Character c = (Character) o;
271: setMnemonic(c.charValue());
272: } else if (o instanceof Integer) {
273: final Integer c = (Integer) o;
274: setMnemonic(c.intValue());
275: }
276: }
277: o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
278: if (o instanceof KeyStroke) {
279: final KeyStroke k = (KeyStroke) o;
280: registerKeyboardAction(newAction, k,
281: WHEN_IN_FOCUSED_WINDOW);
282: }
283: }
284: }
285: }
|