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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.scenegraph.g2d.events.adapter;
\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
40 * Listens to objectinput stream and reads AWT events
\r
41 * Adapts AWT events into G2D events.
\r
43 * Sends adapted events to IEventHandler
\r
45 public class AWTRemoteEventAdapter extends AbstractEventAdapter implements AWTEventListener, MouseListener, MouseMotionListener, MouseWheelListener, KeyListener {
\r
47 /** Mouse id of the default mouse */
\r
48 public static final int MOUSE_ID = 0;
\r
50 private long[] pressTime = new long[5];
\r
51 int buttonStatus = 0;
\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
59 public AWTRemoteEventAdapter(Object sender, IEventHandler delegator) {
\r
60 super(sender, delegator);
\r
64 * create new adapter
\r
65 * @param sender the sender field in the events
\r
68 public AWTRemoteEventAdapter(Object sender, IEventQueue queue) {
\r
69 super(sender, queue);
\r
72 private Point2D getControlPosition(MouseEvent e)
\r
74 return new Point2D.Double(e.getX(), e.getY());
\r
77 private Point2D getScreenPosition(MouseEvent e)
\r
79 java.awt.Point p = e.getLocationOnScreen();
\r
80 return new Point2D.Double(p.x, p.y);
\r
83 private int getMouseButton(MouseEvent e)
\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
95 private static int getStateMask(MouseEvent e) {
\r
96 int modifiers = e.getModifiersEx();
\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
110 public void mouseEntered(MouseEvent e) {
\r
112 MouseEnterEvent me = new MouseEnterEvent(sender, e.getWhen(), MOUSE_ID, buttonStatus, getStateMask(e), getControlPosition(e), getScreenPosition(e));
\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
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
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
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
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
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
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
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
168 wheelRotation = -e.getWheelRotation();
\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
177 public void mouseClicked(MouseEvent e) {
\r
181 public void keyPressed(KeyEvent e) {
\r
183 new KeyPressedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));
\r
187 public void keyReleased(KeyEvent e) {
\r
189 new KeyReleasedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));
\r
193 public void keyTyped(KeyEvent e) {
\r
194 // System.out.println("keyTyped("+e+")");
\r
196 // new KeyTypedEvent(sender, e.getWhen(), e.getKeyChar(), e.getKeyCode(), e.getModifiers()));
\r
200 public void eventDispatched(AWTEvent event) {
\r
201 if(event.getID() == KeyEvent.KEY_PRESSED) {
\r
202 keyPressed((KeyEvent)event);
\r
204 if(event.getID() == KeyEvent.KEY_RELEASED) {
\r
205 keyReleased((KeyEvent)event);
\r
207 if(event.getID() == KeyEvent.KEY_TYPED) {
\r
208 keyTyped((KeyEvent)event);
\r
211 if(event.getID() == MouseEvent.MOUSE_MOVED) {
\r
212 mouseMoved((MouseEvent)event);
\r
214 if(event.getID() == MouseEvent.MOUSE_DRAGGED) {
\r
215 mouseDragged((MouseEvent)event);
\r
217 if(event.getID() == MouseEvent.MOUSE_PRESSED) {
\r
218 mousePressed((MouseEvent)event);
\r
220 if(event.getID() == MouseEvent.MOUSE_RELEASED) {
\r
221 mouseReleased((MouseEvent)event);
\r
223 if(event.getID() == MouseEvent.MOUSE_CLICKED) {
\r
224 mouseClicked((MouseEvent)event);
\r
226 if(event.getID() == MouseEvent.MOUSE_WHEEL) {
\r
227 mouseWheelMoved((MouseWheelEvent)event);
\r
229 if(event.getID() == MouseEvent.MOUSE_ENTERED) {
\r
230 mouseEntered((MouseEvent)event);
\r
232 if(event.getID() == MouseEvent.MOUSE_EXITED) {
\r
233 mouseExited((MouseEvent)event);
\r