1. /*
  2. * @(#)Action.java 1.16 01/11/29
  3. *
  4. * Copyright 2002 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 JFC Action interface provides a useful extension to the ActionListner
  13. * interface in cases where the same functionality may be accessed by
  14. * several controls.
  15. * <p>
  16. * In addition to the <code>actionPerformed</code> method
  17. * defined by the ActionListener interface, this interface allows the
  18. * application to define, in a single place:
  19. * <ul>
  20. * <li>One or more text strings that describe the function. These strings
  21. * can be used, for example, to display the flyover text for a button
  22. * or to set the text in a menu item.
  23. * <li>One or more icons that depict the function. These icons can be used
  24. * for the images in a menu control, or for composite-entries in a more
  25. * sophisticated user-interface.
  26. * <li>The enabled/disabled state of the functionality. Instead of having
  27. * to separately disable the menu-item and the toolbar-button, the
  28. * application can disable the function that implements this interface.
  29. * All components which are registered as listeners for the state-change
  30. * then know to disable event-generation for that item and to modify the
  31. * display accordingly.
  32. * </ul>
  33. * Containers in the Swing set like menus and toolbars know how to add an
  34. * Action object, as well as other components, using a version of the
  35. * <code>add</code> method. When an Action object is added to such a
  36. * container, the container:
  37. * <ol type="a">
  38. * <li>Creates a component that is appropriate for that container
  39. * (a toolbar creates a button component, for example).
  40. * <li>Gets the appropriate property(s) from the Action object to customize
  41. * the component (for example, the icon image and flyover text).
  42. * <li>Checks the intial state of the Action object to determine if it is
  43. * enabled or disabled, and renders the component in the appropriate
  44. * fashion.
  45. * <li>Registers a listener with the Action object so that is notified of
  46. * state changes. When the Action object changes from enabled to disabled,
  47. * or back, the container makes the appropriate revisions to the
  48. * event-generation mechanisms and renders the component accordingly.
  49. * </ol>
  50. * For example, both a menu item and a toolbar button could access a
  51. * <code>Cut</code> action object. The text associated with the object is
  52. * specified as "Cut", and an image depicting a pair of scissors is specified
  53. * as its icon. The <code>Cut</code> action-object can then be added to a
  54. * menu and to a toolbar. Each container does the appropriate things with the
  55. * object, and invokes its <code>actionPerformed</code> method when the
  56. * component associated with it is activated. The application can then disable
  57. * or enable the application object without worrying about what user-interface
  58. * components are connected to it.
  59. * <p>
  60. * This interface can be added to an existing class or used to create an
  61. * adapter (typically, by subclassing AbstractAction). The Action object
  62. * can then be added to multiple action-aware containers and connected to
  63. * Action-capable components. The GUI controls can then be activated or
  64. * deactivated all at once by invoking the Action object's <code>setEnabled</code>
  65. * method.
  66. *
  67. * Note that Action implementations tend to be more expensive in terms of
  68. * storage than a typical ActionListener, which does not offer the benefits
  69. * of centralized control of functionality and broadcast of property changes.
  70. * For this reason, you should take care to only use Actions where their
  71. * benefits are desired, and use a simple ActionListener where they
  72. * are not necessary.
  73. *
  74. * @version 1.16 11/29/01
  75. * @author Georges Saab
  76. * @see AbstractAction
  77. */
  78. public interface Action extends ActionListener {
  79. /**
  80. * Useful constants that can be used as the storage-retreival key
  81. * when setting or getting one of this object's properties (text
  82. * or icon).
  83. */
  84. public static final String DEFAULT = "Default";
  85. /**
  86. * The key used for storing the name for the action,
  87. * used for a menu or button.
  88. */
  89. public static final String NAME = "Name";
  90. /**
  91. * The key used for storing a short description for the action,
  92. * used for tooltip text.
  93. */
  94. public static final String SHORT_DESCRIPTION = "ShortDescription";
  95. /**
  96. * The key used for storing a longer description for the action,
  97. * could be used for context-sensitive help.
  98. */
  99. public static final String LONG_DESCRIPTION = "LongDescription";
  100. /**
  101. * The key used for storing a small icon for the action,
  102. * used for toolbar buttons.
  103. */
  104. public static final String SMALL_ICON = "SmallIcon";
  105. /**
  106. * Gets one of this object's properties
  107. * using the associated key.
  108. * @see #putValue
  109. */
  110. public Object getValue(String key);
  111. /**
  112. * Sets one of this object's properties
  113. * using the associated key. If the value has
  114. * changed, a PropertyChangeEvent is sent
  115. * to listeners.
  116. *
  117. * @param key a String containing the key
  118. * @param value an Object value
  119. */
  120. public void putValue(String key, Object value);
  121. /**
  122. * Tests the enabled state of the Action. When enabled,
  123. * any component associated with this object is active and
  124. * able to fire this object's <code>actionPerformed</code> method.
  125. * If the value has changed, a PropertyChangeEvent is sent
  126. * to listeners.
  127. *
  128. * @param b true to enable this Action, false to disable it
  129. */
  130. public void setEnabled(boolean b);
  131. /**
  132. * Sets the enabled state of the Action. When enabled,
  133. * any component associated with this object is active and
  134. * able to fire this object's <code>actionPerformed</code> method.
  135. *
  136. * @return true if this Action is enabled
  137. */
  138. public boolean isEnabled();
  139. /**
  140. * Add a PropertyChange listener. Containers and attached
  141. * components use these methods to register interest in this Action
  142. * object. When its enabled state or other property changes,
  143. * the registered listeners are informed of the change.
  144. *
  145. * @param listener a PropertyChangeListener object ...
  146. */
  147. public void addPropertyChangeListener(PropertyChangeListener listener);
  148. /**
  149. * Remove a PropertyChange listener.
  150. *
  151. * @param listener a PropertyChangeListener object ...
  152. * @see #addPropertyChangeListener
  153. */
  154. public void removePropertyChangeListener(PropertyChangeListener listener);
  155. }