blob: 45b1f87e40e389bec54153a2d8178b14ac910a30 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1996-2007 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.Event;
29import java.awt.Component;
30import java.awt.GraphicsEnvironment;
31import java.awt.Toolkit;
32import java.util.logging.Logger;
33import java.util.logging.Level;
34
35/**
36 * The root event class for all component-level input events.
37 *
38 * Input events are delivered to listeners before they are
39 * processed normally by the source where they originated.
40 * This allows listeners and component subclasses to "consume"
41 * the event so that the source will not process them in their
42 * default manner. For example, consuming mousePressed events
43 * on a Button component will prevent the Button from being
44 * activated.
45 *
46 * @author Carl Quinn
47 *
48 * @see KeyEvent
49 * @see KeyAdapter
50 * @see MouseEvent
51 * @see MouseAdapter
52 * @see MouseMotionAdapter
53 *
54 * @since 1.1
55 */
56public abstract class InputEvent extends ComponentEvent {
57 private static final Logger log = Logger.getLogger("java.awt.event.InputEvent");
58
59 /**
60 * The Shift key modifier constant.
61 * It is recommended that SHIFT_DOWN_MASK be used instead.
62 */
63 public static final int SHIFT_MASK = Event.SHIFT_MASK;
64
65 /**
66 * The Control key modifier constant.
67 * It is recommended that CTRL_DOWN_MASK be used instead.
68 */
69 public static final int CTRL_MASK = Event.CTRL_MASK;
70
71 /**
72 * The Meta key modifier constant.
73 * It is recommended that META_DOWN_MASK be used instead.
74 */
75 public static final int META_MASK = Event.META_MASK;
76
77 /**
78 * The Alt key modifier constant.
79 * It is recommended that ALT_DOWN_MASK be used instead.
80 */
81 public static final int ALT_MASK = Event.ALT_MASK;
82
83 /**
84 * The AltGraph key modifier constant.
85 */
86 public static final int ALT_GRAPH_MASK = 1 << 5;
87
88 /**
89 * The Mouse Button1 modifier constant.
90 * It is recommended that BUTTON1_DOWN_MASK be used instead.
91 */
92 public static final int BUTTON1_MASK = 1 << 4;
93
94 /**
95 * The Mouse Button2 modifier constant.
96 * It is recommended that BUTTON2_DOWN_MASK be used instead.
97 * Note that BUTTON2_MASK has the same value as ALT_MASK.
98 */
99 public static final int BUTTON2_MASK = Event.ALT_MASK;
100
101 /**
102 * The Mouse Button3 modifier constant.
103 * It is recommended that BUTTON3_DOWN_MASK be used instead.
104 * Note that BUTTON3_MASK has the same value as META_MASK.
105 */
106 public static final int BUTTON3_MASK = Event.META_MASK;
107
108 /**
109 * The Shift key extended modifier constant.
110 * @since 1.4
111 */
112 public static final int SHIFT_DOWN_MASK = 1 << 6;
113
114 /**
115 * The Control key extended modifier constant.
116 * @since 1.4
117 */
118 public static final int CTRL_DOWN_MASK = 1 << 7;
119
120 /**
121 * The Meta key extended modifier constant.
122 * @since 1.4
123 */
124 public static final int META_DOWN_MASK = 1 << 8;
125
126 /**
127 * The Alt key extended modifier constant.
128 * @since 1.4
129 */
130 public static final int ALT_DOWN_MASK = 1 << 9;
131
132 /**
133 * The Mouse Button1 extended modifier constant.
134 * @since 1.4
135 */
136 public static final int BUTTON1_DOWN_MASK = 1 << 10;
137
138 /**
139 * The Mouse Button2 extended modifier constant.
140 * @since 1.4
141 */
142 public static final int BUTTON2_DOWN_MASK = 1 << 11;
143
144 /**
145 * The Mouse Button3 extended modifier constant.
146 * @since 1.4
147 */
148 public static final int BUTTON3_DOWN_MASK = 1 << 12;
149
150 /**
151 * The AltGraph key extended modifier constant.
152 * @since 1.4
153 */
154 public static final int ALT_GRAPH_DOWN_MASK = 1 << 13;
155
156 // the constant below MUST be updated if any extra modifier
157 // bits are to be added!
158 // in fact, it is undesirable to add modifier bits
159 // to the same field as this may break applications
160 // see bug# 5066958
161
162 static final int FIRST_HIGH_BIT = 1 << 14;
163
164 static final int JDK_1_3_MODIFIERS = SHIFT_DOWN_MASK - 1;
165 static final int HIGH_MODIFIERS = ~( FIRST_HIGH_BIT - 1 );
166
167 /**
168 * The input event's Time stamp in UTC format. The time stamp
169 * indicates when the input event was created.
170 *
171 * @serial
172 * @see #getWhen()
173 */
174 long when;
175
176 /**
177 * The state of the modifier mask at the time the input
178 * event was fired.
179 *
180 * @serial
181 * @see #getModifiers()
182 * @see #getModifiersEx()
183 * @see java.awt.event.KeyEvent
184 * @see java.awt.event.MouseEvent
185 */
186 int modifiers;
187
188 /*
189 * A flag that indicates that this instance can be used to access
190 * the system clipboard.
191 */
192 private transient boolean canAccessSystemClipboard;
193
194 static {
195 /* ensure that the necessary native libraries are loaded */
196 NativeLibLoader.loadLibraries();
197 if (!GraphicsEnvironment.isHeadless()) {
198 initIDs();
199 }
200 }
201
202 /**
203 * Initialize JNI field and method IDs for fields that may be
204 accessed from C.
205 */
206 private static native void initIDs();
207
208 /**
209 * Constructs an InputEvent object with the specified source component,
210 * modifiers, and type.
211 * <p>Note that passing in an invalid <code>id</code> results in
212 * unspecified behavior. This method throws an
213 * <code>IllegalArgumentException</code> if <code>source</code>
214 * is <code>null</code>.
215 *
216 * @param source the object where the event originated
217 * @param id the event type
218 * @param when the time the event occurred
219 * @param modifiers represents the modifier keys and mouse buttons down
220 * while the event occurred
221 * @throws IllegalArgumentException if <code>source</code> is null
222 */
223 InputEvent(Component source, int id, long when, int modifiers) {
224 super(source, id);
225 this.when = when;
226 this.modifiers = modifiers;
227 canAccessSystemClipboard = canAccessSystemClipboard();
228 }
229
230 private boolean canAccessSystemClipboard() {
231 boolean b = false;
232
233 if (!GraphicsEnvironment.isHeadless()) {
234 SecurityManager sm = System.getSecurityManager();
235 if (sm != null) {
236 try {
237 sm.checkSystemClipboardAccess();
238 b = true;
239 } catch (SecurityException se) {
240 if (log.isLoggable(Level.FINE)) {
241 log.log(Level.FINE, "InputEvent.canAccessSystemClipboard() got SecurityException ", se);
242 }
243 }
244 } else {
245 b = true;
246 }
247 }
248
249 return b;
250 }
251
252 /**
253 * Returns whether or not the Shift modifier is down on this event.
254 */
255 public boolean isShiftDown() {
256 return (modifiers & SHIFT_MASK) != 0;
257 }
258
259 /**
260 * Returns whether or not the Control modifier is down on this event.
261 */
262 public boolean isControlDown() {
263 return (modifiers & CTRL_MASK) != 0;
264 }
265
266 /**
267 * Returns whether or not the Meta modifier is down on this event.
268 */
269 public boolean isMetaDown() {
270 return (modifiers & META_MASK) != 0;
271 }
272
273 /**
274 * Returns whether or not the Alt modifier is down on this event.
275 */
276 public boolean isAltDown() {
277 return (modifiers & ALT_MASK) != 0;
278 }
279
280 /**
281 * Returns whether or not the AltGraph modifier is down on this event.
282 */
283 public boolean isAltGraphDown() {
284 return (modifiers & ALT_GRAPH_MASK) != 0;
285 }
286
287 /**
288 * Returns the timestamp of when this event occurred.
289 */
290 public long getWhen() {
291 return when;
292 }
293
294 /**
295 * Returns the modifier mask for this event.
296 */
297 public int getModifiers() {
298 return modifiers & (JDK_1_3_MODIFIERS | HIGH_MODIFIERS);
299 }
300
301 /**
302 * Returns the extended modifier mask for this event.
303 * Extended modifiers represent the state of all modal keys,
304 * such as ALT, CTRL, META, and the mouse buttons just after
305 * the event occurred
306 * <P>
307 * For example, if the user presses <b>button 1</b> followed by
308 * <b>button 2</b>, and then releases them in the same order,
309 * the following sequence of events is generated:
310 * <PRE>
311 * <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK</code>
312 * <code>MOUSE_PRESSED</code>: <code>BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK</code>
313 * <code>MOUSE_RELEASED</code>: <code>BUTTON2_DOWN_MASK</code>
314 * <code>MOUSE_CLICKED</code>: <code>BUTTON2_DOWN_MASK</code>
315 * <code>MOUSE_RELEASED</code>:
316 * <code>MOUSE_CLICKED</code>:
317 * </PRE>
318 * <P>
319 * It is not recommended to compare the return value of this method
320 * using <code>==</code> because new modifiers can be added in the future.
321 * For example, the appropriate way to check that SHIFT and BUTTON1 are
322 * down, but CTRL is up is demonstrated by the following code:
323 * <PRE>
324 * int onmask = SHIFT_DOWN_MASK | BUTTON1_DOWN_MASK;
325 * int offmask = CTRL_DOWN_MASK;
326 * if ((event.getModifiersEx() & (onmask | offmask)) == onmask) {
327 * ...
328 * }
329 * </PRE>
330 * The above code will work even if new modifiers are added.
331 *
332 * @since 1.4
333 */
334 public int getModifiersEx() {
335 return modifiers & ~JDK_1_3_MODIFIERS;
336 }
337
338 /**
339 * Consumes this event so that it will not be processed
340 * in the default manner by the source which originated it.
341 */
342 public void consume() {
343 consumed = true;
344 }
345
346 /**
347 * Returns whether or not this event has been consumed.
348 * @see #consume
349 */
350 public boolean isConsumed() {
351 return consumed;
352 }
353
354 // state serialization compatibility with JDK 1.1
355 static final long serialVersionUID = -2482525981698309786L;
356
357 /**
358 * Returns a String describing the extended modifier keys and
359 * mouse buttons, such as "Shift", "Button1", or "Ctrl+Shift".
360 * These strings can be localized by changing the
361 * awt.properties file.
362 *
363 * @param modifiers a modifier mask describing the extended
364 * modifier keys and mouse buttons for the event
365 * @return a text description of the combination of extended
366 * modifier keys and mouse buttons that were held down
367 * during the event.
368 * @since 1.4
369 */
370 public static String getModifiersExText(int modifiers) {
371 StringBuilder buf = new StringBuilder();
372 if ((modifiers & InputEvent.META_DOWN_MASK) != 0) {
373 buf.append(Toolkit.getProperty("AWT.meta", "Meta"));
374 buf.append("+");
375 }
376 if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) {
377 buf.append(Toolkit.getProperty("AWT.control", "Ctrl"));
378 buf.append("+");
379 }
380 if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) {
381 buf.append(Toolkit.getProperty("AWT.alt", "Alt"));
382 buf.append("+");
383 }
384 if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) {
385 buf.append(Toolkit.getProperty("AWT.shift", "Shift"));
386 buf.append("+");
387 }
388 if ((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) {
389 buf.append(Toolkit.getProperty("AWT.altGraph", "Alt Graph"));
390 buf.append("+");
391 }
392 if ((modifiers & InputEvent.BUTTON1_DOWN_MASK) != 0) {
393 buf.append(Toolkit.getProperty("AWT.button1", "Button1"));
394 buf.append("+");
395 }
396 if ((modifiers & InputEvent.BUTTON2_DOWN_MASK) != 0) {
397 buf.append(Toolkit.getProperty("AWT.button2", "Button2"));
398 buf.append("+");
399 }
400 if ((modifiers & InputEvent.BUTTON3_DOWN_MASK) != 0) {
401 buf.append(Toolkit.getProperty("AWT.button3", "Button3"));
402 buf.append("+");
403 }
404 if (buf.length() > 0) {
405 buf.setLength(buf.length()-1); // remove trailing '+'
406 }
407 return buf.toString();
408 }
409}