]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/events/adapter/AWTRemoteEventAdapter.java
Merge commit 'd186091'
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / events / adapter / AWTRemoteEventAdapter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.scenegraph.g2d.events.adapter;\r
13 \r
14 import java.awt.AWTEvent;\r
15 import java.awt.event.AWTEventListener;\r
16 import java.awt.event.InputEvent;\r
17 import java.awt.event.KeyEvent;\r
18 import java.awt.event.KeyListener;\r
19 import java.awt.event.MouseEvent;\r
20 import java.awt.event.MouseListener;\r
21 import java.awt.event.MouseMotionListener;\r
22 import java.awt.event.MouseWheelEvent;\r
23 import java.awt.event.MouseWheelListener;\r
24 import java.awt.geom.Point2D;\r
25 import org.simantics.scenegraph.g2d.events.IEventHandler;\r
26 import org.simantics.scenegraph.g2d.events.IEventQueue;\r
27 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
28 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;\r
29 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
30 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;\r
31 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
32 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseEnterEvent;\r
33 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseExitEvent;\r
34 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
35 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseWheelMovedEvent;\r
36 import org.simantics.scenegraph.g2d.events.adapter.AbstractEventAdapter;\r
37 \r
38 \r
39 /**\r
40  * Listens to objectinput stream and reads AWT events\r
41  * Adapts AWT events into G2D events.\r
42  * \r
43  * Sends adapted events to IEventHandler\r
44  */\r
45 public class AWTRemoteEventAdapter extends AbstractEventAdapter implements AWTEventListener, MouseListener, MouseMotionListener, MouseWheelListener, KeyListener {\r
46 \r
47     /** Mouse id of the default mouse */\r
48     public static final int MOUSE_ID     = 0;\r
49 \r
50     private long[]          pressTime    = new long[5];\r
51     int                     buttonStatus = 0;\r
52 \r
53     /**\r
54      * create new adapter\r
55      * @param input the input stream where the events are read from\r
56      * @param sender the sender field in the events\r
57      * @param delegator the target of the adapted events\r
58      */\r
59     public AWTRemoteEventAdapter(Object sender, IEventHandler delegator) {\r
60         super(sender, delegator);\r
61     }\r
62 \r
63     /**\r
64      * create new adapter\r
65      * @param sender the sender field in the events\r
66      * @param queue\r
67      */\r
68     public AWTRemoteEventAdapter(Object sender, IEventQueue queue) {\r
69         super(sender, queue);\r
70     }   \r
71 \r
72     private Point2D getControlPosition(MouseEvent e)\r
73     {\r
74         return new Point2D.Double(e.getX(), e.getY());\r
75     }\r
76 \r
77     private Point2D getScreenPosition(MouseEvent e)\r
78     {\r
79         java.awt.Point p = e.getLocationOnScreen();\r
80         return new Point2D.Double(p.x, p.y);\r
81     }\r
82 \r
83     private int getMouseButton(MouseEvent e)\r
84     {\r
85         int awtMouseButton = e.getButton();\r
86         if (awtMouseButton == MouseEvent.BUTTON1)\r
87             return org.simantics.scenegraph.g2d.events.MouseEvent.LEFT_BUTTON;\r
88         if (awtMouseButton == MouseEvent.BUTTON2)\r
89             return org.simantics.scenegraph.g2d.events.MouseEvent.MIDDLE_BUTTON;\r
90         if (awtMouseButton == MouseEvent.BUTTON3)\r
91             return org.simantics.scenegraph.g2d.events.MouseEvent.RIGHT_BUTTON;\r
92         return awtMouseButton;\r
93     }\r
94 \r
95     private static int getStateMask(MouseEvent e) {\r
96         int modifiers = e.getModifiersEx();\r
97         int stateMask = 0;\r
98         if((modifiers & InputEvent.CTRL_DOWN_MASK) != 0)\r
99             stateMask |= org.simantics.scenegraph.g2d.events.MouseEvent.CTRL_MASK;\r
100         if((modifiers & InputEvent.ALT_DOWN_MASK) != 0)\r
101             stateMask |= org.simantics.scenegraph.g2d.events.MouseEvent.ALT_MASK;\r
102         if((modifiers & InputEvent.ALT_GRAPH_DOWN_MASK) != 0)\r
103             stateMask |= org.simantics.scenegraph.g2d.events.MouseEvent.ALT_GRAPH_MASK;\r
104         if((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0)\r
105             stateMask |= org.simantics.scenegraph.g2d.events.MouseEvent.SHIFT_MASK;\r
106         return stateMask;\r
107     }\r
108 \r
109 \r
110     public void mouseEntered(MouseEvent e) {\r
111         buttonStatus = 0;\r
112         MouseEnterEvent me = new MouseEnterEvent(sender, e.getWhen(), MOUSE_ID, buttonStatus, getStateMask(e), getControlPosition(e), getScreenPosition(e)); \r
113         handleEvent(me);\r
114     }\r
115 \r
116 \r
117     public void mouseExited(MouseEvent e) {\r
118         MouseExitEvent me = new MouseExitEvent(sender, e.getWhen(), MOUSE_ID, buttonStatus, getStateMask(e), getControlPosition(e), getScreenPosition(e));\r
119         handleEvent(me);\r
120     }\r
121 \r
122 \r
123     public void mousePressed(MouseEvent e) {\r
124         //System.out.println("AWT mouse pressed: " + e);\r
125         int mouseButton = getMouseButton(e);\r
126         if (mouseButton<=pressTime.length) pressTime[mouseButton-1] = e.getWhen();\r
127         buttonStatus |= 1 << (mouseButton-1);\r
128 \r
129         handleEvent(new MouseButtonPressedEvent(sender, e.getWhen(), MOUSE_ID,\r
130                 buttonStatus, getStateMask(e), mouseButton, getControlPosition(e), getScreenPosition(e)));\r
131         if (e.getClickCount() == 2) {\r
132             handleEvent(new MouseDoubleClickedEvent(sender, e.getWhen(),\r
133                     MOUSE_ID, buttonStatus, getStateMask(e), mouseButton, getControlPosition(e), getScreenPosition(e)));\r
134         }\r
135     }\r
136 \r
137 \r
138     public void mouseReleased(MouseEvent e) {\r
139         //System.out.println("AWT mouse released: " + e);\r
140         int mouseButton = getMouseButton(e);\r
141         long holdTime = Long.MAX_VALUE;\r
142         if (mouseButton<=pressTime.length) \r
143             holdTime = e.getWhen() - pressTime[mouseButton-1];\r
144 \r
145         buttonStatus &=~ (1<<(mouseButton-1));  \r
146         MouseButtonReleasedEvent me = new MouseButtonReleasedEvent(sender, e.getWhen(), MOUSE_ID, buttonStatus, getStateMask(e), mouseButton, holdTime, getControlPosition(e), getScreenPosition(e));\r
147         handleEvent(me);\r
148     }\r
149 \r
150 \r
151     public void mouseDragged(MouseEvent e) {\r
152         MouseMovedEvent me = new MouseMovedEvent(sender, e.getWhen(), MOUSE_ID, buttonStatus, getStateMask(e), getControlPosition(e), getScreenPosition(e));\r
153         handleEvent(me);\r
154     }\r
155 \r
156 \r
157     public void mouseMoved(MouseEvent e) {\r
158         MouseMovedEvent me = new MouseMovedEvent(sender, e.getWhen(), MOUSE_ID, buttonStatus, getStateMask(e), getControlPosition(e), getScreenPosition(e));\r
159         handleEvent(me);\r
160     }\r
161 \r
162 \r
163     public void mouseWheelMoved(MouseWheelEvent e) {\r
164         int wheelRotation = 0;\r
165         if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {\r
166             wheelRotation = -e.getUnitsToScroll();\r
167         } else {\r
168             wheelRotation = -e.getWheelRotation();\r
169         }\r
170         handleEvent(new MouseWheelMovedEvent(\r
171                 sender, e.getWhen(), MOUSE_ID, buttonStatus, getStateMask(e), getControlPosition(e), getScreenPosition(e),\r
172                 e.getScrollType(), e.getScrollAmount(), wheelRotation\r
173                 ));\r
174     }\r
175 \r
176 \r
177     public void mouseClicked(MouseEvent e) {\r
178     }\r
179 \r
180 \r
181     public void keyPressed(KeyEvent e) {\r
182         handleEvent(\r
183                 new KeyPressedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));\r
184     }\r
185 \r
186 \r
187     public void keyReleased(KeyEvent e) {\r
188         handleEvent(\r
189                 new KeyReleasedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));\r
190     }\r
191 \r
192 \r
193     public void keyTyped(KeyEvent e) {\r
194 //      System.out.println("keyTyped("+e+")");\r
195 //        handleEvent(\r
196 //                new KeyTypedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));\r
197     }\r
198 \r
199 \r
200     public void eventDispatched(AWTEvent event) {\r
201         if(event.getID() == KeyEvent.KEY_PRESSED) {\r
202             keyPressed((KeyEvent)event);\r
203         }\r
204         if(event.getID() == KeyEvent.KEY_RELEASED) {\r
205             keyReleased((KeyEvent)event);\r
206         }\r
207         if(event.getID() == KeyEvent.KEY_TYPED) {\r
208             keyTyped((KeyEvent)event);\r
209         }\r
210 \r
211         if(event.getID() == MouseEvent.MOUSE_MOVED) {\r
212             mouseMoved((MouseEvent)event);\r
213         }\r
214         if(event.getID() == MouseEvent.MOUSE_DRAGGED) {\r
215             mouseDragged((MouseEvent)event);\r
216         }\r
217         if(event.getID() == MouseEvent.MOUSE_PRESSED) {\r
218             mousePressed((MouseEvent)event);\r
219         }\r
220         if(event.getID() == MouseEvent.MOUSE_RELEASED) {\r
221             mouseReleased((MouseEvent)event);\r
222         }\r
223         if(event.getID() == MouseEvent.MOUSE_CLICKED) {\r
224             mouseClicked((MouseEvent)event);\r
225         }\r
226         if(event.getID() == MouseEvent.MOUSE_WHEEL) {\r
227             mouseWheelMoved((MouseWheelEvent)event);\r
228         }\r
229         if(event.getID() == MouseEvent.MOUSE_ENTERED) {\r
230             mouseEntered((MouseEvent)event);\r
231         }\r
232         if(event.getID() == MouseEvent.MOUSE_EXITED) {\r
233             mouseExited((MouseEvent)event);\r
234         }\r
235     }\r
236 }\r