1. /*
  2. * @(#)Action.java 1.26 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.beans.*;
  11. /**
  12. * The <code>Action</code> interface provides a useful extension to the
  13. * <code>ActionListener</code>
  14. * interface in cases where the same functionality may be accessed by
  15. * several controls.
  16. * <p>
  17. * In addition to the <code>actionPerformed</code> method defined by the
  18. * <code>ActionListener</code> interface, this interface allows the
  19. * application to define, in a single place:
  20. * <ul>
  21. * <li>One or more text strings that describe the function. These strings
  22. * can be used, for example, to display the flyover text for a button
  23. * or to set the text in a menu item.
  24. * <li>One or more icons that depict the function. These icons can be used
  25. * for the images in a menu control, or for composite entries in a more
  26. * sophisticated user interface.
  27. * <li>The enabled/disabled state of the functionality. Instead of having
  28. * to separately disable the menu item and the toolbar button, the
  29. * application can disable the function that implements this interface.
  30. * All components which are registered as listeners for the state change
  31. * then know to disable event generation for that item and to modify the
  32. * display accordingly.
  33. * </ul>
  34. * Certain containers, including menus and tool bars, know how to add an
  35. * <code>Action</code> object. When an <code>Action</code> object is added
  36. * to such a container, the container:
  37. * <ol type="a">
  38. * <li>Creates a component that is appropriate for that container
  39. * (a tool bar creates a button component, for example).
  40. * <li>Gets the appropriate property(s) from the <code>Action</code> object to
  41. * customize the component (for example, the icon image and flyover text).
  42. * <li>Checks the initial state of the <code>Action</code> object to determine
  43. * if it is enabled or disabled, and renders the component in the
  44. * appropriate fashion.
  45. * <li>Registers a listener with the <code>Action</code> object so that is
  46. * notified of state changes. When the <code>Action</code> object changes
  47. * from enabled to disabled,
  48. * or back, the container makes the appropriate revisions to the
  49. * event-generation mechanisms and renders the component accordingly.
  50. * </ol>
  51. * For example, both a menu item and a toolbar button could access a
  52. * <code>Cut</code> action object. The text associated with the object is
  53. * specified as "Cut", and an image depicting a pair of scissors is specified
  54. * as its icon. The <code>Cut</code> action-object can then be added to a
  55. * menu and to a tool bar. Each container does the appropriate things with the
  56. * object, and invokes its <code>actionPerformed</code> method when the
  57. * component associated with it is activated. The application can then disable
  58. * or enable the application object without worrying about what user-interface
  59. * components are connected to it.
  60. * <p>
  61. * This interface can be added to an existing class or used to create an
  62. * adapter (typically, by subclassing <code>AbstractActio</code>).
  63. * The <code>Action</code> object
  64. * can then be added to multiple action-aware containers and connected to
  65. * Action-capable components. The GUI controls can then be activated or
  66. * deactivated all at once by invoking the <code>Action</code> object's
  67. * <code>setEnabled</code> method.
  68. * <p>
  69. * Note that <code>Action</code> implementations tend to be more expensive
  70. * in terms of storage than a typical <code>ActionListener</code>,
  71. * which does not offer the benefits of centralized control of
  72. * functionality and broadcast of property changes. For th is reason,
  73. * you should take care to only use <code>Action</code>s where their benefits
  74. * are desired, and use simple <code>ActionListeners</code> elsewhere.
  75. *
  76. * @version 1.26 01/23/03
  77. * @author Georges Saab
  78. * @see AbstractAction
  79. */
  80. public interface Action extends ActionListener {
  81. /**
  82. * Useful constants that can be used as the storage-retrieval key
  83. * when setting or getting one of this object's properties (text
  84. * or icon).
  85. */
  86. /**
  87. * Not currently used.
  88. */
  89. public static final String DEFAULT = "Default";
  90. /**
  91. * The key used for storing the name for the action,
  92. * used for a menu or button.
  93. */
  94. public static final String NAME = "Name";
  95. /**
  96. * The key used for storing a short description for the action,
  97. * used for tooltip text.
  98. */
  99. public static final String SHORT_DESCRIPTION = "ShortDescription";
  100. /**
  101. * The key used for storing a longer description for the action,
  102. * could be used for context-sensitive help.
  103. */
  104. public static final String LONG_DESCRIPTION = "LongDescription";
  105. /**
  106. * The key used for storing a small icon for the action,
  107. * used for toolbar buttons.
  108. */
  109. public static final String SMALL_ICON = "SmallIcon";
  110. /**
  111. * The key used to determine the command string for the
  112. * <code>ActionEvent</code> that will be created when an
  113. * <code>Action</code> is going to be notified as the result of
  114. * residing in a <code>Keymap</code> associated with a
  115. * <code>JComponent</code>.
  116. */
  117. public static final String ACTION_COMMAND_KEY = "ActionCommandKey";
  118. /**
  119. * The key used for storing a <code>KeyStroke</code> to be used as the
  120. * accelerator for the action.
  121. *
  122. * @since 1.3
  123. */
  124. public static final String ACCELERATOR_KEY="AcceleratorKey";
  125. /**
  126. * The key used for storing an int key code to be used as the mnemonic
  127. * for the action.
  128. *
  129. * @since 1.3
  130. */
  131. public static final String MNEMONIC_KEY="MnemonicKey";
  132. /**
  133. * Gets one of this object's properties
  134. * using the associated key.
  135. * @see #putValue
  136. */
  137. public Object getValue(String key);
  138. /**
  139. * Sets one of this object's properties
  140. * using the associated key. If the value has
  141. * changed, a <code>PropertyChangeEvent</code> is sent
  142. * to listeners.
  143. *
  144. * @param key a <code>String</code> containing the key
  145. * @param value an <code>Object</code> value
  146. */
  147. public void putValue(String key, Object value);
  148. /**
  149. * Sets the enabled state of the <code>Action</code>. When enabled,
  150. * any component associated with this object is active and
  151. * able to fire this object's <code>actionPerformed</code> method.
  152. * If the value has changed, a <code>PropertyChangeEvent</code> is sent
  153. * to listeners.
  154. *
  155. * @param b true to enable this <code>Action</code>, false to disable it
  156. */
  157. public void setEnabled(boolean b);
  158. /**
  159. * Returns the enabled state of the <code>Action</code>. When enabled,
  160. * any component associated with this object is active and
  161. * able to fire this object's <code>actionPerformed</code> method.
  162. *
  163. * @return true if this <code>Action</code> is enabled
  164. */
  165. public boolean isEnabled();
  166. /**
  167. * Adds a <code>PropertyChange</code> listener. Containers and attached
  168. * components use these methods to register interest in this
  169. * <code>Action</code> object. When its enabled state or other property
  170. * changes, the registered listeners are informed of the change.
  171. *
  172. * @param listener a <code>PropertyChangeListener</code> object
  173. */
  174. public void addPropertyChangeListener(PropertyChangeListener listener);
  175. /**
  176. * Removes a <code>PropertyChange</code> listener.
  177. *
  178. * @param listener a <code>PropertyChangeListener</code> object
  179. * @see #addPropertyChangeListener
  180. */
  181. public void removePropertyChangeListener(PropertyChangeListener listener);
  182. }