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 style="display:none">Supported Action properties</caption>
  99  * <thead>
 100  *  <tr>
 101  *    <th>Component Property
 102  *    <th>Components
 103  *    <th>Action Key
 104  *    <th>Notes
 105  * </thead>
 106  * <tbody>
 107  *  <tr valign="top" style="text-align:left">
 108  *      <td><b><code>enabled</code></b>
 109  *      <td>All
 110  *      <td>The <code>isEnabled</code> method
 111  *      <td>&nbsp;
 112  *  <tr valign="top" style="text-align:left">
 113  *      <td><b><code>toolTipText</code></b>
 114  *      <td>All
 115  *      <td><code>SHORT_DESCRIPTION</code>
 116  *      <td>&nbsp;
 117  *  <tr valign="top" style="text-align:left">
 118  *      <td><b><code>actionCommand</code></b>
 119  *      <td>All
 120  *      <td><code>ACTION_COMMAND_KEY</code>
 121  *      <td>&nbsp;
 122  *  <tr valign="top" style="text-align:left">
 123  *      <td><b><code>mnemonic</code></b>
 124  *      <td>All buttons
 125  *      <td><code>MNEMONIC_KEY</code>
 126  *      <td>A <code>null</code> value or <code>Action</code> results in the
 127  *          button's <code>mnemonic</code> property being set to
 128  *          <code>'\0'</code>.
 129  *  <tr valign="top" style="text-align:left">
 130  *      <td><b><code>text</code></b>
 131  *      <td>All buttons
 132  *      <td><code>NAME</code>
 133  *      <td>If you do not want the text of the button to mirror that
 134  *          of the <code>Action</code>, set the property
 135  *          <code>hideActionText</code> to <code>true</code>.  If
 136  *          <code>hideActionText</code> is <code>true</code>, setting the
 137  *          <code>Action</code> changes the text of the button to
 138  *          <code>null</code> and any changes to <code>NAME</code>
 139  *          are ignored.  <code>hideActionText</code> is useful for
 140  *          tool bar buttons that typically only show an <code>Icon</code>.
 141  *          <code>JToolBar.add(Action)</code> sets the property to
 142  *          <code>true</code> if the <code>Action</code> has a
 143  *          non-<code>null</code> value for <code>LARGE_ICON_KEY</code> or
 144  *          <code>SMALL_ICON</code>.
 145  *  <tr valign="top" style="text-align:left">
 146  *      <td><b><code>displayedMnemonicIndex</code></b>
 147  *      <td>All buttons
 148  *      <td><code>DISPLAYED_MNEMONIC_INDEX_KEY</code>
 149  *      <td>If the value of <code>DISPLAYED_MNEMONIC_INDEX_KEY</code> is
 150  *          beyond the bounds of the text, it is ignored.  When
 151  *          <code>setAction</code> is called, if the value from the
 152  *          <code>Action</code> is <code>null</code>, the displayed
 153  *          mnemonic index is not updated.  In any subsequent changes to
 154  *          <code>DISPLAYED_MNEMONIC_INDEX_KEY</code>, <code>null</code>
 155  *          is treated as -1.
 156  *  <tr valign="top" style="text-align:left">
 157  *      <td><b><code>icon</code></b>
 158  *      <td>All buttons except of <code>JCheckBox</code>,
 159  *      <code>JToggleButton</code> and <code>JRadioButton</code>.
 160  *      <td>either <code>LARGE_ICON_KEY</code> or
 161  *          <code>SMALL_ICON</code>
 162  *     <td>The <code>JMenuItem</code> subclasses only use
 163  *         <code>SMALL_ICON</code>.  All other buttons will use
 164  *         <code>LARGE_ICON_KEY</code>; if the value is <code>null</code> they
 165  *         use <code>SMALL_ICON</code>.
 166  *  <tr valign="top" style="text-align:left">
 167  *      <td><b><code>accelerator</code></b>
 168  *      <td>All <code>JMenuItem</code> subclasses, with the exception of
 169  *          <code>JMenu</code>.
 170  *      <td><code>ACCELERATOR_KEY</code>
 171  *      <td>&nbsp;
 172  *  <tr valign="top" style="text-align:left">
 173  *      <td><b><code>selected</code></b>
 174  *      <td><code>JToggleButton</code>, <code>JCheckBox</code>,
 175  *          <code>JRadioButton</code>, <code>JCheckBoxMenuItem</code> and
 176  *          <code>JRadioButtonMenuItem</code>
 177  *      <td><code>SELECTED_KEY</code>
 178  *      <td>Components that honor this property only use
 179  *          the value if it is {@code non-null}. For example, if
 180  *          you set an {@code Action} that has a {@code null}
 181  *          value for {@code SELECTED_KEY} on a {@code JToggleButton}, the
 182  *          {@code JToggleButton} will not update it's selected state in
 183  *          any way. Similarly, any time the {@code JToggleButton}'s
 184  *          selected state changes it will only set the value back on
 185  *          the {@code Action} if the {@code Action} has a {@code non-null}
 186  *          value for {@code SELECTED_KEY}.
 187  *          <br>
 188  *          Components that honor this property keep their selected state
 189  *          in sync with this property. When the same {@code Action} is used
 190  *          with multiple components, all the components keep their selected
 191  *          state in sync with this property. Mutually exclusive
 192  *          buttons, such as {@code JToggleButton}s in a {@code ButtonGroup},
 193  *          force only one of the buttons to be selected. As such, do not
 194  *          use the same {@code Action} that defines a value for the
 195  *          {@code SELECTED_KEY} property with multiple mutually
 196  *          exclusive buttons.
 197  * </tbody>
 198  * </table>
 199  * <p>
 200  * <code>JPopupMenu</code>, <code>JToolBar</code> and <code>JMenu</code>
 201  * all provide convenience methods for creating a component and setting the
 202  * <code>Action</code> on the corresponding component.  Refer to each of
 203  * these classes for more information.
 204  * <p>
 205  * <code>Action</code> uses <code>PropertyChangeListener</code> to
 206  * inform listeners the <code>Action</code> has changed.  The beans
 207  * specification indicates that a <code>null</code> property name can
 208  * be used to indicate multiple values have changed.  By default Swing
 209  * components that take an <code>Action</code> do not handle such a
 210  * change.  To indicate that Swing should treat <code>null</code>
 211  * according to the beans specification set the system property
 212  * <code>swing.actions.reconfigureOnNull</code> to the <code>String</code>
 213  * value <code>true</code>.
 214  *
 215  * @author Georges Saab
 216  * @see AbstractAction
 217  * @since 1.2
 218  */
 219 public interface Action extends ActionListener {
 220     /**
 221      * Useful constants that can be used as the storage-retrieval key
 222      * when setting or getting one of this object's properties (text
 223      * or icon).
 224      */
 225     /**
 226      * Not currently used.
 227      */
 228     public static final String DEFAULT = "Default";
 229     /**
 230      * The key used for storing the <code>String</code> name
 231      * for the action, used for a menu or button.
 232      */
 233     public static final String NAME = "Name";
 234     /**
 235      * The key used for storing a short <code>String</code>
 236      * description for the action, used for tooltip text.
 237      */
 238     public static final String SHORT_DESCRIPTION = "ShortDescription";
 239     /**
 240      * The key used for storing a longer <code>String</code>
 241      * description for the action, could be used for context-sensitive help.
 242      */
 243     public static final String LONG_DESCRIPTION = "LongDescription";
 244     /**
 245      * The key used for storing a small <code>Icon</code>, such
 246      * as <code>ImageIcon</code>.  This is typically used with
 247      * menus such as <code>JMenuItem</code>.
 248      * <p>
 249      * If the same <code>Action</code> is used with menus and buttons you'll
 250      * typically specify both a <code>SMALL_ICON</code> and a
 251      * <code>LARGE_ICON_KEY</code>.  The menu will use the
 252      * <code>SMALL_ICON</code> and the button will use the
 253      * <code>LARGE_ICON_KEY</code>.
 254      */
 255     public static final String SMALL_ICON = "SmallIcon";
 256 
 257     /**
 258      * The key used to determine the command <code>String</code> for the
 259      * <code>ActionEvent</code> that will be created when an
 260      * <code>Action</code> is going to be notified as the result of
 261      * residing in a <code>Keymap</code> associated with a
 262      * <code>JComponent</code>.
 263      */
 264     public static final String ACTION_COMMAND_KEY = "ActionCommandKey";
 265 
 266     /**
 267      * The key used for storing a <code>KeyStroke</code> to be used as the
 268      * accelerator for the action.
 269      *
 270      * @since 1.3
 271      */
 272     public static final String ACCELERATOR_KEY="AcceleratorKey";
 273 
 274     /**
 275      * The key used for storing an <code>Integer</code> that corresponds to
 276      * one of the <code>KeyEvent</code> key codes.  The value is
 277      * commonly used to specify a mnemonic.  For example:
 278      * <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A)</code>
 279      * sets the mnemonic of <code>myAction</code> to 'a', while
 280      * <code>myAction.putValue(Action.MNEMONIC_KEY, KeyEvent.getExtendedKeyCodeForChar('\u0444'))</code>
 281      * sets the mnemonic of <code>myAction</code> to Cyrillic letter "Ef".
 282      *
 283      * @since 1.3
 284      */
 285     public static final String MNEMONIC_KEY="MnemonicKey";
 286 
 287     /**
 288      * The key used for storing a <code>Boolean</code> that corresponds
 289      * to the selected state.  This is typically used only for components
 290      * that have a meaningful selection state.  For example,
 291      * <code>JRadioButton</code> and <code>JCheckBox</code> make use of
 292      * this but instances of <code>JMenu</code> don't.
 293      * <p>
 294      * This property differs from the others in that it is both read
 295      * by the component and set by the component.  For example,
 296      * if an <code>Action</code> is attached to a <code>JCheckBox</code>
 297      * the selected state of the <code>JCheckBox</code> will be set from
 298      * that of the <code>Action</code>.  If the user clicks on the
 299      * <code>JCheckBox</code> the selected state of the <code>JCheckBox</code>
 300      * <b>and</b> the <code>Action</code> will <b>both</b> be updated.
 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      * @since 1.6
 306      */
 307     public static final String SELECTED_KEY = "SwingSelectedKey";
 308 
 309     /**
 310      * The key used for storing an <code>Integer</code> that corresponds
 311      * to the index in the text (identified by the <code>NAME</code>
 312      * property) that the decoration for a mnemonic should be rendered at.  If
 313      * the value of this property is greater than or equal to the length of
 314      * the text, it will treated as -1.
 315      * <p>
 316      * Note: the value of this field is prefixed with 'Swing' to
 317      * avoid possible collisions with existing <code>Actions</code>.
 318      *
 319      * @see AbstractButton#setDisplayedMnemonicIndex
 320      * @since 1.6
 321      */
 322     public static final String DISPLAYED_MNEMONIC_INDEX_KEY =
 323                                  "SwingDisplayedMnemonicIndexKey";
 324 
 325     /**
 326      * The key used for storing an <code>Icon</code>.  This is typically
 327      * used by buttons, such as <code>JButton</code> and
 328      * <code>JToggleButton</code>.
 329      * <p>
 330      * If the same <code>Action</code> is used with menus and buttons you'll
 331      * typically specify both a <code>SMALL_ICON</code> and a
 332      * <code>LARGE_ICON_KEY</code>.  The menu will use the
 333      * <code>SMALL_ICON</code> and the button the <code>LARGE_ICON_KEY</code>.
 334      * <p>
 335      * Note: the value of this field is prefixed with 'Swing' to
 336      * avoid possible collisions with existing <code>Actions</code>.
 337      *
 338      * @since 1.6
 339      */
 340     public static final String LARGE_ICON_KEY = "SwingLargeIconKey";
 341 
 342     /**
 343      * Gets one of this object's properties
 344      * using the associated key.
 345      *
 346      * @param key a {@code String} containing the key
 347      * @return the {@code Object} value
 348      * @see #putValue
 349      */
 350     public Object getValue(String key);
 351     /**
 352      * Sets one of this object's properties
 353      * using the associated key. If the value has
 354      * changed, a <code>PropertyChangeEvent</code> is sent
 355      * to listeners.
 356      *
 357      * @param key    a <code>String</code> containing the key
 358      * @param value  an <code>Object</code> value
 359      */
 360     public void putValue(String key, Object value);
 361 
 362     /**
 363      * Sets the enabled state of the {@code Action}.  When enabled,
 364      * any component associated with this object is active and
 365      * able to fire this object's {@code actionPerformed} method.
 366      * If the value has changed, a {@code PropertyChangeEvent} is sent
 367      * to listeners.
 368      *
 369      * @param  b true to enable this {@code Action}, false to disable it
 370      * @see #accept
 371      */
 372     public void setEnabled(boolean b);
 373     /**
 374      * Returns the enabled state of the {@code Action}. When enabled,
 375      * any component associated with this object is active and
 376      * able to fire this object's {@code actionPerformed} method.
 377      *
 378      * @return true if this {@code Action} is enabled
 379      * @see #accept
 380      */
 381     public boolean isEnabled();
 382 
 383     /**
 384      * Determines whether the action should be performed with the specified
 385      * sender object. The {@code sender} can be {@code null}.
 386      * The method must return false if the action is disabled.
 387      * <p>
 388      * @param sender the object to check, can be null
 389      * @return {@code true} if the action should be performed with the sender
 390      *         object, must be false if the action is disabled.
 391      * @see #isEnabled
 392      * @see #setEnabled
 393      */
 394     default boolean accept(Object sender) {
 395         return isEnabled();
 396     }
 397 
 398     /**
 399      * Adds a <code>PropertyChange</code> listener. Containers and attached
 400      * components use these methods to register interest in this
 401      * <code>Action</code> object. When its enabled state or other property
 402      * changes, the registered listeners are informed of the change.
 403      *
 404      * @param listener  a <code>PropertyChangeListener</code> object
 405      */
 406     public void addPropertyChangeListener(PropertyChangeListener listener);
 407     /**
 408      * Removes a <code>PropertyChange</code> listener.
 409      *
 410      * @param listener  a <code>PropertyChangeListener</code> object
 411      * @see #addPropertyChangeListener
 412      */
 413     public void removePropertyChangeListener(PropertyChangeListener listener);
 414 
 415 }