001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.awt.*;
028 import java.awt.event.*;
029 import java.beans.*;
030
031 /**
032 * The <code>Action</code> interface provides a useful extension to the
033 * <code>ActionListener</code>
034 * interface in cases where the same functionality may be accessed by
035 * several controls.
036 * <p>
037 * In addition to the <code>actionPerformed</code> method defined by the
038 * <code>ActionListener</code> interface, this interface allows the
039 * application to define, in a single place:
040 * <ul>
041 * <li>One or more text strings that describe the function. These strings
042 * can be used, for example, to display the flyover text for a button
043 * or to set the text in a menu item.
044 * <li>One or more icons that depict the function. These icons can be used
045 * for the images in a menu control, or for composite entries in a more
046 * sophisticated user interface.
047 * <li>The enabled/disabled state of the functionality. Instead of having
048 * to separately disable the menu item and the toolbar button, the
049 * application can disable the function that implements this interface.
050 * All components which are registered as listeners for the state change
051 * then know to disable event generation for that item and to modify the
052 * display accordingly.
053 * </ul>
054 * <p>
055 * This interface can be added to an existing class or used to create an
056 * adapter (typically, by subclassing <code>AbstractAction</code>).
057 * The <code>Action</code> object
058 * can then be added to multiple <code>Action</code>-aware containers
059 * and connected to <code>Action</code>-capable
060 * components. The GUI controls can then be activated or
061 * deactivated all at once by invoking the <code>Action</code> object's
062 * <code>setEnabled</code> method.
063 * <p>
064 * Note that <code>Action</code> implementations tend to be more expensive
065 * in terms of storage than a typical <code>ActionListener</code>,
066 * which does not offer the benefits of centralized control of
067 * functionality and broadcast of property changes. For this reason,
068 * you should take care to only use <code>Action</code>s where their benefits
069 * are desired, and use simple <code>ActionListener</code>s elsewhere.
070 * <p>
071 *
072 * <h4><a name="buttonActions"></a>Swing Components Supporting <code>Action</code></h4>
073 * <p>
074 * Many of Swing's components have an <code>Action</code> property. When
075 * an <code>Action</code> is set on a component, the following things
076 * happen:
077 * <ul>
078 * <li>The <code>Action</code> is added as an <code>ActionListener</code> to
079 * the component.
080 * <li>The component configures some of its properties to match the
081 * <code>Action</code>.
082 * <li>The component installs a <code>PropertyChangeListener</code> on the
083 * <code>Action</code> so that the component can change its properties
084 * to reflect changes in the <code>Action</code>'s properties.
085 * </ul>
086 * <p>
087 * The following table describes the properties used by
088 * <code>Swing</code> components that support <code>Actions</code>.
089 * In the table, <em>button</em> refers to any
090 * <code>AbstractButton</code> subclass, which includes not only
091 * <code>JButton</code> but also classes such as
092 * <code>JMenuItem</code>. Unless otherwise stated, a
093 * <code>null</code> property value in an <code>Action</code> (or a
094 * <code>Action</code> that is <code>null</code>) results in the
095 * button's corresponding property being set to <code>null</code>.
096 * <p>
097 * <table border="1" cellpadding="1" cellspacing="0"
098 * summary="Supported Action properties"
099 * valign="top" >
100 * <tr valign="top" align="left">
101 * <th bgcolor="#CCCCFF" align="left">Component Property
102 * <th bgcolor="#CCCCFF" align="left">Components
103 * <th bgcolor="#CCCCFF" align="left">Action Key
104 * <th bgcolor="#CCCCFF" align="left">Notes
105 * <tr valign="top" align="left">
106 * <td><b><code>enabled</code></b>
107 * <td>All
108 * <td>The <code>isEnabled</code> method
109 * <td>
110 * <tr valign="top" align="left">
111 * <td><b><code>toolTipText</code></b>
112 * <td>All
113 * <td><code>SHORT_DESCRIPTION</code>
114 * <td>
115 * <tr valign="top" align="left">
116 * <td><b><code>actionCommand</code></b>
117 * <td>All
118 * <td><code>ACTION_COMMAND_KEY</code>
119 * <td>
120 * <tr valign="top" align="left">
121 * <td><b><code>mnemonic</code></b>
122 * <td>All buttons
123 * <td><code>MNEMONIC_KEY</code>
124 * <td>A <code>null</code> value or <code>Action</code> results in the
125 * button's <code>mnemonic</code> property being set to
126 * <code>'\0'</code>.
127 * <tr valign="top" align="left">
128 * <td><b><code>text</code></b>
129 * <td>All buttons
130 * <td><code>NAME</code>
131 * <td>If you do not want the text of the button to mirror that
132 * of the <code>Action</code>, set the property
133 * <code>hideActionText</code> to <code>true</code>. If
134 * <code>hideActionText</code> is <code>true</code>, setting the
135 * <code>Action</code> changes the text of the button to
136 * <code>null</code> and any changes to <code>NAME</code>
137 * are ignored. <code>hideActionText</code> is useful for
138 * tool bar buttons that typically only show an <code>Icon</code>.
139 * <code>JToolBar.add(Action)</code> sets the property to
140 * <code>true</code> if the <code>Action</code> has a
141 * non-<code>null</code> value for <code>LARGE_ICON_KEY</code> or
142 * <code>SMALL_ICON</code>.
143 * <tr valign="top" align="left">
144 * <td><b><code>displayedMnemonicIndex</code></b>
145 * <td>All buttons
146 * <td><code>DISPLAYED_MNEMONIC_INDEX_KEY</code>
147 * <td>If the value of <code>DISPLAYED_MNEMONIC_INDEX_KEY</code> is
148 * beyond the bounds of the text, it is ignored. When
149 * <code>setAction</code> is called, if the value from the
150 * <code>Action</code> is <code>null</code>, the displayed
151 * mnemonic index is not updated. In any subsequent changes to
152 * <code>DISPLAYED_MNEMONIC_INDEX_KEY</code>, <code>null</code>
153 * is treated as -1.
154 * <tr valign="top" align="left">
155 * <td><b><code>icon</code></b>
156 * <td>All buttons except of <code>JCheckBox</code>,
157 * <code>JToggleButton</code> and <code>JRadioButton</code>.
158 * <td>either <code>LARGE_ICON_KEY</code> or
159 * <code>SMALL_ICON</code>
160 * <td>The <code>JMenuItem</code> subclasses only use
161 * <code>SMALL_ICON</code>. All other buttons will use
162 * <code>LARGE_ICON_KEY</code>; if the value is <code>null</code> they
163 * use <code>SMALL_ICON</code>.
164 * <tr valign="top" align="left">
165 * <td><b><code>accelerator</code></b>
166 * <td>All <code>JMenuItem</code> subclasses, with the exception of
167 * <code>JMenu</code>.
168 * <td><code>ACCELERATOR_KEY</code>
169 * <td>
170 * <tr valign="top" align="left">
171 * <td><b><code>selected</code></b>
172 * <td><code>JToggleButton</code>, <code>JCheckBox</code>,
173 * <code>JRadioButton</code>, <code>JCheckBoxMenuItem</code> and
174 * <code>JRadioButtonMenuItem</code>
175 * <td><code>SELECTED_KEY</code>
176 * <td>Components that honor this property only use
177 * the value if it is {@code non-null}. For example, if
178 * you set an {@code Action} that has a {@code null}
179 * value for {@code SELECTED_KEY} on a {@code JToggleButton}, the
180 * {@code JToggleButton} will not update it's selected state in
181 * any way. Similarly, any time the {@code JToggleButton}'s
182 * selected state changes it will only set the value back on
183 * the {@code Action} if the {@code Action} has a {@code non-null}
184 * value for {@code SELECTED_KEY}.
185 * <br>
186 * Components that honor this property keep their selected state
187 * in sync with this property. When the same {@code Action} is used
188 * with multiple components, all the components keep their selected
189 * state in sync with this property. Mutually exclusive
190 * buttons, such as {@code JToggleButton}s in a {@code ButtonGroup},
191 * force only one of the buttons to be selected. As such, do not
192 * use the same {@code Action} that defines a value for the
193 * {@code SELECTED_KEY} property with multiple mutually
194 * exclusive buttons.
195 * </table>
196 * <p>
197 * <code>JPopupMenu</code>, <code>JToolBar</code> and <code>JMenu</code>
198 * all provide convenience methods for creating a component and setting the
199 * <code>Action</code> on the corresponding component. Refer to each of
200 * these classes for more information.
201 * <p>
202 * <code>Action</code> uses <code>PropertyChangeListener</code> to
203 * inform listeners the <code>Action</code> has changed. The beans
204 * specification indicates that a <code>null</code> property name can
205 * be used to indicate multiple values have changed. By default Swing
206 * components that take an <code>Action</code> do not handle such a
207 * change. To indicate that Swing should treat <code>null</code>
208 * according to the beans specification set the system property
209 * <code>swing.actions.reconfigureOnNull</code> to the <code>String</code>
210 * value <code>true</code>.
211 *
212 * @version 1.42 05/05/07
213 * @author Georges Saab
214 * @see AbstractAction
215 */
216 public interface Action extends ActionListener {
217 /**
218 * Useful constants that can be used as the storage-retrieval key
219 * when setting or getting one of this object's properties (text
220 * or icon).
221 */
222 /**
223 * Not currently used.
224 */
225 public static final String DEFAULT = "Default";
226 /**
227 * The key used for storing the <code>String</code> name
228 * for the action, used for a menu or button.
229 */
230 public static final String NAME = "Name";
231 /**
232 * The key used for storing a short <code>String</code>
233 * description for the action, used for tooltip text.
234 */
235 public static final String SHORT_DESCRIPTION = "ShortDescription";
236 /**
237 * The key used for storing a longer <code>String</code>
238 * description for the action, could be used for context-sensitive help.
239 */
240 public static final String LONG_DESCRIPTION = "LongDescription";
241 /**
242 * The key used for storing a small <code>Icon</code>, such
243 * as <code>ImageIcon</code>. This is typically used with
244 * menus such as <code>JMenuItem</code>.
245 * <p>
246 * If the same <code>Action</code> is used with menus and buttons you'll
247 * typically specify both a <code>SMALL_ICON</code> and a
248 * <code>LARGE_ICON_KEY</code>. The menu will use the
249 * <code>SMALL_ICON</code> and the button will use the
250 * <code>LARGE_ICON_KEY</code>.
251 */
252 public static final String SMALL_ICON = "SmallIcon";
253
254 /**
255 * The key used to determine the command <code>String</code> for the
256 * <code>ActionEvent</code> that will be created when an
257 * <code>Action</code> is going to be notified as the result of
258 * residing in a <code>Keymap</code> associated with a
259 * <code>JComponent</code>.
260 */
261 public static final String ACTION_COMMAND_KEY = "ActionCommandKey";
262
263 /**
264 * The key used for storing a <code>KeyStroke</code> to be used as the
265 * accelerator for the action.
266 *
267 * @since 1.3
268 */
269 public static final String ACCELERATOR_KEY = "AcceleratorKey";
270
271 /**
272 * The key used for storing an <code>Integer</code> that corresponds to
273 * one of the <code>KeyEvent</code> key codes. The value is
274 * commonly used to specify a mnemonic. For example:
275 * <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)</code>
276 * sets the mnemonic of <code>myAction</code> to 'a'.
277 *
278 * @since 1.3
279 */
280 public static final String MNEMONIC_KEY = "MnemonicKey";
281
282 /**
283 * The key used for storing a <code>Boolean</code> that corresponds
284 * to the selected state. This is typically used only for components
285 * that have a meaningful selection state. For example,
286 * <code>JRadioButton</code> and <code>JCheckBox</code> make use of
287 * this but instances of <code>JMenu</code> don't.
288 * <p>
289 * This property differs from the others in that it is both read
290 * by the component and set by the component. For example,
291 * if an <code>Action</code> is attached to a <code>JCheckBox</code>
292 * the selected state of the <code>JCheckBox</code> will be set from
293 * that of the <code>Action</code>. If the user clicks on the
294 * <code>JCheckBox</code> the selected state of the <code>JCheckBox</code>
295 * <b>and</b> the <code>Action</code> will <b>both</b> be updated.
296 * <p>
297 * Note: the value of this field is prefixed with 'Swing' to
298 * avoid possible collisions with existing <code>Actions</code>.
299 *
300 * @since 1.6
301 */
302 public static final String SELECTED_KEY = "SwingSelectedKey";
303
304 /**
305 * The key used for storing an <code>Integer</code> that corresponds
306 * to the index in the text (identified by the <code>NAME</code>
307 * property) that the decoration for a mnemonic should be rendered at. If
308 * the value of this property is greater than or equal to the length of
309 * the text, it will treated as -1.
310 * <p>
311 * Note: the value of this field is prefixed with 'Swing' to
312 * avoid possible collisions with existing <code>Actions</code>.
313 *
314 * @see AbstractButton#setDisplayedMnemonicIndex
315 * @since 1.6
316 */
317 public static final String DISPLAYED_MNEMONIC_INDEX_KEY = "SwingDisplayedMnemonicIndexKey";
318
319 /**
320 * The key used for storing an <code>Icon</code>. This is typically
321 * used by buttons, such as <code>JButton</code> and
322 * <code>JToggleButton</code>.
323 * <p>
324 * If the same <code>Action</code> is used with menus and buttons you'll
325 * typically specify both a <code>SMALL_ICON</code> and a
326 * <code>LARGE_ICON_KEY</code>. The menu will use the
327 * <code>SMALL_ICON</code> and the button the <code>LARGE_ICON_KEY</code>.
328 * <p>
329 * Note: the value of this field is prefixed with 'Swing' to
330 * avoid possible collisions with existing <code>Actions</code>.
331 *
332 * @since 1.6
333 */
334 public static final String LARGE_ICON_KEY = "SwingLargeIconKey";
335
336 /**
337 * Gets one of this object's properties
338 * using the associated key.
339 * @see #putValue
340 */
341 public Object getValue(String key);
342
343 /**
344 * Sets one of this object's properties
345 * using the associated key. If the value has
346 * changed, a <code>PropertyChangeEvent</code> is sent
347 * to listeners.
348 *
349 * @param key a <code>String</code> containing the key
350 * @param value an <code>Object</code> value
351 */
352 public void putValue(String key, Object value);
353
354 /**
355 * Sets the enabled state of the <code>Action</code>. When enabled,
356 * any component associated with this object is active and
357 * able to fire this object's <code>actionPerformed</code> method.
358 * If the value has changed, a <code>PropertyChangeEvent</code> is sent
359 * to listeners.
360 *
361 * @param b true to enable this <code>Action</code>, false to disable it
362 */
363 public void setEnabled(boolean b);
364
365 /**
366 * Returns the enabled state of the <code>Action</code>. When enabled,
367 * any component associated with this object is active and
368 * able to fire this object's <code>actionPerformed</code> method.
369 *
370 * @return true if this <code>Action</code> is enabled
371 */
372 public boolean isEnabled();
373
374 /**
375 * Adds a <code>PropertyChange</code> listener. Containers and attached
376 * components use these methods to register interest in this
377 * <code>Action</code> object. When its enabled state or other property
378 * changes, the registered listeners are informed of the change.
379 *
380 * @param listener a <code>PropertyChangeListener</code> object
381 */
382 public void addPropertyChangeListener(
383 PropertyChangeListener listener);
384
385 /**
386 * Removes a <code>PropertyChange</code> listener.
387 *
388 * @param listener a <code>PropertyChangeListener</code> object
389 * @see #addPropertyChangeListener
390 */
391 public void removePropertyChangeListener(
392 PropertyChangeListener listener);
393
394 }
|