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