1. /*
  2. * @(#)Action.java 1.30 03/12/19
  3. *
  4. * Copyright 2004 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>AbstractAction</code>).
  63. * The <code>Action</code> object
  64. * can then be added to multiple <code>Action</code>-aware containers
  65. * and connected to <code>Action</code>-capable
  66. * components. The GUI controls can then be activated or
  67. * deactivated all at once by invoking the <code>Action</code> object's
  68. * <code>setEnabled</code> method.
  69. * <p>
  70. * Note that <code>Action</code> implementations tend to be more expensive
  71. * in terms of storage than a typical <code>ActionListener</code>,
  72. * which does not offer the benefits of centralized control of
  73. * functionality and broadcast of property changes. For this reason,
  74. * you should take care to only use <code>Action</code>s where their benefits
  75. * are desired, and use simple <code>ActionListener</code>s elsewhere.
  76. *
  77. * @version 1.30 12/19/03
  78. * @author Georges Saab
  79. * @see AbstractAction
  80. */
  81. public interface Action extends ActionListener {
  82. /**
  83. * Useful constants that can be used as the storage-retrieval key
  84. * when setting or getting one of this object's properties (text
  85. * or icon).
  86. */
  87. /**
  88. * Not currently used.
  89. */
  90. public static final String DEFAULT = "Default";
  91. /**
  92. * The key used for storing the <code>String</code> name
  93. * for the action, used for a menu or button.
  94. */
  95. public static final String NAME = "Name";
  96. /**
  97. * The key used for storing a short <code>String</code>
  98. * description for the action, used for tooltip text.
  99. */
  100. public static final String SHORT_DESCRIPTION = "ShortDescription";
  101. /**
  102. * The key used for storing a longer <code>String</code>
  103. * description for the action, could be used for context-sensitive help.
  104. */
  105. public static final String LONG_DESCRIPTION = "LongDescription";
  106. /**
  107. * The key used for storing a small <code>Icon</code>, such
  108. * as <code>ImageIcon</code>, for the action, used for toolbar buttons.
  109. */
  110. public static final String SMALL_ICON = "SmallIcon";
  111. /**
  112. * The key used to determine the command <code>String</code> for the
  113. * <code>ActionEvent</code> that will be created when an
  114. * <code>Action</code> is going to be notified as the result of
  115. * residing in a <code>Keymap</code> associated with a
  116. * <code>JComponent</code>.
  117. */
  118. public static final String ACTION_COMMAND_KEY = "ActionCommandKey";
  119. /**
  120. * The key used for storing a <code>KeyStroke</code> to be used as the
  121. * accelerator for the action.
  122. *
  123. * @since 1.3
  124. */
  125. public static final String ACCELERATOR_KEY="AcceleratorKey";
  126. /**
  127. * The key used for storing a <code>KeyEvent</code> to be used as
  128. * the mnemonic for the action.
  129. *
  130. * @since 1.3
  131. */
  132. public static final String MNEMONIC_KEY="MnemonicKey";
  133. /**
  134. * Gets one of this object's properties
  135. * using the associated key.
  136. * @see #putValue
  137. */
  138. public Object getValue(String key);
  139. /**
  140. * Sets one of this object's properties
  141. * using the associated key. If the value has
  142. * changed, a <code>PropertyChangeEvent</code> is sent
  143. * to listeners.
  144. *
  145. * @param key a <code>String</code> containing the key
  146. * @param value an <code>Object</code> value
  147. */
  148. public void putValue(String key, Object value);
  149. /**
  150. * Sets the enabled state of the <code>Action</code>. When enabled,
  151. * any component associated with this object is active and
  152. * able to fire this object's <code>actionPerformed</code> method.
  153. * If the value has changed, a <code>PropertyChangeEvent</code> is sent
  154. * to listeners.
  155. *
  156. * @param b true to enable this <code>Action</code>, false to disable it
  157. */
  158. public void setEnabled(boolean b);
  159. /**
  160. * Returns the enabled state of the <code>Action</code>. When enabled,
  161. * any component associated with this object is active and
  162. * able to fire this object's <code>actionPerformed</code> method.
  163. *
  164. * @return true if this <code>Action</code> is enabled
  165. */
  166. public boolean isEnabled();
  167. /**
  168. * Adds a <code>PropertyChange</code> listener. Containers and attached
  169. * components use these methods to register interest in this
  170. * <code>Action</code> object. When its enabled state or other property
  171. * changes, the registered listeners are informed of the change.
  172. *
  173. * @param listener a <code>PropertyChangeListener</code> object
  174. */
  175. public void addPropertyChangeListener(PropertyChangeListener listener);
  176. /**
  177. * Removes a <code>PropertyChange</code> listener.
  178. *
  179. * @param listener a <code>PropertyChangeListener</code> object
  180. * @see #addPropertyChangeListener
  181. */
  182. public void removePropertyChangeListener(PropertyChangeListener listener);
  183. }