]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/MouseEvent.java
Duplicate MouseWheelMovedEvent was not actually fixed
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / events / MouseEvent.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  *
14  * @author Toni Kalajainen
15  */
16 package org.simantics.scenegraph.g2d.events;
17
18 import java.awt.datatransfer.Transferable;
19 import java.awt.event.InputEvent;
20 import java.awt.geom.Point2D;
21
22 /**
23  * TODO Remove hold time
24  * @see Event
25  * @see MouseClickEvent
26  * @see MouseDragBegin
27  * 
28  * @author Toni Kalajainen
29  */
30 public abstract class MouseEvent extends Event {
31
32     private static final long serialVersionUID = -2090477028406944454L;
33
34     public static final int   LEFT_BUTTON        = 1;
35     public static final int   RIGHT_BUTTON       = 2;
36     public static final int   MIDDLE_BUTTON      = 3;
37
38     /** Time for mouse press + mouse release to be interpreted as mouse click */
39     public static final long  CLICK_TIME         = 250;
40
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;
45
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;
51
52     /**
53      * The identifier of the mouse of this event.
54      * 0 = the main mouse.
55      */
56     public final int mouseId;
57
58     /** Position in the coordinate system of the UI component */
59     public final Point2D controlPosition;
60
61     /** Position in the coordinate system of the screen */
62     public final Point2D screenPosition;
63
64     /**
65      * The status of mouse buttons
66      * @see #LEFT_MASK
67      * @see #MIDDLE_MASK
68      * @see #RIGHT_MASK
69      */
70     public final int buttons;
71
72     /**
73      * The state of the keyboard modifier keys at the time the event was
74      * generated.
75      * 
76      * @see #ALT_MASK
77      * @see #ALT_GRAPH_MASK
78      * @see #CTRL_MASK
79      * @see #SHIFT_MASK
80      */
81     public int stateMask;
82
83     private MouseEvent(Object context, long time, int mouseId, int buttons, int stateMask,
84             Point2D controlPosition, Point2D screenPosition) {
85         super(context, time);
86         this.mouseId = mouseId;
87         this.buttons = buttons;
88         this.controlPosition = controlPosition;
89         this.screenPosition = screenPosition;
90         this.stateMask = stateMask;
91     }
92
93     public boolean hasAnyButton(int mask) {
94         return (buttons & mask) != 0;
95     }
96
97     public boolean hasAllButtons(int mask) {
98         return (buttons & mask) == mask;
99     }
100
101     public boolean hasAnyModifier(int mask) {
102         return (stateMask & mask) != 0;
103     }
104
105     public boolean hasAllModifiers(int mask) {
106         return (stateMask & mask) == mask;
107     }
108
109     /**
110      * Returns whether or not the Shift modifier is down on this event.
111      */
112     public boolean isShiftDown() {
113         return (stateMask & MouseEvent.SHIFT_MASK) != 0;
114     }
115
116     /**
117      * Returns whether or not the Control modifier is down on this event.
118      */
119     public boolean isControlDown() {
120         return (stateMask & MouseEvent.CTRL_MASK) != 0;
121     }
122
123     /**
124      * Returns whether or not the Alt modifier is down on this event.
125      */
126     public boolean isAltDown() {
127         return (stateMask & MouseEvent.ALT_MASK) != 0;
128     }
129
130     /**
131      * Returns whether or not the Alt Graph modifier is down on this event.
132      */
133     public boolean isAltGraphDown() {
134         return (stateMask & MouseEvent.ALT_GRAPH_MASK) != 0;
135     }
136
137     public abstract static class MouseButtonEvent extends MouseEvent {
138
139         private static final long serialVersionUID = 3540032494506535841L;
140
141         /**
142          * button of this event
143          */
144         public final int button;
145
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;
150         }
151     }
152
153     public static class MouseWheelMovedEvent extends MouseEvent {
154
155         private static final long serialVersionUID = -7896477913481842708L;
156
157         public static final int SCROLL_AMOUNT_ZERO = 0;
158
159         /**
160          * Constant representing scrolling by "units" (like scrolling with the
161          * arrow keys)
162          * 
163          * @see #getScrollType
164          */
165         public static final int WHEEL_UNIT_SCROLL = 0;
166
167         /**
168          * Constant representing scrolling by a "block" (like scrolling
169          * with page-up, page-down keys)
170          *
171          * @see #getScrollType
172          */
173         public static final int WHEEL_BLOCK_SCROLL = 1;
174
175         /**
176          * Indicates what sort of scrolling should take place in response to this
177          * event, based on platform settings.  Legal values are:
178          * <ul>
179          * <li> WHEEL_UNIT_SCROLL
180          * <li> WHEEL_BLOCK_SCROLL
181          * </ul>
182          * 
183          * @see #getScrollType
184          */
185         public final int scrollType;
186
187         /**
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.
191          *
192          * @see #getScrollAmount
193          * @see #getScrollType
194          */
195         public final int scrollAmount;
196
197         /**
198          * Indicates how far the mouse wheel was rotated.
199          *
200          * @see #getWheelRotation
201          */
202         public final int wheelRotation;
203
204         public MouseWheelMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
205                 Point2D controlPosition, Point2D screenPosition, int scrollType, int scrollAmount,
206                 int wheelRotation) {
207             super(context, time, mouseId, buttons, stateMask, controlPosition,screenPosition);
208             this.scrollType = scrollType;
209             this.scrollAmount = scrollAmount;
210             this.wheelRotation = wheelRotation;
211         }
212
213     }
214
215     public static class MouseButtonPressedEvent extends MouseButtonEvent {
216
217         private static final long serialVersionUID = -4687294674299429690L;
218
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);
222         }
223
224     }
225
226     public static class MouseButtonReleasedEvent extends MouseButtonEvent {
227
228         private static final long serialVersionUID = -2303672225339491858L;
229
230         /** Time in milliseconds how long the button was held down */
231         public final long holdTime;
232
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;
238         }
239     }
240
241     public static class MouseMovedEvent extends MouseEvent {
242         private static final long serialVersionUID = 1463958776335678155L;
243
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);
247         }
248     }
249
250     public static class MouseEnterEvent extends MouseEvent {
251         private static final long serialVersionUID = 6074648747865556588L;
252
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);
256         }
257     }
258
259     public static class MouseExitEvent extends MouseEvent {
260         private static final long serialVersionUID = 8596801599996844789L;
261
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);
265         }
266     }
267
268     public static class MouseDoubleClickedEvent extends MouseButtonEvent {
269         private static final long serialVersionUID = -8046967155607105912L;
270
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);
274         }
275     }
276
277     public static class MouseClickEvent extends MouseButtonEvent {
278         private static final long serialVersionUID = -1737712792090986607L;
279         /**
280          * Indicates the number of quick consecutive clicks of
281          * a mouse button.
282          */
283         public final int clickCount;
284     
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;
289         }
290     }
291
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;
302         }
303     }
304
305     /**
306      * Spans button bit mask into individual button ids (starting from 1)
307      * @param buttons
308      * @return
309      */
310     public static int[] getButtonIds(int buttons)
311     {
312         // calculate 1 bits
313         int size = 0;
314         for (int i=0; i<32; i++)
315             if ((buttons & (1<<i))!=0)
316                 size++;
317         int result[] = new int[size];
318         int j=0;
319         for (int i=0; i<32; i++)
320             if ((buttons & (1<<i))!=0)
321                 result[j++] = i+1;
322         return result;
323     }
324
325     @Override
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) + "]";
330     }
331
332 }