Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/Action.java
38829 views
/*1* Copyright (c) 1997, 2013, 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* <h3><a name="buttonActions"></a>Swing Components Supporting <code>Action</code></h3>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 border="1" cellpadding="1" cellspacing="0"97* summary="Supported Action properties">98* <tr valign="top" align="left">99* <th style="background-color:#CCCCFF" align="left">Component Property100* <th style="background-color:#CCCCFF" align="left">Components101* <th style="background-color:#CCCCFF" align="left">Action Key102* <th style="background-color:#CCCCFF" align="left">Notes103* <tr valign="top" align="left">104* <td><b><code>enabled</code></b>105* <td>All106* <td>The <code>isEnabled</code> method107* <td> 108* <tr valign="top" align="left">109* <td><b><code>toolTipText</code></b>110* <td>All111* <td><code>SHORT_DESCRIPTION</code>112* <td> 113* <tr valign="top" align="left">114* <td><b><code>actionCommand</code></b>115* <td>All116* <td><code>ACTION_COMMAND_KEY</code>117* <td> 118* <tr valign="top" align="left">119* <td><b><code>mnemonic</code></b>120* <td>All buttons121* <td><code>MNEMONIC_KEY</code>122* <td>A <code>null</code> value or <code>Action</code> results in the123* button's <code>mnemonic</code> property being set to124* <code>'\0'</code>.125* <tr valign="top" align="left">126* <td><b><code>text</code></b>127* <td>All buttons128* <td><code>NAME</code>129* <td>If you do not want the text of the button to mirror that130* of the <code>Action</code>, set the property131* <code>hideActionText</code> to <code>true</code>. If132* <code>hideActionText</code> is <code>true</code>, setting the133* <code>Action</code> changes the text of the button to134* <code>null</code> and any changes to <code>NAME</code>135* are ignored. <code>hideActionText</code> is useful for136* tool bar buttons that typically only show an <code>Icon</code>.137* <code>JToolBar.add(Action)</code> sets the property to138* <code>true</code> if the <code>Action</code> has a139* non-<code>null</code> value for <code>LARGE_ICON_KEY</code> or140* <code>SMALL_ICON</code>.141* <tr valign="top" align="left">142* <td><b><code>displayedMnemonicIndex</code></b>143* <td>All buttons144* <td><code>DISPLAYED_MNEMONIC_INDEX_KEY</code>145* <td>If the value of <code>DISPLAYED_MNEMONIC_INDEX_KEY</code> is146* beyond the bounds of the text, it is ignored. When147* <code>setAction</code> is called, if the value from the148* <code>Action</code> is <code>null</code>, the displayed149* mnemonic index is not updated. In any subsequent changes to150* <code>DISPLAYED_MNEMONIC_INDEX_KEY</code>, <code>null</code>151* is treated as -1.152* <tr valign="top" align="left">153* <td><b><code>icon</code></b>154* <td>All buttons except of <code>JCheckBox</code>,155* <code>JToggleButton</code> and <code>JRadioButton</code>.156* <td>either <code>LARGE_ICON_KEY</code> or157* <code>SMALL_ICON</code>158* <td>The <code>JMenuItem</code> subclasses only use159* <code>SMALL_ICON</code>. All other buttons will use160* <code>LARGE_ICON_KEY</code>; if the value is <code>null</code> they161* use <code>SMALL_ICON</code>.162* <tr valign="top" align="left">163* <td><b><code>accelerator</code></b>164* <td>All <code>JMenuItem</code> subclasses, with the exception of165* <code>JMenu</code>.166* <td><code>ACCELERATOR_KEY</code>167* <td> 168* <tr valign="top" align="left">169* <td><b><code>selected</code></b>170* <td><code>JToggleButton</code>, <code>JCheckBox</code>,171* <code>JRadioButton</code>, <code>JCheckBoxMenuItem</code> and172* <code>JRadioButtonMenuItem</code>173* <td><code>SELECTED_KEY</code>174* <td>Components that honor this property only use175* the value if it is {@code non-null}. For example, if176* you set an {@code Action} that has a {@code null}177* value for {@code SELECTED_KEY} on a {@code JToggleButton}, the178* {@code JToggleButton} will not update it's selected state in179* any way. Similarly, any time the {@code JToggleButton}'s180* selected state changes it will only set the value back on181* the {@code Action} if the {@code Action} has a {@code non-null}182* value for {@code SELECTED_KEY}.183* <br>184* Components that honor this property keep their selected state185* in sync with this property. When the same {@code Action} is used186* with multiple components, all the components keep their selected187* state in sync with this property. Mutually exclusive188* buttons, such as {@code JToggleButton}s in a {@code ButtonGroup},189* force only one of the buttons to be selected. As such, do not190* use the same {@code Action} that defines a value for the191* {@code SELECTED_KEY} property with multiple mutually192* exclusive buttons.193* </table>194* <p>195* <code>JPopupMenu</code>, <code>JToolBar</code> and <code>JMenu</code>196* all provide convenience methods for creating a component and setting the197* <code>Action</code> on the corresponding component. Refer to each of198* these classes for more information.199* <p>200* <code>Action</code> uses <code>PropertyChangeListener</code> to201* inform listeners the <code>Action</code> has changed. The beans202* specification indicates that a <code>null</code> property name can203* be used to indicate multiple values have changed. By default Swing204* components that take an <code>Action</code> do not handle such a205* change. To indicate that Swing should treat <code>null</code>206* according to the beans specification set the system property207* <code>swing.actions.reconfigureOnNull</code> to the <code>String</code>208* value <code>true</code>.209*210* @author Georges Saab211* @see AbstractAction212*/213public interface Action extends ActionListener {214/**215* Useful constants that can be used as the storage-retrieval key216* when setting or getting one of this object's properties (text217* or icon).218*/219/**220* Not currently used.221*/222public static final String DEFAULT = "Default";223/**224* The key used for storing the <code>String</code> name225* for the action, used for a menu or button.226*/227public static final String NAME = "Name";228/**229* The key used for storing a short <code>String</code>230* description for the action, used for tooltip text.231*/232public static final String SHORT_DESCRIPTION = "ShortDescription";233/**234* The key used for storing a longer <code>String</code>235* description for the action, could be used for context-sensitive help.236*/237public static final String LONG_DESCRIPTION = "LongDescription";238/**239* The key used for storing a small <code>Icon</code>, such240* as <code>ImageIcon</code>. This is typically used with241* menus such as <code>JMenuItem</code>.242* <p>243* If the same <code>Action</code> is used with menus and buttons you'll244* typically specify both a <code>SMALL_ICON</code> and a245* <code>LARGE_ICON_KEY</code>. The menu will use the246* <code>SMALL_ICON</code> and the button will use the247* <code>LARGE_ICON_KEY</code>.248*/249public static final String SMALL_ICON = "SmallIcon";250251/**252* The key used to determine the command <code>String</code> for the253* <code>ActionEvent</code> that will be created when an254* <code>Action</code> is going to be notified as the result of255* residing in a <code>Keymap</code> associated with a256* <code>JComponent</code>.257*/258public static final String ACTION_COMMAND_KEY = "ActionCommandKey";259260/**261* The key used for storing a <code>KeyStroke</code> to be used as the262* accelerator for the action.263*264* @since 1.3265*/266public static final String ACCELERATOR_KEY="AcceleratorKey";267268/**269* The key used for storing an <code>Integer</code> that corresponds to270* one of the <code>KeyEvent</code> key codes. The value is271* commonly used to specify a mnemonic. For example:272* <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)</code>273* sets the mnemonic of <code>myAction</code> to 'a', while274* <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('\u0444'))</code>275* sets the mnemonic of <code>myAction</code> to Cyrillic letter "Ef".276*277* @since 1.3278*/279public static final String MNEMONIC_KEY="MnemonicKey";280281/**282* The key used for storing a <code>Boolean</code> that corresponds283* to the selected state. This is typically used only for components284* that have a meaningful selection state. For example,285* <code>JRadioButton</code> and <code>JCheckBox</code> make use of286* this but instances of <code>JMenu</code> don't.287* <p>288* This property differs from the others in that it is both read289* by the component and set by the component. For example,290* if an <code>Action</code> is attached to a <code>JCheckBox</code>291* the selected state of the <code>JCheckBox</code> will be set from292* that of the <code>Action</code>. If the user clicks on the293* <code>JCheckBox</code> the selected state of the <code>JCheckBox</code>294* <b>and</b> the <code>Action</code> will <b>both</b> be updated.295* <p>296* Note: the value of this field is prefixed with 'Swing' to297* avoid possible collisions with existing <code>Actions</code>.298*299* @since 1.6300*/301public static final String SELECTED_KEY = "SwingSelectedKey";302303/**304* The key used for storing an <code>Integer</code> that corresponds305* to the index in the text (identified by the <code>NAME</code>306* property) that the decoration for a mnemonic should be rendered at. If307* the value of this property is greater than or equal to the length of308* the text, it will treated as -1.309* <p>310* Note: the value of this field is prefixed with 'Swing' to311* avoid possible collisions with existing <code>Actions</code>.312*313* @see AbstractButton#setDisplayedMnemonicIndex314* @since 1.6315*/316public static final String DISPLAYED_MNEMONIC_INDEX_KEY =317"SwingDisplayedMnemonicIndexKey";318319/**320* The key used for storing an <code>Icon</code>. This is typically321* used by buttons, such as <code>JButton</code> and322* <code>JToggleButton</code>.323* <p>324* If the same <code>Action</code> is used with menus and buttons you'll325* typically specify both a <code>SMALL_ICON</code> and a326* <code>LARGE_ICON_KEY</code>. The menu will use the327* <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' to330* avoid possible collisions with existing <code>Actions</code>.331*332* @since 1.6333*/334public static final String LARGE_ICON_KEY = "SwingLargeIconKey";335336/**337* Gets one of this object's properties338* using the associated key.339* @see #putValue340*/341public Object getValue(String key);342/**343* Sets one of this object's properties344* using the associated key. If the value has345* changed, a <code>PropertyChangeEvent</code> is sent346* to listeners.347*348* @param key a <code>String</code> containing the key349* @param value an <code>Object</code> value350*/351public void putValue(String key, Object value);352353/**354* Sets the enabled state of the <code>Action</code>. When enabled,355* any component associated with this object is active and356* able to fire this object's <code>actionPerformed</code> method.357* If the value has changed, a <code>PropertyChangeEvent</code> is sent358* to listeners.359*360* @param b true to enable this <code>Action</code>, false to disable it361*/362public void setEnabled(boolean b);363/**364* Returns the enabled state of the <code>Action</code>. When enabled,365* any component associated with this object is active and366* able to fire this object's <code>actionPerformed</code> method.367*368* @return true if this <code>Action</code> is enabled369*/370public boolean isEnabled();371372/**373* Adds a <code>PropertyChange</code> listener. Containers and attached374* components use these methods to register interest in this375* <code>Action</code> object. When its enabled state or other property376* changes, the registered listeners are informed of the change.377*378* @param listener a <code>PropertyChangeListener</code> object379*/380public void addPropertyChangeListener(PropertyChangeListener listener);381/**382* Removes a <code>PropertyChange</code> listener.383*384* @param listener a <code>PropertyChangeListener</code> object385* @see #addPropertyChangeListener386*/387public void removePropertyChangeListener(PropertyChangeListener listener);388389}390391392