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, by Thomas Morgner and Contributors.
031: *
032: * Original Author: Thomas Morgner;
033: * Contributor(s): David Gilbert (for Object Refinery Limited);
034: *
035: * $Id: ActionRadioButton.java,v 1.3 2005/10/18 13:22:11 mungady 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.JRadioButton;
054: import javax.swing.KeyStroke;
055:
056: import org.jfree.util.Log;
057:
058: /**
059: * The ActionRadioButton is used to connect an Action and its properties to a JRadioButton.
060: * This functionality is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
061: *
062: * @author Thomas Morgner
063: */
064: public class ActionRadioButton extends JRadioButton {
065: /** The action. */
066: private Action action;
067:
068: /** The property change handler. */
069: private ActionEnablePropertyChangeHandler propertyChangeHandler;
070:
071: /**
072: * Helperclass to handle the property change event raised by the action. Changed properties in
073: * the action will affect the button.
074: */
075: private class ActionEnablePropertyChangeHandler implements
076: PropertyChangeListener {
077: /**
078: * Receives notification of a property change event.
079: *
080: * @param event the property change event.
081: */
082: public void propertyChange(final PropertyChangeEvent event) {
083: try {
084: if (event.getPropertyName().equals("enabled")) {
085: setEnabled(getAction().isEnabled());
086: } else if (event.getPropertyName().equals(
087: Action.SMALL_ICON)) {
088: setIcon((Icon) getAction().getValue(
089: Action.SMALL_ICON));
090: } else if (event.getPropertyName().equals(Action.NAME)) {
091: setText((String) getAction().getValue(Action.NAME));
092: } else if (event.getPropertyName().equals(
093: Action.SHORT_DESCRIPTION)) {
094: ActionRadioButton.this
095: .setToolTipText((String) getAction()
096: .getValue(Action.SHORT_DESCRIPTION));
097: }
098:
099: final Action ac = getAction();
100: if (event.getPropertyName().equals(
101: ActionDowngrade.ACCELERATOR_KEY)) {
102: final KeyStroke oldVal = (KeyStroke) event
103: .getOldValue();
104: if (oldVal != null) {
105: unregisterKeyboardAction(oldVal);
106: }
107: final Object o = ac
108: .getValue(ActionDowngrade.ACCELERATOR_KEY);
109: if (o instanceof KeyStroke && o != null) {
110: final KeyStroke k = (KeyStroke) o;
111: registerKeyboardAction(ac, k,
112: WHEN_IN_FOCUSED_WINDOW);
113: }
114: } else if (event.getPropertyName().equals(
115: ActionDowngrade.MNEMONIC_KEY)) {
116: final Object o = ac
117: .getValue(ActionDowngrade.MNEMONIC_KEY);
118: if (o != null) {
119: if (o instanceof Character) {
120: final Character c = (Character) o;
121: setMnemonic(c.charValue());
122: } else if (o instanceof Integer) {
123: final Integer c = (Integer) o;
124: setMnemonic(c.intValue());
125: }
126: }
127: }
128: } catch (Exception e) {
129: Log
130: .warn(
131: "Error on PropertyChange in ActionButton: ",
132: e);
133: }
134: }
135: }
136:
137: /**
138: * Creates a Button without any text and without an assigned Action.
139: */
140: public ActionRadioButton() {
141: super ();
142: }
143:
144: /**
145: * Creates a Button and set the given text as label.
146: *
147: * @param text the label for the new button.
148: */
149: public ActionRadioButton(final String text) {
150: super (text);
151: }
152:
153: /**
154: * Creates an ActionButton and sets the given text and icon on the button.
155: *
156: * @param text the label for the new button.
157: * @param icon the icon for the button.
158: */
159: public ActionRadioButton(final String text, final Icon icon) {
160: super (text, icon);
161: }
162:
163: /**
164: * Creates an ActionButton and sets the given icon on the button.
165: *
166: * @param icon the icon for the button.
167: */
168: public ActionRadioButton(final Icon icon) {
169: super (icon);
170: }
171:
172: /**
173: * Nreates an ActionButton and assigns the given action with the button.
174: *
175: * @param action the action.
176: */
177: public ActionRadioButton(final Action action) {
178: setAction(action);
179: }
180:
181: /**
182: * Returns the assigned action or null if no action has been assigned.
183: *
184: * @return the action (possibly null).
185: */
186: public Action getAction() {
187: return this .action;
188: }
189:
190: /**
191: * Returns and initializes the PropertyChangehandler for this ActionButton.
192: * The PropertyChangeHandler monitors the action and updates the button if necessary.
193: *
194: * @return the property change handler.
195: */
196: private ActionEnablePropertyChangeHandler getPropertyChangeHandler() {
197: if (this .propertyChangeHandler == null) {
198: this .propertyChangeHandler = new ActionEnablePropertyChangeHandler();
199: }
200: return this .propertyChangeHandler;
201: }
202:
203: /**
204: * Enables and disables this button and if an action is assigned to this button the
205: * propertychange is forwarded to the assigned action.
206: *
207: * @param b the new enable-state of this button
208: */
209: public void setEnabled(final boolean b) {
210: super .setEnabled(b);
211: if (getAction() != null) {
212: getAction().setEnabled(b);
213: }
214: }
215:
216: /**
217: * Assigns the given action to this button. The properties of the action will be assigned to
218: * the button. If an previous action was set, the old action is unregistered.
219: * <p>
220: * <ul>
221: * <li>NAME - specifies the button text
222: * <li>SMALL_ICON - specifies the buttons icon
223: * <li>MNEMONIC_KEY - specifies the buttons mnemonic key
224: * <li>ACCELERATOR_KEY - specifies the buttons accelerator
225: * </ul>
226: *
227: * @param newAction the new action
228: */
229: public void setAction(final Action newAction) {
230: final Action oldAction = getAction();
231: if (oldAction != null) {
232: removeActionListener(oldAction);
233: oldAction
234: .removePropertyChangeListener(getPropertyChangeHandler());
235:
236: final Object o = oldAction
237: .getValue(ActionDowngrade.ACCELERATOR_KEY);
238: if (o instanceof KeyStroke && o != null) {
239: final KeyStroke k = (KeyStroke) o;
240: unregisterKeyboardAction(k);
241: }
242: }
243: this .action = newAction;
244: if (this .action != null) {
245: addActionListener(newAction);
246: newAction
247: .addPropertyChangeListener(getPropertyChangeHandler());
248:
249: setText((String) (newAction.getValue(Action.NAME)));
250: setToolTipText((String) (newAction
251: .getValue(Action.SHORT_DESCRIPTION)));
252: setIcon((Icon) newAction.getValue(Action.SMALL_ICON));
253: setEnabled(this .action.isEnabled());
254:
255: Object o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
256: if (o != null) {
257: if (o instanceof Character) {
258: final Character c = (Character) o;
259: setMnemonic(c.charValue());
260: } else if (o instanceof Integer) {
261: final Integer c = (Integer) o;
262: setMnemonic(c.intValue());
263: }
264: }
265: o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
266: if (o instanceof KeyStroke && o != null) {
267: final KeyStroke k = (KeyStroke) o;
268: registerKeyboardAction(newAction, k,
269: WHEN_IN_FOCUSED_WINDOW);
270: }
271: }
272: }
273: }
|