blob: 41ed7d6488ce9a6cfba3a08e2f24851ab1daf4cc [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package java.awt.event;
27
28import java.awt.AWTEvent;
29import java.awt.Event;
30
31/**
32 * A semantic event which indicates that a component-defined action occurred.
33 * This high-level event is generated by a component (such as a
34 * <code>Button</code>) when
35 * the component-specific action occurs (such as being pressed).
36 * The event is passed to every <code>ActionListener</code> object
37 * that registered to receive such events using the component's
38 * <code>addActionListener</code> method.
39 * <p>
40 * <b>Note:</b> To invoke an <code>ActionEvent</code> on a
41 * <code>Button</code> using the keyboard, use the Space bar.
42 * <P>
43 * The object that implements the <code>ActionListener</code> interface
44 * gets this <code>ActionEvent</code> when the event occurs. The listener
45 * is therefore spared the details of processing individual mouse movements
46 * and mouse clicks, and can instead process a "meaningful" (semantic)
47 * event like "button pressed".
48 *
49 * @see ActionListener
50 * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/eventmodel.html">Tutorial: Java 1.1 Event Model</a>
51 *
52 * @author Carl Quinn
53 * @since 1.1
54 */
55public class ActionEvent extends AWTEvent {
56
57 /**
58 * The shift modifier. An indicator that the shift key was held
59 * down during the event.
60 */
61 public static final int SHIFT_MASK = Event.SHIFT_MASK;
62
63 /**
64 * The control modifier. An indicator that the control key was held
65 * down during the event.
66 */
67 public static final int CTRL_MASK = Event.CTRL_MASK;
68
69 /**
70 * The meta modifier. An indicator that the meta key was held
71 * down during the event.
72 */
73 public static final int META_MASK = Event.META_MASK;
74
75 /**
76 * The alt modifier. An indicator that the alt key was held
77 * down during the event.
78 */
79 public static final int ALT_MASK = Event.ALT_MASK;
80
81
82 /**
83 * The first number in the range of ids used for action events.
84 */
85 public static final int ACTION_FIRST = 1001;
86
87 /**
88 * The last number in the range of ids used for action events.
89 */
90 public static final int ACTION_LAST = 1001;
91
92 /**
93 * This event id indicates that a meaningful action occured.
94 */
95 public static final int ACTION_PERFORMED = ACTION_FIRST; //Event.ACTION_EVENT
96
97 /**
98 * The nonlocalized string that gives more details
99 * of what actually caused the event.
100 * This information is very specific to the component
101 * that fired it.
102
103 * @serial
104 * @see #getActionCommand
105 */
106 String actionCommand;
107
108 /**
109 * Timestamp of when this event occurred. Because an ActionEvent is a high-
110 * level, semantic event, the timestamp is typically the same as an
111 * underlying InputEvent.
112 *
113 * @serial
114 * @see #getWhen
115 */
116 long when;
117
118 /**
119 * This represents the key modifier that was selected,
120 * and is used to determine the state of the selected key.
121 * If no modifier has been selected it will default to
122 * zero.
123 *
124 * @serial
125 * @see #getModifiers
126 */
127 int modifiers;
128
129 /*
130 * JDK 1.1 serialVersionUID
131 */
132 private static final long serialVersionUID = -7671078796273832149L;
133
134 /**
135 * Constructs an <code>ActionEvent</code> object.
136 * <p>
137 * Note that passing in an invalid <code>id</code> results in
138 * unspecified behavior. This method throws an
139 * <code>IllegalArgumentException</code> if <code>source</code>
140 * is <code>null</code>.
141 * A <code>null</code> <code>command</code> string is legal,
142 * but not recommended.
143 *
144 * @param source the object that originated the event
145 * @param id an integer that identifies the event
146 * @param command a string that may specify a command (possibly one
147 * of several) associated with the event
148 * @throws IllegalArgumentException if <code>source</code> is null
149 */
150 public ActionEvent(Object source, int id, String command) {
151 this(source, id, command, 0);
152 }
153
154 /**
155 * Constructs an <code>ActionEvent</code> object with modifier keys.
156 * <p>
157 * Note that passing in an invalid <code>id</code> results in
158 * unspecified behavior. This method throws an
159 * <code>IllegalArgumentException</code> if <code>source</code>
160 * is <code>null</code>.
161 * A <code>null</code> <code>command</code> string is legal,
162 * but not recommended.
163 *
164 * @param source the object that originated the event
165 * @param id an integer that identifies the event
166 * @param command a string that may specify a command (possibly one
167 * of several) associated with the event
168 * @param modifiers the modifier keys held down during this action
169 * @throws IllegalArgumentException if <code>source</code> is null
170 */
171 public ActionEvent(Object source, int id, String command, int modifiers) {
172 this(source, id, command, 0, modifiers);
173 }
174
175 /**
176 * Constructs an <code>ActionEvent</code> object with the specified
177 * modifier keys and timestamp.
178 * <p>
179 * Note that passing in an invalid <code>id</code> results in
180 * unspecified behavior. This method throws an
181 * <code>IllegalArgumentException</code> if <code>source</code>
182 * is <code>null</code>.
183 * A <code>null</code> <code>command</code> string is legal,
184 * but not recommended.
185 *
186 * @param source the object that originated the event
187 * @param id an integer that identifies the event
188 * @param command a string that may specify a command (possibly one
189 * of several) associated with the event
190 * @param when the time the event occurred
191 * @param modifiers the modifier keys held down during this action
192 * @throws IllegalArgumentException if <code>source</code> is null
193 *
194 * @since 1.4
195 */
196 public ActionEvent(Object source, int id, String command, long when,
197 int modifiers) {
198 super(source, id);
199 this.actionCommand = command;
200 this.when = when;
201 this.modifiers = modifiers;
202 }
203
204 /**
205 * Returns the command string associated with this action.
206 * This string allows a "modal" component to specify one of several
207 * commands, depending on its state. For example, a single button might
208 * toggle between "show details" and "hide details". The source object
209 * and the event would be the same in each case, but the command string
210 * would identify the intended action.
211 * <p>
212 * Note that if a <code>null</code> command string was passed
213 * to the constructor for this <code>ActionEvent</code>, this
214 * this method returns <code>null</code>.
215 *
216 * @return the string identifying the command for this event
217 */
218 public String getActionCommand() {
219 return actionCommand;
220 }
221
222 /**
223 * Returns the timestamp of when this event occurred. Because an
224 * ActionEvent is a high-level, semantic event, the timestamp is typically
225 * the same as an underlying InputEvent.
226 *
227 * @return this event's timestamp
228 * @since 1.4
229 */
230 public long getWhen() {
231 return when;
232 }
233
234 /**
235 * Returns the modifier keys held down during this action event.
236 *
237 * @return the bitwise-or of the modifier constants
238 */
239 public int getModifiers() {
240 return modifiers;
241 }
242
243 /**
244 * Returns a parameter string identifying this action event.
245 * This method is useful for event-logging and for debugging.
246 *
247 * @return a string identifying the event and its associated command
248 */
249 public String paramString() {
250 String typeStr;
251 switch(id) {
252 case ACTION_PERFORMED:
253 typeStr = "ACTION_PERFORMED";
254 break;
255 default:
256 typeStr = "unknown type";
257 }
258 return typeStr + ",cmd="+actionCommand+",when="+when+",modifiers="+
259 KeyEvent.getKeyModifiersText(modifiers);
260 }
261}