]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/MouseEvent.java
Fixed all line endings of the repository
[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         /**
158          * Constant representing scrolling by "units" (like scrolling with the
159          * arrow keys)
160          * 
161          * @see #getScrollType
162          */
163         public static final int WHEEL_UNIT_SCROLL = 0;
164
165         /**
166          * Constant representing scrolling by a "block" (like scrolling
167          * with page-up, page-down keys)
168          *
169          * @see #getScrollType
170          */
171         public static final int WHEEL_BLOCK_SCROLL = 1;
172
173         /**
174          * Indicates what sort of scrolling should take place in response to this
175          * event, based on platform settings.  Legal values are:
176          * <ul>
177          * <li> WHEEL_UNIT_SCROLL
178          * <li> WHEEL_BLOCK_SCROLL
179          * </ul>
180          * 
181          * @see #getScrollType
182          */
183         public final int scrollType;
184
185         /**
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.
189          *
190          * @see #getScrollAmount
191          * @see #getScrollType
192          */
193         public final int scrollAmount;
194
195         /**
196          * Indicates how far the mouse wheel was rotated.
197          *
198          * @see #getWheelRotation
199          */
200         public final int wheelRotation;
201
202         public MouseWheelMovedEvent(Object context, long time, int mouseId, int buttons, int stateMask,
203                 Point2D controlPosition, Point2D screenPosition, int scrollType, int scrollAmount,
204                 int wheelRotation) {
205             super(context, time, mouseId, buttons, stateMask, controlPosition,screenPosition);
206             this.scrollType = scrollType;
207             this.scrollAmount = scrollAmount;
208             this.wheelRotation = wheelRotation;
209         }
210
211     }
212
213     public static class MouseButtonPressedEvent extends MouseButtonEvent {
214
215         private static final long serialVersionUID = -4687294674299429690L;
216
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);
220         }
221
222     }
223
224     public static class MouseButtonReleasedEvent extends MouseButtonEvent {
225
226         private static final long serialVersionUID = -2303672225339491858L;
227
228         /** Time in milliseconds how long the button was held down */
229         public final long holdTime;
230
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;
236         }
237     }
238
239     public static class MouseMovedEvent extends MouseEvent {
240         private static final long serialVersionUID = 1463958776335678155L;
241
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);
245         }
246     }
247
248     public static class MouseEnterEvent extends MouseEvent {
249         private static final long serialVersionUID = 6074648747865556588L;
250
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);
254         }
255     }
256
257     public static class MouseExitEvent extends MouseEvent {
258         private static final long serialVersionUID = 8596801599996844789L;
259
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);
263         }
264     }
265
266     public static class MouseDoubleClickedEvent extends MouseButtonEvent {
267         private static final long serialVersionUID = -8046967155607105912L;
268
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);
272         }
273     }
274
275     public static class MouseClickEvent extends MouseButtonEvent {
276         private static final long serialVersionUID = -1737712792090986607L;
277         /**
278          * Indicates the number of quick consecutive clicks of
279          * a mouse button.
280          */
281         public final int clickCount;
282     
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;
287         }
288     }
289
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;
300         }
301     }
302
303     /**
304      * Spans button bit mask into individual button ids (starting from 1)
305      * @param buttons
306      * @return
307      */
308     public static int[] getButtonIds(int buttons)
309     {
310         // calculate 1 bits
311         int size = 0;
312         for (int i=0; i<32; i++)
313             if ((buttons & (1<<i))!=0)
314                 size++;
315         int result[] = new int[size];
316         int j=0;
317         for (int i=0; i<32; i++)
318             if ((buttons & (1<<i))!=0)
319                 result[j++] = i+1;
320         return result;
321     }
322
323     @Override
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) + "]";
328     }
329
330 }