Path: blob/master/src/java.desktop/share/classes/javax/swing/Action.java
41153 views
/*1* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.swing;2526import java.awt.*;27import java.awt.event.*;28import java.beans.*;2930/**31* The <code>Action</code> interface provides a useful extension to the32* <code>ActionListener</code>33* interface in cases where the same functionality may be accessed by34* several controls.35* <p>36* In addition to the <code>actionPerformed</code> method defined by the37* <code>ActionListener</code> interface, this interface allows the38* application to define, in a single place:39* <ul>40* <li>One or more text strings that describe the function. These strings41* can be used, for example, to display the flyover text for a button42* or to set the text in a menu item.43* <li>One or more icons that depict the function. These icons can be used44* for the images in a menu control, or for composite entries in a more45* sophisticated user interface.46* <li>The enabled/disabled state of the functionality. Instead of having47* to separately disable the menu item and the toolbar button, the48* application can disable the function that implements this interface.49* All components which are registered as listeners for the state change50* then know to disable event generation for that item and to modify the51* display accordingly.52* </ul>53* <p>54* This interface can be added to an existing class or used to create an55* adapter (typically, by subclassing <code>AbstractAction</code>).56* The <code>Action</code> object57* can then be added to multiple <code>Action</code>-aware containers58* and connected to <code>Action</code>-capable59* components. The GUI controls can then be activated or60* deactivated all at once by invoking the <code>Action</code> object's61* <code>setEnabled</code> method.62* <p>63* Note that <code>Action</code> implementations tend to be more expensive64* in terms of storage than a typical <code>ActionListener</code>,65* which does not offer the benefits of centralized control of66* functionality and broadcast of property changes. For this reason,67* you should take care to only use <code>Action</code>s where their benefits68* are desired, and use simple <code>ActionListener</code>s elsewhere.69* <br>70*71* <h2><a id="buttonActions"></a>Swing Components Supporting <code>Action</code></h2>72* <p>73* Many of Swing's components have an <code>Action</code> property. When74* an <code>Action</code> is set on a component, the following things75* happen:76* <ul>77* <li>The <code>Action</code> is added as an <code>ActionListener</code> to78* the component.79* <li>The component configures some of its properties to match the80* <code>Action</code>.81* <li>The component installs a <code>PropertyChangeListener</code> on the82* <code>Action</code> so that the component can change its properties83* to reflect changes in the <code>Action</code>'s properties.84* </ul>85* <p>86* The following table describes the properties used by87* <code>Swing</code> components that support <code>Actions</code>.88* In the table, <em>button</em> refers to any89* <code>AbstractButton</code> subclass, which includes not only90* <code>JButton</code> but also classes such as91* <code>JMenuItem</code>. Unless otherwise stated, a92* <code>null</code> property value in an <code>Action</code> (or a93* <code>Action</code> that is <code>null</code>) results in the94* button's corresponding property being set to <code>null</code>.95*96* <table class="striped">97* <caption>Supported Action properties</caption>98* <thead>99* <tr>100* <th scope="col">Component Property101* <th scope="col">Components102* <th scope="col">Action Key103* <th scope="col">Notes104* </thead>105* <tbody>106* <tr>107* <th scope="row">{@code enabled}108* <td>All109* <td>The {@code isEnabled} method110* <td> 111* <tr>112* <th scope="row">{@code toolTipText}113* <td>All114* <td>{@code SHORT_DESCRIPTION}115* <td> 116* <tr>117* <th scope="row">{@code actionCommand}118* <td>All119* <td>{@code ACTION_COMMAND_KEY}120* <td> 121* <tr>122* <th scope="row">{@code mnemonic}123* <td>All buttons124* <td>{@code MNEMONIC_KEY}125* <td>A {@code null} value or {@code Action} results in the button's126* {@code mnemonic} property being set to {@code '\0'}.127* <tr>128* <th scope="row">{@code text}129* <td>All buttons130* <td>{@code NAME}131* <td>If you do not want the text of the button to mirror that of the132* {@code Action}, set the property {@code hideActionText} to {@code true}.133* If {@code hideActionText} is {@code true}, setting the {@code Action}134* changes the text of the button to {@code null} and any changes to135* {@code NAME} are ignored. {@code hideActionText} is useful for tool bar136* buttons that typically only show an {@code Icon}.137* {@code JToolBar.add(Action)} sets the property to {@code true} if the138* {@code Action} has a non-{@code null} value for {@code LARGE_ICON_KEY} or139* {@code SMALL_ICON}.140* <tr>141* <th scope="row">{@code displayedMnemonicIndex}142* <td>All buttons143* <td>{@code DISPLAYED_MNEMONIC_INDEX_KEY}144* <td>If the value of {@code DISPLAYED_MNEMONIC_INDEX_KEY} is beyond the145* bounds of the text, it is ignored. When {@code setAction} is called, if146* the value from the {@code Action} is {@code null}, the displayed mnemonic147* index is not updated. In any subsequent changes to148* {@code DISPLAYED_MNEMONIC_INDEX_KEY}, {@code null} is treated as -1.149* <tr>150* <th scope="row">{@code icon}151* <td>All buttons except of {@code JCheckBox}, {@code JToggleButton} and152* {@code JRadioButton}.153* <td>either {@code LARGE_ICON_KEY} or {@code SMALL_ICON}154* <td>The {@code JMenuItem} subclasses only use {@code SMALL_ICON}. All155* other buttons will use {@code LARGE_ICON_KEY}; if the value is156* {@code null} they use {@code SMALL_ICON}.157* <tr>158* <th scope="row">{@code accelerator}159* <td>All {@code JMenuItem} subclasses, with the exception of {@code JMenu}.160* <td>{@code ACCELERATOR_KEY}161* <td> 162* <tr>163* <th scope="row">{@code selected}164* <td>{@code JToggleButton}, {@code JCheckBox}, {@code JRadioButton},165* {@code JCheckBoxMenuItem} and {@code JRadioButtonMenuItem}166* <td>{@code SELECTED_KEY}167* <td>Components that honor this property only use the value if it is168* {@code non-null}. For example, if you set an {@code Action} that has a169* {@code null} value for {@code SELECTED_KEY} on a {@code JToggleButton},170* the {@code JToggleButton} will not update it's selected state in any way.171* Similarly, any time the {@code JToggleButton}'s selected state changes it172* will only set the value back on the {@code Action} if the {@code Action}173* has a {@code non-null} value for {@code SELECTED_KEY}.174* <br>175* Components that honor this property keep their selected state in sync with176* this property. When the same {@code Action} is used with multiple177* components, all the components keep their selected state in sync with this178* property. Mutually exclusive buttons, such as {@code JToggleButton}s in a179* {@code ButtonGroup}, force only one of the buttons to be selected. As180* such, do not use the same {@code Action} that defines a value for the181* {@code SELECTED_KEY} property with multiple mutually exclusive buttons.182* </tbody>183* </table>184* <p>185* <code>JPopupMenu</code>, <code>JToolBar</code> and <code>JMenu</code>186* all provide convenience methods for creating a component and setting the187* <code>Action</code> on the corresponding component. Refer to each of188* these classes for more information.189* <p>190* <code>Action</code> uses <code>PropertyChangeListener</code> to191* inform listeners the <code>Action</code> has changed. The beans192* specification indicates that a <code>null</code> property name can193* be used to indicate multiple values have changed. By default Swing194* components that take an <code>Action</code> do not handle such a195* change. To indicate that Swing should treat <code>null</code>196* according to the beans specification set the system property197* <code>swing.actions.reconfigureOnNull</code> to the <code>String</code>198* value <code>true</code>.199*200* @author Georges Saab201* @see AbstractAction202* @since 1.2203*/204public interface Action extends ActionListener {205/**206* Useful constants that can be used as the storage-retrieval key207* when setting or getting one of this object's properties (text208* or icon).209*/210/**211* Not currently used.212*/213public static final String DEFAULT = "Default";214/**215* The key used for storing the <code>String</code> name216* for the action, used for a menu or button.217*/218public static final String NAME = "Name";219/**220* The key used for storing a short <code>String</code>221* description for the action, used for tooltip text.222*/223public static final String SHORT_DESCRIPTION = "ShortDescription";224/**225* The key used for storing a longer <code>String</code>226* description for the action, could be used for context-sensitive help.227*/228public static final String LONG_DESCRIPTION = "LongDescription";229/**230* The key used for storing a small <code>Icon</code>, such231* as <code>ImageIcon</code>. This is typically used with232* menus such as <code>JMenuItem</code>.233* <p>234* If the same <code>Action</code> is used with menus and buttons you'll235* typically specify both a <code>SMALL_ICON</code> and a236* <code>LARGE_ICON_KEY</code>. The menu will use the237* <code>SMALL_ICON</code> and the button will use the238* <code>LARGE_ICON_KEY</code>.239*/240public static final String SMALL_ICON = "SmallIcon";241242/**243* The key used to determine the command <code>String</code> for the244* <code>ActionEvent</code> that will be created when an245* <code>Action</code> is going to be notified as the result of246* residing in a <code>Keymap</code> associated with a247* <code>JComponent</code>.248*/249public static final String ACTION_COMMAND_KEY = "ActionCommandKey";250251/**252* The key used for storing a <code>KeyStroke</code> to be used as the253* accelerator for the action.254*255* @since 1.3256*/257public static final String ACCELERATOR_KEY="AcceleratorKey";258259/**260* The key used for storing an <code>Integer</code> that corresponds to261* one of the <code>KeyEvent</code> key codes. The value is262* commonly used to specify a mnemonic. For example:263* <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)</code>264* sets the mnemonic of <code>myAction</code> to 'a', while265* <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('\u0444'))</code>266* sets the mnemonic of <code>myAction</code> to Cyrillic letter "Ef".267*268* @since 1.3269*/270public static final String MNEMONIC_KEY="MnemonicKey";271272/**273* The key used for storing a <code>Boolean</code> that corresponds274* to the selected state. This is typically used only for components275* that have a meaningful selection state. For example,276* <code>JRadioButton</code> and <code>JCheckBox</code> make use of277* this but instances of <code>JMenu</code> don't.278* <p>279* This property differs from the others in that it is both read280* by the component and set by the component. For example,281* if an <code>Action</code> is attached to a <code>JCheckBox</code>282* the selected state of the <code>JCheckBox</code> will be set from283* that of the <code>Action</code>. If the user clicks on the284* <code>JCheckBox</code> the selected state of the <code>JCheckBox</code>285* <b>and</b> the <code>Action</code> will <b>both</b> be updated.286* <p>287* Note: the value of this field is prefixed with 'Swing' to288* avoid possible collisions with existing <code>Actions</code>.289*290* @since 1.6291*/292public static final String SELECTED_KEY = "SwingSelectedKey";293294/**295* The key used for storing an <code>Integer</code> that corresponds296* to the index in the text (identified by the <code>NAME</code>297* property) that the decoration for a mnemonic should be rendered at. If298* the value of this property is greater than or equal to the length of299* the text, it will treated as -1.300* <p>301* Note: the value of this field is prefixed with 'Swing' to302* avoid possible collisions with existing <code>Actions</code>.303*304* @see AbstractButton#setDisplayedMnemonicIndex305* @since 1.6306*/307public static final String DISPLAYED_MNEMONIC_INDEX_KEY =308"SwingDisplayedMnemonicIndexKey";309310/**311* The key used for storing an <code>Icon</code>. This is typically312* used by buttons, such as <code>JButton</code> and313* <code>JToggleButton</code>.314* <p>315* If the same <code>Action</code> is used with menus and buttons you'll316* typically specify both a <code>SMALL_ICON</code> and a317* <code>LARGE_ICON_KEY</code>. The menu will use the318* <code>SMALL_ICON</code> and the button the <code>LARGE_ICON_KEY</code>.319* <p>320* Note: the value of this field is prefixed with 'Swing' to321* avoid possible collisions with existing <code>Actions</code>.322*323* @since 1.6324*/325public static final String LARGE_ICON_KEY = "SwingLargeIconKey";326327/**328* Gets one of this object's properties329* using the associated key.330*331* @param key a {@code String} containing the key332* @return the {@code Object} value333* @see #putValue334*/335public Object getValue(String key);336/**337* Sets one of this object's properties338* using the associated key. If the value has339* changed, a <code>PropertyChangeEvent</code> is sent340* to listeners.341*342* @param key a <code>String</code> containing the key343* @param value an <code>Object</code> value344*/345public void putValue(String key, Object value);346347/**348* Sets the enabled state of the {@code Action}. When enabled,349* any component associated with this object is active and350* able to fire this object's {@code actionPerformed} method.351* If the value has changed, a {@code PropertyChangeEvent} is sent352* to listeners.353*354* @param b true to enable this {@code Action}, false to disable it355* @see #accept356*/357public void setEnabled(boolean b);358/**359* Returns the enabled state of the {@code Action}. When enabled,360* any component associated with this object is active and361* able to fire this object's {@code actionPerformed} method.362*363* @return true if this {@code Action} is enabled364* @see #accept365*/366public boolean isEnabled();367368/**369* Determines whether the action should be performed with the specified370* sender object. The {@code sender} can be {@code null}.371* The method must return false if the action is disabled.372*373* @param sender the object to check, can be null374* @return {@code true} if the action should be performed with the sender375* object, must be false if the action is disabled.376* @see #isEnabled377* @see #setEnabled378*/379default boolean accept(Object sender) {380return isEnabled();381}382383/**384* Adds a <code>PropertyChange</code> listener. Containers and attached385* components use these methods to register interest in this386* <code>Action</code> object. When its enabled state or other property387* changes, the registered listeners are informed of the change.388*389* @param listener a <code>PropertyChangeListener</code> object390*/391public void addPropertyChangeListener(PropertyChangeListener listener);392/**393* Removes a <code>PropertyChange</code> listener.394*395* @param listener a <code>PropertyChangeListener</code> object396* @see #addPropertyChangeListener397*/398public void removePropertyChangeListener(PropertyChangeListener listener);399400}401402403