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