KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > ui > action > ActionButton


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * -----------------
28  * ActionButton.java
29  * -----------------
30  * (C)opyright 2002-2004, by Thomas Morgner and Contributors.
31  *
32  * Original Author: Thomas Morgner;
33  * Contributor(s): David Gilbert (for Object Refinery Limited);
34  *
35  * $Id: ActionButton.java,v 1.6 2006/11/02 13:10:35 taqua Exp $
36  *
37  * ChangeLog
38  * ---------
39  * 30-Aug-2002 : Initial version
40  * 01-Sep-2002 : Documentation
41  * 10-Dec-2002 : Minor Javadoc updates (DG);
42  * 07-Jun-2004 : Corrected source headers (DG);
43  *
44  */

45
46 package org.jfree.ui.action;
47
48 import java.beans.PropertyChangeEvent JavaDoc;
49 import java.beans.PropertyChangeListener JavaDoc;
50
51 import javax.swing.Action JavaDoc;
52 import javax.swing.Icon JavaDoc;
53 import javax.swing.JButton JavaDoc;
54 import javax.swing.KeyStroke JavaDoc;
55
56 import org.jfree.util.Log;
57
58 /**
59  * The ActionButton is used to connect an Action and its properties to a Button. This functionality
60  * is already implemented in JDK 1.3 but needed for JDK 1.2.2 compatibility.
61  *
62  * @author Thomas Morgner
63  */

64 public class ActionButton extends JButton JavaDoc {
65
66     /**
67      * The action.
68      */

69     private Action JavaDoc action;
70
71     /**
72      * The property change handler.
73      */

74     private ActionEnablePropertyChangeHandler propertyChangeHandler;
75
76     /**
77      * Helperclass to handle the property change event raised by the action. Changed properties in
78      * the action will affect the button.
79      */

80     private class ActionEnablePropertyChangeHandler implements PropertyChangeListener JavaDoc {
81
82         /**
83          * Default constructor.
84          */

85         public ActionEnablePropertyChangeHandler() {
86         }
87
88         /**
89          * Receives notification of a property change event.
90          *
91          * @param event the property change event.
92          */

93         public void propertyChange(final PropertyChangeEvent JavaDoc event) {
94             try {
95                 if (event.getPropertyName().equals("enabled")) {
96                     setEnabled(getAction().isEnabled());
97                 }
98                 else if (event.getPropertyName().equals(Action.SMALL_ICON)) {
99                     setIcon((Icon JavaDoc) getAction().getValue(Action.SMALL_ICON));
100                 }
101                 else if (event.getPropertyName().equals(Action.NAME)) {
102                     setText((String JavaDoc) getAction().getValue
103                         (Action.NAME));
104                 }
105                 else if (event.getPropertyName().equals(Action.SHORT_DESCRIPTION)) {
106                     ActionButton.this.setToolTipText((String JavaDoc)
107                         getAction().getValue(Action.SHORT_DESCRIPTION));
108                 }
109
110                 final Action JavaDoc ac = getAction();
111                 if (event.getPropertyName().equals(ActionDowngrade.ACCELERATOR_KEY)) {
112                     final KeyStroke JavaDoc oldVal = (KeyStroke JavaDoc) event.getOldValue();
113                     if (oldVal != null) {
114                         unregisterKeyboardAction(oldVal);
115                     }
116                     final Object JavaDoc o = ac.getValue(ActionDowngrade.ACCELERATOR_KEY);
117                     if (o instanceof KeyStroke JavaDoc) {
118                         final KeyStroke JavaDoc k = (KeyStroke JavaDoc) o;
119                         registerKeyboardAction(ac, k, WHEN_IN_FOCUSED_WINDOW);
120                     }
121                 }
122                 else if (event.getPropertyName().equals(ActionDowngrade.MNEMONIC_KEY)) {
123                     final Object JavaDoc o = ac.getValue(ActionDowngrade.MNEMONIC_KEY);
124                     if (o != null) {
125                         if (o instanceof Character JavaDoc) {
126                             final Character JavaDoc c = (Character JavaDoc) o;
127                             setMnemonic(c.charValue());
128                         }
129                         else if (o instanceof Integer JavaDoc) {
130                             final Integer JavaDoc c = (Integer JavaDoc) o;
131                             setMnemonic(c.intValue());
132                         }
133                     }
134                 }
135             }
136             catch (Exception JavaDoc e) {
137                 Log.warn("Error on PropertyChange in ActionButton: ", e);
138             }
139         }
140     }
141
142     /**
143      * Creates a Button without any text and without an assigned Action.
144      */

145     public ActionButton() {
146         super();
147     }
148
149     /**
150      * Creates a Button and set the given text as label.
151      *
152      * @param text the label for the new button.
153      */

154     public ActionButton(final String JavaDoc text) {
155         super(text);
156     }
157
158     /**
159      * Creates an ActionButton and sets the given text and icon on the button.
160      *
161      * @param text the label for the new button.
162      * @param icon the icon for the button.
163      */

164     public ActionButton(final String JavaDoc text, final Icon JavaDoc icon) {
165         super(text, icon);
166     }
167
168
169     /**
170      * Creates an ActionButton and sets the given icon on the button.
171      *
172      * @param icon the icon for the button.
173      */

174     public ActionButton(final Icon JavaDoc icon) {
175         super(icon);
176     }
177
178     /**
179      * Nreates an ActionButton and assigns the given action with the button.
180      *
181      * @param action the action.
182      */

183     public ActionButton(final Action JavaDoc 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 (possibly null).
191      */

192     public Action JavaDoc getAction() {
193         return this.action;
194     }
195
196
197     /**
198      * Returns and initializes the PropertyChangehandler for this ActionButton.
199      * The PropertyChangeHandler monitors the action and updates the button if 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 button the
212      * propertychange is forwarded to the assigned action.
213      *
214      * @param b the new enable-state of this button
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 button. The properties of the action will be assigned to
225      * the button. If an previous action was set, the old action is unregistered.
226      * <p/>
227      * <ul>
228      * <li>NAME - specifies the button text
229      * <li>SMALL_ICON - specifies the buttons icon
230      * <li>MNEMONIC_KEY - specifies the buttons mnemonic key
231      * <li>ACCELERATOR_KEY - specifies the buttons accelerator
232      * </ul>
233      *
234      * @param newAction the new action
235      */

236     public void setAction(final Action JavaDoc newAction) {
237         final Action JavaDoc oldAction = getAction();
238         if (oldAction != null) {
239             removeActionListener(oldAction);
240             oldAction.removePropertyChangeListener(getPropertyChangeHandler());
241
242             final Object JavaDoc o = oldAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
243             if (o instanceof KeyStroke JavaDoc) {
244                 final KeyStroke JavaDoc k = (KeyStroke JavaDoc) o;
245                 unregisterKeyboardAction(k);
246             }
247         }
248         this.action = newAction;
249         if (this.action != null) {
250             addActionListener(newAction);
251             newAction.addPropertyChangeListener(getPropertyChangeHandler());
252
253             setText((String JavaDoc) (newAction.getValue(Action.NAME)));
254             setToolTipText((String JavaDoc) (newAction.getValue(Action.SHORT_DESCRIPTION)));
255             setIcon((Icon JavaDoc) newAction.getValue(Action.SMALL_ICON));
256             setEnabled(this.action.isEnabled());
257
258             Object JavaDoc o = newAction.getValue(ActionDowngrade.MNEMONIC_KEY);
259             if (o != null) {
260                 if (o instanceof Character JavaDoc) {
261                     final Character JavaDoc c = (Character JavaDoc) o;
262                     setMnemonic(c.charValue());
263                 }
264                 else if (o instanceof Integer JavaDoc) {
265                     final Integer JavaDoc c = (Integer JavaDoc) o;
266                     setMnemonic(c.intValue());
267                 }
268             }
269             o = newAction.getValue(ActionDowngrade.ACCELERATOR_KEY);
270             if (o instanceof KeyStroke JavaDoc) {
271                 final KeyStroke JavaDoc k = (KeyStroke JavaDoc) o;
272                 registerKeyboardAction(newAction, k, WHEN_IN_FOCUSED_WINDOW);
273             }
274         }
275     }
276 }
277
278
Popular Tags