1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
14 * @author Toni Kalajainen
16 package org.simantics.scenegraph.g2d.events;
18 import java.awt.datatransfer.Transferable;
19 import java.awt.event.InputEvent;
20 import java.awt.geom.Point2D;
23 * TODO Remove hold time
25 * @see MouseClickEvent
28 * @author Toni Kalajainen
30 public abstract class MouseEvent extends Event {
32 private static final long serialVersionUID = -2090477028406944454L;
34 public static final int LEFT_BUTTON = 1;
35 public static final int RIGHT_BUTTON = 2;
36 public static final int MIDDLE_BUTTON = 3;
38 /** Time for mouse press + mouse release to be interpreted as mouse click */
39 public static final long CLICK_TIME = 250;
41 public static final int LEFT_MASK = 1 << (LEFT_BUTTON - 1);
42 public static final int RIGHT_MASK = 1 << (RIGHT_BUTTON - 1);
43 public static final int MIDDLE_MASK = 1 << (MIDDLE_BUTTON - 1);
44 public static final int ALL_BUTTONS_MASK = LEFT_MASK | RIGHT_MASK | MIDDLE_MASK;
46 public static final int CTRL_MASK = InputEvent.CTRL_DOWN_MASK;
47 public static final int ALT_MASK = InputEvent.ALT_DOWN_MASK;
48 public static final int ALT_GRAPH_MASK = InputEvent.ALT_GRAPH_DOWN_MASK;
49 public static final int SHIFT_MASK = InputEvent.SHIFT_DOWN_MASK;
50 public static final int ALL_MODIFIERS_MASK = CTRL_MASK | ALT_MASK | ALT_GRAPH_MASK | SHIFT_MASK;
53 * The identifier of the mouse of this event.
56 public final int mouseId;
58 /** Position in the coordinate system of the UI component */
59 public final Point2D controlPosition;
61 /** Position in the coordinate system of the screen */
62 public final Point2D screenPosition;
65 * The status of mouse buttons
70 public final int buttons;
73 * The state of the keyboard modifier keys at the time the event was
77 * @see #ALT_GRAPH_MASK
83 private MouseEvent(Object context, long time, int mouseId, int buttons, int stateMask,
84 Point2D controlPosition, Point2D screenPosition) {
86 this.mouseId = mouseId;
87 this.buttons = buttons;
88 this.controlPosition = controlPosition;
89 this.screenPosition = screenPosition;
90 this.stateMask = stateMask;
93 public boolean hasAnyButton(int mask) {
94 return (buttons & mask) != 0;
97 public boolean hasAllButtons(int mask) {
98 return (buttons & mask) == mask;
101 public boolean hasAnyModifier(int mask) {
102 return (stateMask & mask) != 0;
105 public boolean hasAllModifiers(int mask) {
106 return (stateMask & mask) == mask;
110 * Returns whether or not the Shift modifier is down on this event.
112 public boolean isShiftDown() {
113 return (stateMask & MouseEvent.SHIFT_MASK) != 0;
117 * Returns whether or not the Control modifier is down on this event.
119 public boolean isControlDown() {
120 return (stateMask & MouseEvent.CTRL_MASK) != 0;
124 * Returns whether or not the Alt modifier is down on this event.
126 public boolean isAltDown() {
127 return (stateMask & MouseEvent.ALT_MASK) != 0;
131 * Returns whether or not the Alt Graph modifier is down on this event.
133 public boolean isAltGraphDown() {
134 return (stateMask & MouseEvent.ALT_GRAPH_MASK) != 0;
137 public abstract static class MouseButtonEvent extends MouseEvent {
139 private static final long serialVersionUID = 3540032494506535841L;
142 * button of this event
144 public final int button;
146 public MouseButtonEvent(Object context, long time, int mouseId, int buttons, int stateMask,
147 int button, Point2D controlPosition, Point2D screenPosition) {
148 super(context, time, mouseId, buttons, stateMask, controlPosition,screenPosition);
149 this.button = button;
153 public static class MouseWheelMovedEvent extends MouseEvent {
155 private static final long serialVersionUID = -7896477913481842708L;
158 * Constant representing scrolling by "units" (like scrolling with the
161 * @see #getScrollType
163 public static final int WHEEL_UNIT_SCROLL = 0;
166 * Constant representing scrolling by a "block" (like scrolling
167 * with page-up, page-down keys)
169 * @see #getScrollType
171 public static final int WHEEL_BLOCK_SCROLL = 1;
174 * Indicates what sort of scrolling should take place in response to this
175 * event, based on platform settings. Legal values are:
177 * <li> WHEEL_UNIT_SCROLL
178 * <li> WHEEL_BLOCK_SCROLL
181 * @see #getScrollType
183 public final int scrollType;
186 * Only valid for scrollType WHEEL_UNIT_SCROLL.
187 * Indicates number of units that should be scrolled per
188 * click of mouse wheel rotation, based on platform settings.
190 * @see #getScrollAmount
191 * @see #getScrollType
193 public final int scrollAmount;
196 * Indicates how far the mouse wheel was rotated.
198 * @see #getWheelRotation
200 public final int wheelRotation;
202 public MouseWheelMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
203 Point2D controlPosition, Point2D screenPosition, int scrollType, int scrollAmount,
205 super(context, time, mouseId, buttons, stateMask, controlPosition,screenPosition);
206 this.scrollType = scrollType;
207 this.scrollAmount = scrollAmount;
208 this.wheelRotation = wheelRotation;
213 public static class MouseButtonPressedEvent extends MouseButtonEvent {
215 private static final long serialVersionUID = -4687294674299429690L;
217 public MouseButtonPressedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
218 int button, Point2D controlPosition, Point2D screenPosition) {
219 super(context, time, mouseId, buttons, stateMask, button, controlPosition,screenPosition);
224 public static class MouseButtonReleasedEvent extends MouseButtonEvent {
226 private static final long serialVersionUID = -2303672225339491858L;
228 /** Time in milliseconds how long the button was held down */
229 public final long holdTime;
231 public MouseButtonReleasedEvent(Object context, long time,
232 int mouseId, int buttons, int stateMask, int button, long holdTime,
233 Point2D controlPosition, Point2D screenPosition) {
234 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
235 this.holdTime = holdTime;
239 public static class MouseMovedEvent extends MouseEvent {
240 private static final long serialVersionUID = 1463958776335678155L;
242 public MouseMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
243 Point2D controlPosition, Point2D screenPosition) {
244 super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);
248 public static class MouseEnterEvent extends MouseEvent {
249 private static final long serialVersionUID = 6074648747865556588L;
251 public MouseEnterEvent(Object context, long time, int mouseId, int buttons, int stateMask,
252 Point2D controlPosition, Point2D screenPosition) {
253 super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);
257 public static class MouseExitEvent extends MouseEvent {
258 private static final long serialVersionUID = 8596801599996844789L;
260 public MouseExitEvent(Object context, long time, int mouseId, int buttons, int stateMask,
261 Point2D controlPosition, Point2D screenPosition) {
262 super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);
266 public static class MouseDoubleClickedEvent extends MouseButtonEvent {
267 private static final long serialVersionUID = -8046967155607105912L;
269 public MouseDoubleClickedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
270 int button, Point2D controlPosition, Point2D screenPosition) {
271 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
275 public static class MouseClickEvent extends MouseButtonEvent {
276 private static final long serialVersionUID = -1737712792090986607L;
278 * Indicates the number of quick consecutive clicks of
281 public final int clickCount;
283 public MouseClickEvent(Object context, long time, int mouseId, int buttons, int stateMask,
284 int button, int clickCount, Point2D controlPosition, Point2D screenPosition) {
285 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
286 this.clickCount = clickCount;
290 public static class MouseDragBegin extends MouseButtonEvent {
291 private static final long serialVersionUID = 3882552912240236450L;
292 public final Point2D startCanvasPos;
293 public final Point2D startControlPos;
294 public Transferable transferable = null;
295 public MouseDragBegin(Object context, long time, int mouseId, int buttons, int stateMask,
296 int button, Point2D startCanvasPos, Point2D startControlPos, Point2D controlPosition, Point2D screenPosition) {
297 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
298 this.startCanvasPos = startCanvasPos;
299 this.startControlPos = startControlPos;
304 * Spans button bit mask into individual button ids (starting from 1)
308 public static int[] getButtonIds(int buttons)
312 for (int i=0; i<32; i++)
313 if ((buttons & (1<<i))!=0)
315 int result[] = new int[size];
317 for (int i=0; i<32; i++)
318 if ((buttons & (1<<i))!=0)
324 public String toString() {
325 return super.toString() + "[mouse#=" + mouseId + ", control pos=" + controlPosition + ", screen pos="
326 + screenPosition + ", buttons=" + Integer.toBinaryString(buttons) + ", stateMask=0x"
327 + Integer.toHexString(stateMask) + "]";