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