1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.beans.*;
  30 
  31 /**
  32  * The <code>Action</code> interface provides a useful extension to the
  33  * <code>ActionListener</code>
  34  * interface in cases where the same functionality may be accessed by
  35  * several controls.
  36  * <p>
  37  * In addition to the <code>actionPerformed</code> method defined by the
  38  * <code>ActionListener</code> interface, this interface allows the
  39  * application to define, in a single place:
  40  * <ul>
  41  * <li>One or more text strings that describe the function. These strings
  42  *     can be used, for example, to display the flyover text for a button
  43  *     or to set the text in a menu item.
  44  * <li>One or more icons that depict the function. These icons can be used
  45  *     for the images in a menu control, or for composite entries in a more
  46  *     sophisticated user interface.
  47  * <li>The enabled/disabled state of the functionality. Instead of having
  48  *     to separately disable the menu item and the toolbar button, the
  49  *     application can disable the function that implements this interface.
  50  *     All components which are registered as listeners for the state change
  51  *     then know to disable event generation for that item and to modify the
  52  *     display accordingly.
  53  * </ul>
  54  * <p>
  55  * This interface can be added to an existing class or used to create an
  56  * adapter (typically, by subclassing <code>AbstractAction</code>).
  57  * The <code>Action</code> object
  58  * can then be added to multiple <code>Action</code>-aware containers
  59  * and connected to <code>Action</code>-capable
  60  * components. The GUI controls can then be activated or
  61  * deactivated all at once by invoking the <code>Action</code> object's
  62  * <code>setEnabled</code> method.
  63  * <p>
  64  * Note that <code>Action</code> implementations tend to be more expensive
  65  * in terms of storage than a typical <code>ActionListener</code>,
  66  * which does not offer the benefits of centralized control of
  67  * functionality and broadcast of property changes.  For this reason,
  68  * you should take care to only use <code>Action</code>s where their benefits
  69  * are desired, and use simple <code>ActionListener</code>s elsewhere.
  70  * <br>
  71  *
  72  * <h3><a id="buttonActions"></a>Swing Components Supporting <code>Action</code></h3>
  73  * <p>
  74  * Many of Swing's components have an <code>Action</code> property.  When
  75  * an <code>Action</code> is set on a component, the following things
  76  * happen:
  77  * <ul>
  78  * <li>The <code>Action</code> is added as an <code>ActionListener</code> to
  79  *     the component.
  80  * <li>The component configures some of its properties to match the
  81  *      <code>Action</code>.
  82  * <li>The component installs a <code>PropertyChangeListener</code> on the
  83  *     <code>Action</code> so that the component can change its properties
  84  *     to reflect changes in the <code>Action</code>'s properties.
  85  * </ul>
  86  * <p>
  87  * The following table describes the properties used by
  88  * <code>Swing</code> components that support <code>Actions</code>.
  89  * In the table, <em>button</em> refers to any
  90  * <code>AbstractButton</code> subclass, which includes not only
  91  * <code>JButton</code> but also classes such as
  92  * <code>JMenuItem</code>. Unless otherwise stated, a
  93  * <code>null</code> property value in an <code>Action</code> (or a
  94  * <code>Action</code> that is <code>null</code>) results in the
  95  * button's corresponding property being set to <code>null</code>.
  96  *
  97  * <table class="striped">
  98  * <caption>Supported Action properties</caption>
  99  * <thead>
 100  *  <tr>
 101  *    <th scope="col">Component Property
 102  *    <th scope="col">Components
 103  *    <th scope="col">Action Key
 104  *    <th scope="col">Notes
 105  * </thead>
 106  * <tbody>
 107  *  <tr>
 108  *    <th scope="row"><b>{@code enabled}</b>
 109  *    <td>All
 110  *    <td>The {@code isEnabled} method
 111  *    <td>&nbsp;
 112  *  <tr>
 113  *    <th scope="row"><b>{@code toolTipText}</b>
 114  *    <td>All
 115  *    <td>{@code SHORT_DESCRIPTION}
 116  *    <td>&nbsp;
 117  *  <tr>
 118  *    <th scope="row"><b>{@code actionCommand}</b>
 119  *    <td>All
 120  *    <td>{@code ACTION_COMMAND_KEY}
 121  *    <td>&nbsp;
 122  *  <tr>
 123  *    <th scope="row"><b>{@code mnemonic}</b>
 124  *    <td>All buttons
 125  *    <td>{@code MNEMONIC_KEY}
 126  *    <td>A {@code null} value or {@code Action} results in the button's
 127  *    {@code mnemonic} property being set to {@code '\0'}.
 128  *  <tr>
 129  *    <th scope="row"><b>{@code text}</b>
 130  *    <td>All buttons
 131  *    <td>{@code NAME}
 132  *    <td>If you do not want the text of the button to mirror that of the
 133  *    {@code Action}, set the property {@code hideActionText} to {@code true}.
 134  *    If {@code hideActionText} is {@code true}, setting the {@code Action}
 135  *    changes the text of the button to {@code null} and any changes to
 136  *    {@code NAME} are ignored. {@code hideActionText} is useful for tool bar
 137  *    buttons that typically only show an {@code Icon}.
 138  *    {@code JToolBar.add(Action)} sets the property to {@code true} if the
 139  *    {@code Action} has a non-{@code null} value for {@code LARGE_ICON_KEY} or
 140  *    {@code SMALL_ICON}.
 141  *  <tr>
 142  *    <th scope="row"><b>{@code displayedMnemonicIndex}</b>
 143  *    <td>All buttons
 144  *    <td>{@code DISPLAYED_MNEMONIC_INDEX_KEY}
 145  *    <td>If the value of {@code DISPLAYED_MNEMONIC_INDEX_KEY} is beyond the
 146  *    bounds of the text, it is ignored. When {@code setAction} is called, if
 147  *    the value from the {@code Action} is {@code null}, the displayed mnemonic
 148  *    index is not updated. In any subsequent changes to
 149  *    {@code DISPLAYED_MNEMONIC_INDEX_KEY}, {@code null} is treated as -1.
 150  *  <tr>
 151  *    <th scope="row"><b>{@code icon}</b>
 152  *    <td>All buttons except of {@code JCheckBox}, {@code JToggleButton} and
 153  *    {@code JRadioButton}.
 154  *    <td>either {@code LARGE_ICON_KEY} or {@code SMALL_ICON}
 155  *    <td>The {@code JMenuItem} subclasses only use {@code SMALL_ICON}. All
 156  *    other buttons will use {@code LARGE_ICON_KEY}; if the value is
 157  *    {@code null} they use {@code SMALL_ICON}.
 158  *  <tr>
 159  *    <th scope="row"><b>{@code accelerator}</b>
 160  *    <td>All {@code JMenuItem} subclasses, with the exception of {@code JMenu}.
 161  *    <td>{@code ACCELERATOR_KEY}
 162  *    <td>&nbsp;
 163  *  <tr>
 164  *    <th scope="row"><b>{@code selected}</b>
 165  *    <td>{@code JToggleButton}, {@code JCheckBox}, {@code JRadioButton},
 166  *    {@code JCheckBoxMenuItem} and {@code JRadioButtonMenuItem}
 167  *    <td>{@code SELECTED_KEY}
 168  *    <td>Components that honor this property only use the value if it is
 169  *    {@code non-null}. For example, if you set an {@code Action} that has a
 170  *    {@code null} value for {@code SELECTED_KEY} on a {@code JToggleButton},
 171  *    the {@code JToggleButton} will not update it's selected state in any way.
 172  *    Similarly, any time the {@code JToggleButton}'s selected state changes it
 173  *    will only set the value back on the {@code Action} if the {@code Action}
 174  *    has a {@code non-null} value for {@code SELECTED_KEY}.
 175  *    <br>
 176  *    Components that honor this property keep their selected state in sync with
 177  *    this property. When the same {@code Action} is used with multiple
 178  *    components, all the components keep their selected state in sync with this
 179  *    property. Mutually exclusive buttons, such as {@code JToggleButton}s in a
 180  *    {@code ButtonGroup}, force only one of the buttons to be selected. As
 181  *    such, do not use the same {@code Action} that defines a value for the
 182  *    {@code SELECTED_KEY} property with multiple mutually exclusive buttons.
 183  * </tbody>
 184  * </table>
 185  * <p>
 186  * <code>JPopupMenu</code>, <code>JToolBar</code> and <code>JMenu</code>
 187  * all provide convenience methods for creating a component and setting the
 188  * <code>Action</code> on the corresponding component.  Refer to each of
 189  * these classes for more information.
 190  * <p>
 191  * <code>Action</code> uses <code>PropertyChangeListener</code> to
 192  * inform listeners the <code>Action</code> has changed.  The beans
 193  * specification indicates that a <code>null</code> property name can
 194  * be used to indicate multiple values have changed.  By default Swing
 195  * components that take an <code>Action</code> do not handle such a
 196  * change.  To indicate that Swing should treat <code>null</code>
 197  * according to the beans specification set the system property
 198  * <code>swing.actions.reconfigureOnNull</code> to the <code>String</code>
 199  * value <code>true</code>.
 200  *
 201  * @author Georges Saab
 202  * @see AbstractAction
 203  * @since 1.2
 204  */
 205 public interface Action extends ActionListener {
 206     /**
 207      * Useful constants that can be used as the storage-retrieval key
 208      * when setting or getting one of this object's properties (text
 209      * or icon).
 210      */
 211     /**
 212      * Not currently used.
 213      */
 214     public static final String DEFAULT = "Default";
 215     /**
 216      * The key used for storing the <code>String</code> name
 217      * for the action, used for a menu or button.
 218      */
 219     public static final String NAME = "Name";
 220     /**
 221      * The key used for storing a short <code>String</code>
 222      * description for the action, used for tooltip text.
 223      */
 224     public static final String SHORT_DESCRIPTION = "ShortDescription";
 225     /**
 226      * The key used for storing a longer <code>String</code>
 227      * description for the action, could be used for context-sensitive help.
 228      */
 229     public static final String LONG_DESCRIPTION = "LongDescription";
 230     /**
 231      * The key used for storing a small <code>Icon</code>, such
 232      * as <code>ImageIcon</code>.  This is typically used with
 233      * menus such as <code>JMenuItem</code>.
 234      * <p>
 235      * If the same <code>Action</code> is used with menus and buttons you'll
 236      * typically specify both a <code>SMALL_ICON</code> and a
 237      * <code>LARGE_ICON_KEY</code>.  The menu will use the
 238      * <code>SMALL_ICON</code> and the button will use the
 239      * <code>LARGE_ICON_KEY</code>.
 240      */
 241     public static final String SMALL_ICON = "SmallIcon";
 242 
 243     /**
 244      * The key used to determine the command <code>String</code> for the
 245      * <code>ActionEvent</code> that will be created when an
 246      * <code>Action</code> is going to be notified as the result of
 247      * residing in a <code>Keymap</code> associated with a
 248      * <code>JComponent</code>.
 249      */
 250     public static final String ACTION_COMMAND_KEY = "ActionCommandKey";
 251 
 252     /**
 253      * The key used for storing a <code>KeyStroke</code> to be used as the
 254      * accelerator for the action.
 255      *
 256      * @since 1.3
 257      */
 258     public static final String ACCELERATOR_KEY="AcceleratorKey";
 259 
 260     /**
 261      * The key used for storing an <code>Integer</code> that corresponds to
 262      * one of the <code>KeyEvent</code> key codes.  The value is
 263      * commonly used to specify a mnemonic.  For example:
 264      * <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)</code>
 265      * sets the mnemonic of <code>myAction</code> to 'a', while
 266      * <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('\u0444'))</code>
 267      * sets the mnemonic of <code>myAction</code> to Cyrillic letter "Ef".
 268      *
 269      * @since 1.3
 270      */
 271     public static final String MNEMONIC_KEY="MnemonicKey";
 272 
 273     /**
 274      * The key used for storing a <code>Boolean</code> that corresponds
 275      * to the selected state.  This is typically used only for components
 276      * that have a meaningful selection state.  For example,
 277      * <code>JRadioButton</code> and <code>JCheckBox</code> make use of
 278      * this but instances of <code>JMenu</code> don't.
 279      * <p>
 280      * This property differs from the others in that it is both read
 281      * by the component and set by the component.  For example,
 282      * if an <code>Action</code> is attached to a <code>JCheckBox</code>
 283      * the selected state of the <code>JCheckBox</code> will be set from
 284      * that of the <code>Action</code>.  If the user clicks on the
 285      * <code>JCheckBox</code> the selected state of the <code>JCheckBox</code>
 286      * <b>and</b> the <code>Action</code> will <b>both</b> be updated.
 287      * <p>
 288      * Note: the value of this field is prefixed with 'Swing' to
 289      * avoid possible collisions with existing <code>Actions</code>.
 290      *
 291      * @since 1.6
 292      */
 293     public static final String SELECTED_KEY = "SwingSelectedKey";
 294 
 295     /**
 296      * The key used for storing an <code>Integer</code> that corresponds
 297      * to the index in the text (identified by the <code>NAME</code>
 298      * property) that the decoration for a mnemonic should be rendered at.  If
 299      * the value of this property is greater than or equal to the length of
 300      * the text, it will treated as -1.
 301      * <p>
 302      * Note: the value of this field is prefixed with 'Swing' to
 303      * avoid possible collisions with existing <code>Actions</code>.
 304      *
 305      * @see AbstractButton#setDisplayedMnemonicIndex
 306      * @since 1.6
 307      */
 308     public static final String DISPLAYED_MNEMONIC_INDEX_KEY =
 309                                  "SwingDisplayedMnemonicIndexKey";
 310 
 311     /**
 312      * The key used for storing an <code>Icon</code>.  This is typically
 313      * used by buttons, such as <code>JButton</code> and
 314      * <code>JToggleButton</code>.
 315      * <p>
 316      * If the same <code>Action</code> is used with menus and buttons you'll
 317      * typically specify both a <code>SMALL_ICON</code> and a
 318      * <code>LARGE_ICON_KEY</code>.  The menu will use the
 319      * <code>SMALL_ICON</code> and the button the <code>LARGE_ICON_KEY</code>.
 320      * <p>
 321      * Note: the value of this field is prefixed with 'Swing' to
 322      * avoid possible collisions with existing <code>Actions</code>.
 323      *
 324      * @since 1.6
 325      */
 326     public static final String LARGE_ICON_KEY = "SwingLargeIconKey";
 327 
 328     /**
 329      * Gets one of this object's properties
 330      * using the associated key.
 331      *
 332      * @param key a {@code String} containing the key
 333      * @return the {@code Object} value
 334      * @see #putValue
 335      */
 336     public Object getValue(String key);
 337     /**
 338      * Sets one of this object's properties
 339      * using the associated key. If the value has
 340      * changed, a <code>PropertyChangeEvent</code> is sent
 341      * to listeners.
 342      *
 343      * @param key    a <code>String</code> containing the key
 344      * @param value  an <code>Object</code> value
 345      */
 346     public void putValue(String key, Object value);
 347 
 348     /**
 349      * Sets the enabled state of the {@code Action}.  When enabled,
 350      * any component associated with this object is active and
 351      * able to fire this object's {@code actionPerformed} method.
 352      * If the value has changed, a {@code PropertyChangeEvent} is sent
 353      * to listeners.
 354      *
 355      * @param  b true to enable this {@code Action}, false to disable it
 356      * @see #accept
 357      */
 358     public void setEnabled(boolean b);
 359     /**
 360      * Returns the enabled state of the {@code Action}. When enabled,
 361      * any component associated with this object is active and
 362      * able to fire this object's {@code actionPerformed} method.
 363      *
 364      * @return true if this {@code Action} is enabled
 365      * @see #accept
 366      */
 367     public boolean isEnabled();
 368 
 369     /**
 370      * Determines whether the action should be performed with the specified
 371      * sender object. The {@code sender} can be {@code null}.
 372      * The method must return false if the action is disabled.
 373      * <p>
 374      * @param sender the object to check, can be null
 375      * @return {@code true} if the action should be performed with the sender
 376      *         object, must be false if the action is disabled.
 377      * @see #isEnabled
 378      * @see #setEnabled
 379      */
 380     default boolean accept(Object sender) {
 381         return isEnabled();
 382     }
 383 
 384     /**
 385      * Adds a <code>PropertyChange</code> listener. Containers and attached
 386      * components use these methods to register interest in this
 387      * <code>Action</code> object. When its enabled state or other property
 388      * changes, the registered listeners are informed of the change.
 389      *
 390      * @param listener  a <code>PropertyChangeListener</code> object
 391      */
 392     public void addPropertyChangeListener(PropertyChangeListener listener);
 393     /**
 394      * Removes a <code>PropertyChange</code> listener.
 395      *
 396      * @param listener  a <code>PropertyChangeListener</code> object
 397      * @see #addPropertyChangeListener
 398      */
 399     public void removePropertyChangeListener(PropertyChangeListener listener);
 400 
 401 }