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;
157 public static final int SCROLL_AMOUNT_ZERO = 0;
160 * Constant representing scrolling by "units" (like scrolling with the
163 * @see #getScrollType
165 public static final int WHEEL_UNIT_SCROLL = 0;
168 * Constant representing scrolling by a "block" (like scrolling
169 * with page-up, page-down keys)
171 * @see #getScrollType
173 public static final int WHEEL_BLOCK_SCROLL = 1;
176 * Indicates what sort of scrolling should take place in response to this
177 * event, based on platform settings. Legal values are:
179 * <li> WHEEL_UNIT_SCROLL
180 * <li> WHEEL_BLOCK_SCROLL
183 * @see #getScrollType
185 public final int scrollType;
188 * Only valid for scrollType WHEEL_UNIT_SCROLL.
189 * Indicates number of units that should be scrolled per
190 * click of mouse wheel rotation, based on platform settings.
192 * @see #getScrollAmount
193 * @see #getScrollType
195 public final int scrollAmount;
198 * Indicates how far the mouse wheel was rotated.
200 * @see #getWheelRotation
202 public final int wheelRotation;
204 public MouseWheelMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
205 Point2D controlPosition, Point2D screenPosition, int scrollType, int scrollAmount,
207 super(context, time, mouseId, buttons, stateMask, controlPosition,screenPosition);
208 this.scrollType = scrollType;
209 this.scrollAmount = scrollAmount;
210 this.wheelRotation = wheelRotation;
215 public static class MouseButtonPressedEvent extends MouseButtonEvent {
217 private static final long serialVersionUID = -4687294674299429690L;
219 public MouseButtonPressedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
220 int button, Point2D controlPosition, Point2D screenPosition) {
221 super(context, time, mouseId, buttons, stateMask, button, controlPosition,screenPosition);
226 public static class MouseButtonReleasedEvent extends MouseButtonEvent {
228 private static final long serialVersionUID = -2303672225339491858L;
230 /** Time in milliseconds how long the button was held down */
231 public final long holdTime;
233 public MouseButtonReleasedEvent(Object context, long time,
234 int mouseId, int buttons, int stateMask, int button, long holdTime,
235 Point2D controlPosition, Point2D screenPosition) {
236 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
237 this.holdTime = holdTime;
241 public static class MouseMovedEvent extends MouseEvent {
242 private static final long serialVersionUID = 1463958776335678155L;
244 public MouseMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
245 Point2D controlPosition, Point2D screenPosition) {
246 super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);
250 public static class MouseEnterEvent extends MouseEvent {
251 private static final long serialVersionUID = 6074648747865556588L;
253 public MouseEnterEvent(Object context, long time, int mouseId, int buttons, int stateMask,
254 Point2D controlPosition, Point2D screenPosition) {
255 super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);
259 public static class MouseExitEvent extends MouseEvent {
260 private static final long serialVersionUID = 8596801599996844789L;
262 public MouseExitEvent(Object context, long time, int mouseId, int buttons, int stateMask,
263 Point2D controlPosition, Point2D screenPosition) {
264 super(context, time, mouseId, buttons, stateMask, controlPosition, screenPosition);
268 public static class MouseDoubleClickedEvent extends MouseButtonEvent {
269 private static final long serialVersionUID = -8046967155607105912L;
271 public MouseDoubleClickedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
272 int button, Point2D controlPosition, Point2D screenPosition) {
273 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
277 public static class MouseClickEvent extends MouseButtonEvent {
278 private static final long serialVersionUID = -1737712792090986607L;
280 * Indicates the number of quick consecutive clicks of
283 public final int clickCount;
285 public MouseClickEvent(Object context, long time, int mouseId, int buttons, int stateMask,
286 int button, int clickCount, Point2D controlPosition, Point2D screenPosition) {
287 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
288 this.clickCount = clickCount;
292 public static class MouseDragBegin extends MouseButtonEvent {
293 private static final long serialVersionUID = 3882552912240236450L;
294 public final Point2D startCanvasPos;
295 public final Point2D startControlPos;
296 public Transferable transferable = null;
297 public MouseDragBegin(Object context, long time, int mouseId, int buttons, int stateMask,
298 int button, Point2D startCanvasPos, Point2D startControlPos, Point2D controlPosition, Point2D screenPosition) {
299 super(context, time, mouseId, buttons, stateMask, button, controlPosition, screenPosition);
300 this.startCanvasPos = startCanvasPos;
301 this.startControlPos = startControlPos;
306 * Spans button bit mask into individual button ids (starting from 1)
310 public static int[] getButtonIds(int buttons)
314 for (int i=0; i<32; i++)
315 if ((buttons & (1<<i))!=0)
317 int result[] = new int[size];
319 for (int i=0; i<32; i++)
320 if ((buttons & (1<<i))!=0)
326 public String toString() {
327 return super.toString() + "[mouse#=" + mouseId + ", control pos=" + controlPosition + ", screen pos="
328 + screenPosition + ", buttons=" + Integer.toBinaryString(buttons) + ", stateMask=0x"
329 + Integer.toHexString(stateMask) + "]";