1. /*
  2. * @(#)ActionEvent.java 1.25 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 java.awt.event;
  8. import java.awt.AWTEvent;
  9. import java.awt.Event;
  10. /**
  11. * A semantic event which indicates that a component-defined action occured.
  12. * This high-level event is generated by a component (such as a Button) when
  13. * the component-specific action occurs (such as being pressed).
  14. * The event is passed to every every <code>ActionListener</code> object
  15. * that registered to receive such events using the component's
  16. * <code>addActionListener</code> method.
  17. * <P>
  18. * The object that implements the <code>ActionListener</code> interface
  19. * gets this <code>ActionEvent</code> when the event occurs. The listener
  20. * is therefore spared the details of processing individual mouse movements
  21. * and mouse clicks, and can instead process a "meaningful" (semantic)
  22. * event like "button pressed".
  23. *
  24. * @see ActionListener
  25. * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/eventmodel.html">Tutorial: Java 1.1 Event Model</a>
  26. * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
  27. *
  28. * @author Carl Quinn
  29. * @version 1.25 01/23/03
  30. * @since 1.1
  31. */
  32. public class ActionEvent extends AWTEvent {
  33. /**
  34. * The shift modifier. An indicator that the shift key was held
  35. * down during the event.
  36. */
  37. public static final int SHIFT_MASK = Event.SHIFT_MASK;
  38. /**
  39. * The control modifier. An indicator that the control key was held
  40. * down during the event.
  41. */
  42. public static final int CTRL_MASK = Event.CTRL_MASK;
  43. /**
  44. * The meta modifier. An indicator that the meta key was held
  45. * down during the event.
  46. */
  47. public static final int META_MASK = Event.META_MASK;
  48. /**
  49. * The alt modifier. An indicator that the alt key was held
  50. * down during the event.
  51. */
  52. public static final int ALT_MASK = Event.ALT_MASK;
  53. /**
  54. * The first number in the range of ids used for action events.
  55. */
  56. public static final int ACTION_FIRST = 1001;
  57. /**
  58. * The last number in the range of ids used for action events.
  59. */
  60. public static final int ACTION_LAST = 1001;
  61. /**
  62. * This event id indicates that a meaningful action occured.
  63. */
  64. public static final int ACTION_PERFORMED = ACTION_FIRST; //Event.ACTION_EVENT
  65. /**
  66. * The nonlocalized string that gives more details
  67. * of what actually caused the event.
  68. * This information is very specific to the component
  69. * that fired it.
  70. * @serial
  71. * @see #getActionCommand
  72. */
  73. String actionCommand;
  74. /**
  75. * Timestamp of when this event occurred. Because an ActionEvent is a high-
  76. * level, semantic event, the timestamp is typically the same as an
  77. * underlying InputEvent.
  78. *
  79. * @serial
  80. * @see #getWhen
  81. */
  82. long when;
  83. /**
  84. * This represents the key modifier that was selected,
  85. * and is used to determine the state of the selected key.
  86. * If no modifier has been selected it will default to
  87. * zero.
  88. *
  89. * @serial
  90. * @see #getModifiers
  91. */
  92. int modifiers;
  93. /*
  94. * JDK 1.1 serialVersionUID
  95. */
  96. private static final long serialVersionUID = -7671078796273832149L;
  97. /**
  98. * Constructs an <code>ActionEvent</code> object.
  99. * <p>Note that passing in an invalid <code>id</code> results in
  100. * unspecified behavior.
  101. *
  102. * @param source the object that originated the event
  103. * @param id an integer that identifies the event
  104. * @param command a string that may specify a command (possibly one
  105. * of several) associated with the event
  106. */
  107. public ActionEvent(Object source, int id, String command) {
  108. this(source, id, command, 0);
  109. }
  110. /**
  111. * Constructs an <code>ActionEvent</code> object with modifier keys.
  112. * <p>Note that passing in an invalid <code>id</code> results in
  113. * unspecified behavior.
  114. *
  115. * @param source the object that originated the event
  116. * @param id an integer that identifies the event
  117. * @param command a string that may specify a command (possibly one
  118. * of several) associated with the event
  119. * @param modifiers the modifier keys held down during this action
  120. */
  121. public ActionEvent(Object source, int id, String command, int modifiers) {
  122. this(source, id, command, 0, modifiers);
  123. }
  124. /**
  125. * Constructs an <code>ActionEvent</code> object with the specified
  126. * modifier keys and timestamp.
  127. * <p>
  128. * Note that passing in an invalid <code>id</code> results in unspecified
  129. * behavior.
  130. *
  131. * @param source the object that originated the event
  132. * @param id an integer that identifies the event
  133. * @param command a string that may specify a command (possibly one
  134. * of several) associated with the event
  135. * @param when the time the event occurred
  136. * @param modifiers the modifier keys held down during this action
  137. *
  138. * @since 1.4
  139. */
  140. public ActionEvent(Object source, int id, String command, long when,
  141. int modifiers) {
  142. super(source, id);
  143. this.actionCommand = command;
  144. this.when = when;
  145. this.modifiers = modifiers;
  146. }
  147. /**
  148. * Returns the command string associated with this action.
  149. * This string allows a "modal" component to specify one of several
  150. * commands, depending on its state. For example, a single button might
  151. * toggle between "show details" and "hide details". The source object
  152. * and the event would be the same in each case, but the command string
  153. * would identify the intended action.
  154. *
  155. * @return the string identifying the command for this event
  156. */
  157. public String getActionCommand() {
  158. return actionCommand;
  159. }
  160. /**
  161. * Returns the timestamp of when this event occurred. Because an
  162. * ActionEvent is a high-level, semantic event, the timestamp is typically
  163. * the same as an underlying InputEvent.
  164. *
  165. * @return this event's timestamp
  166. * @since 1.4
  167. */
  168. public long getWhen() {
  169. return when;
  170. }
  171. /**
  172. * Returns the modifier keys held down during this action event.
  173. *
  174. * @return the bitwise-or of the modifier constants
  175. */
  176. public int getModifiers() {
  177. return modifiers;
  178. }
  179. /**
  180. * Returns a parameter string identifying this action event.
  181. * This method is useful for event-logging and for debugging.
  182. *
  183. * @return a string identifying the event and its associated command
  184. */
  185. public String paramString() {
  186. String typeStr;
  187. switch(id) {
  188. case ACTION_PERFORMED:
  189. typeStr = "ACTION_PERFORMED";
  190. break;
  191. default:
  192. typeStr = "unknown type";
  193. }
  194. return typeStr + ",cmd="+actionCommand+",when="+when+",modifiers="+
  195. KeyEvent.getKeyModifiersText(modifiers);
  196. }
  197. }