KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > actions > AbstractActionExt


1 /*
2  * $Id: AbstractActionExt.java,v 1.3 2004/09/07 18:16:00 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.actions;
9
10 import java.awt.event.ItemListener JavaDoc;
11
12 import java.beans.PropertyChangeListener JavaDoc;
13
14 import javax.swing.AbstractAction JavaDoc;
15 import javax.swing.Action JavaDoc;
16 import javax.swing.Icon JavaDoc;
17 import javax.swing.KeyStroke JavaDoc;
18 import javax.swing.JFrame JavaDoc;
19 import javax.swing.JOptionPane JavaDoc;
20
21 /**
22  * Extends the concept of the Action to include toggle or group states.
23  *
24  */

25 public abstract class AbstractActionExt extends AbstractAction JavaDoc
26     implements ItemListener JavaDoc {
27
28     /**
29      * The key for the large icon
30      */

31     public static final String JavaDoc LARGE_ICON = "__LargeIcon__";
32
33     /**
34      * The key for the button group
35      */

36     public static final String JavaDoc GROUP = "__Group__";
37
38     /**
39      * The key for the flag which indicates that this is a state type.
40      */

41     public static final String JavaDoc IS_STATE = "__State__";
42
43     /**
44      * Specified whether the action is selected; the default is false
45      */

46     private boolean selected = false;
47
48     /**
49      * Copy constuctor copies the state.
50      */

51     public AbstractActionExt(AbstractActionExt action) {
52         Object JavaDoc[] keys = action.getKeys();
53         for (int i = 0; i < keys.length; i++) {
54             putValue((String JavaDoc)keys[i], action.getValue((String JavaDoc)keys[i]));
55         }
56         this.selected = action.selected;
57         this.enabled = action.enabled;
58
59         // Copy change listeners.
60
PropertyChangeListener JavaDoc[] listeners = action.getPropertyChangeListeners();
61         for (int i = 0; i < listeners.length; i++) {
62             addPropertyChangeListener(listeners[i]);
63         }
64     }
65
66     public AbstractActionExt(String JavaDoc name) {
67         super(name);
68     }
69
70     public AbstractActionExt(String JavaDoc name, Icon JavaDoc icon) {
71         super(name, icon);
72     }
73
74     /**
75      * Constructs an Action with the label and command
76      *
77      * @param name name of the action usually used as a label
78      * @param command command key of the action
79      */

80     public AbstractActionExt(String JavaDoc name, String JavaDoc command) {
81         this(name);
82         setActionCommand(command);
83     }
84
85     /**
86      * @param name display name of the action
87      * @param command the value of the action command key
88      * @param icon icon to display
89      */

90     public AbstractActionExt(String JavaDoc name, String JavaDoc command, Icon JavaDoc icon) {
91         super(name, icon);
92         setActionCommand(command);
93     }
94     /**
95      * Returns a short desciption of the action.
96      *
97      * @return the short description or null
98      */

99     public String JavaDoc getShortDescription() {
100         return (String JavaDoc)getValue(Action.SHORT_DESCRIPTION);
101     }
102
103     /**
104      * Sets the short desciption of the action. This will also
105      * set the long description value is it is null.
106      * <p>
107      * This is a convenience method for <code>putValue</code> with the
108      * <code>Action.SHORT_DESCRIPTION</code> key.
109      *
110      * @param desc the short description; can be <code>null</code>w
111      * @see Action#SHORT_DESCRIPTION
112      * @see Action#putValue
113      */

114     public void setShortDescription(String JavaDoc desc) {
115         putValue(Action.SHORT_DESCRIPTION, desc);
116         if (desc != null && getLongDescription() == null) {
117             setLongDescription(desc);
118         }
119     }
120
121     /**
122      * Returns a long desciption of the action.
123      *
124      * @return the long description or null
125      */

126     public String JavaDoc getLongDescription() {
127         return (String JavaDoc)getValue(Action.LONG_DESCRIPTION);
128     }
129
130     /**
131      * Sets the long desciption of the action. This will also set the
132      * value of the short description if that value is null.
133      * <p>
134      * This is a convenience method for <code>putValue</code> with the
135      * <code>Action.LONG_DESCRIPTION</code> key.
136      *
137      * @param desc the long description; can be <code>null</code>
138      * @see Action#LONG_DESCRIPTION
139      * @see Action#putValue
140      */

141     public void setLongDescription(String JavaDoc desc) {
142         putValue(Action.LONG_DESCRIPTION, desc);
143         if (desc != null && getShortDescription() == null) {
144             setLongDescription(desc);
145         }
146     }
147
148     /**
149      * Returns a small icon which represents the action.
150      *
151      * @return the small icon or null
152      */

153     public Icon JavaDoc getSmallIcon() {
154         return (Icon JavaDoc)getValue(SMALL_ICON);
155     }
156
157     /**
158      * Sets the small icon which represents the action.
159      * <p>
160      * This is a convenience method for <code>putValue</code> with the
161      * <code>Action.SMALL_ICON</code> key.
162      *
163      * @param icon the small icon; can be <code>null</code>
164      * @see Action#SMALL_ICON
165      * @see Action#putValue
166      */

167     public void setSmallIcon(Icon JavaDoc icon) {
168         putValue(SMALL_ICON, icon);
169     }
170
171     /**
172      * Returns a large icon which represents the action.
173      *
174      * @return the large icon or null
175      */

176     public Icon JavaDoc getLargeIcon() {
177         return (Icon JavaDoc)getValue(LARGE_ICON);
178     }
179
180     /**
181      * Sets the large icon which represents the action.
182      * <p>
183      * This is a convenience method for <code>putValue</code> with the
184      * <code>LARGE_ICON</code> key.
185      *
186      * @param icon the large icon; can be <code>null</code>
187      * @see #LARGE_ICON
188      * @see Action#putValue
189      */

190     public void setLargeIcon(Icon JavaDoc icon) {
191         putValue(LARGE_ICON, icon);
192     }
193
194     /**
195      * Sets the name of the action.
196      * <p>
197      * This is a convenience method for <code>putValue</code> with the
198      * <code>Action.NAME</code> key.
199      *
200      * @param name the name of the action; can be <code>null</code>
201      * @see Action#NAME
202      * @see Action#putValue
203      */

204     public void setName(String JavaDoc name) {
205         putValue(Action.NAME, name);
206     }
207
208     /**
209      * Returns the name of the action.
210      *
211      * @return the name of the action or null
212      */

213     public String JavaDoc getName() {
214         return (String JavaDoc)getValue(Action.NAME);
215     }
216
217     public void setMnemonic(String JavaDoc mnemonic) {
218         if (mnemonic != null && mnemonic.length() > 0) {
219             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(mnemonic.charAt(0)));
220         }
221     }
222
223     /**
224      * Sets the mnemonic key code for the action.
225      * <p>
226      * This is a convenience method for <code>putValue</code> with the
227      * <code>Action.MNEMONIC_KEY</code> key.
228      * <p>
229      * This method does not validate the value. Please see
230      * {@link javax.swing.AbstractButton#setMnemonic(int)} for details
231      * concerning the value of the mnemonic.
232      *
233      * @param mnemonic an int key code mnemonic or 0
234      * @see javax.swing.AbstractButton#setMnemonic(int)
235      * @see Action#MNEMONIC_KEY
236      * @see Action#putValue
237      */

238     public void setMnemonic(int mnemonic) {
239         putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(mnemonic));
240     }
241
242     /**
243      * Return the mnemonic key code for the action.
244      *
245      * @return the mnemonic or 0
246      */

247     public int getMnemonic() {
248         Integer JavaDoc value = (Integer JavaDoc)getValue(Action.MNEMONIC_KEY);
249         if (value != null) {
250             return value.intValue();
251         }
252         return '\0';
253     }
254
255     /**
256      * Sets the action command key. The action command key
257      * is used to identify the action.
258      * <p>
259      * This is a convenience method for <code>putValue</code> with the
260      * <code>Action.ACTION_COMMAND_KEY</code> key.
261      *
262      * @param key the action command
263      * @see Action#ACTION_COMMAND_KEY
264      * @see Action#putValue
265      */

266     public void setActionCommand(Object JavaDoc key) {
267         putValue(Action.ACTION_COMMAND_KEY, key);
268     }
269
270     /**
271      * Returns the action command.
272      *
273      * @return the action command or null
274      */

275     public Object JavaDoc getActionCommand() {
276         return getValue(Action.ACTION_COMMAND_KEY);
277     }
278
279     /**
280      * Returns the key stroke which represents an accelerator
281      * for the action.
282      *
283      * @return the key stroke or null
284      */

285     public KeyStroke JavaDoc getAccelerator() {
286         return (KeyStroke JavaDoc)getValue(Action.ACCELERATOR_KEY);
287     }
288
289     /**
290      * Sets the key stroke which represents an accelerator
291      * for the action.
292      * <p>
293      * This is a convenience method for <code>putValue</code> with the
294      * <code>Action.ACCELERATOR_KEY</code> key.
295      *
296      * @param key the key stroke; can be <code>null</code>
297      * @see Action#ACCELERATOR_KEY
298      * @see Action#putValue
299      */

300     public void setAccelerator(KeyStroke JavaDoc key) {
301         putValue(Action.ACCELERATOR_KEY, key);
302     }
303
304     /**
305      * Sets the group identity of the state action. This is used to
306      * identify the action as part of a button group.
307      */

308     public void setGroup(Object JavaDoc group) {
309         putValue(GROUP, group);
310     }
311
312     public Object JavaDoc getGroup() {
313         return getValue(GROUP);
314     }
315
316     /**
317      * Will perform cleanup on the object.
318      * Should be called when finished with the Action. This should be used if
319      * a new action is constructed from the properties of an old action.
320      * The old action properties should be disposed.
321      */

322     public void dispose() {
323         PropertyChangeListener JavaDoc[] listeners = getPropertyChangeListeners();
324         for (int i = 0; i < listeners.length; i++) {
325             removePropertyChangeListener(listeners[i]);
326         }
327     }
328
329     // Properties etc....
330

331     /**
332      * Inicates if this action has states. If this method returns
333      * true then the this will send ItemEvents to ItemListeners
334      * when the control constructed with this action in invoked.
335      *
336      * @return true if this can handle states
337      */

338     public boolean isStateAction() {
339         Boolean JavaDoc state = (Boolean JavaDoc)getValue(IS_STATE);
340         if (state != null) {
341             return state.booleanValue();
342         }
343         return false;
344     }
345
346     /**
347      * Set the state property to true.
348      */

349     public void setStateAction() {
350         setStateAction(true);
351     }
352
353     /**
354      * Set the state property.
355      *
356      * @param state if true then this action will fire ItemEvents
357      */

358     public void setStateAction(boolean state) {
359         putValue(IS_STATE, Boolean.valueOf(state));
360     }
361
362     /**
363      * @return true if the action is in the selected state
364      */

365     public boolean isSelected() {
366         return selected;
367     }
368
369     /**
370      * Changes the state of the action
371      * @param newValue true to set the action as selected of the action.
372      */

373     public synchronized void setSelected(boolean newValue) {
374         boolean oldValue = this.selected;
375         if (oldValue != newValue) {
376             this.selected = newValue;
377             firePropertyChange("selected", Boolean.valueOf(oldValue),
378                                Boolean.valueOf(newValue));
379         }
380     }
381
382     public String JavaDoc toString() {
383         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("[");
384         // RG: Fix for J2SE 5.0; Can't cascade append() calls because
385
// return type in StringBuffer and AbstractStringBuilder are different
386
buffer.append(this.getClass().toString());
387         buffer.append(":");
388         try {
389             Object JavaDoc[] keys = getKeys();
390             for (int i = 0; i < keys.length; i++) {
391                 buffer.append(keys[i]);
392                 buffer.append('=');
393                 buffer.append(getValue( (String JavaDoc) keys[i]).toString());
394                 if (i < keys.length - 1) {
395                     buffer.append(',');
396                 }
397             }
398             buffer.append(']');
399         }
400         catch (Exception JavaDoc ex) { // RG: append(char) throws IOException in J2SE 5.0
401
/** @todo Log it */
402         }
403         return buffer.toString();
404     }
405 }
406
Popular Tags