001: /**
002: *
003: */package net.refractions.udig.project.ui.internal.render.displayAdapter.impl;
004:
005: import java.awt.Dimension;
006:
007: import net.refractions.udig.project.render.displayAdapter.MapDisplayEvent;
008: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
009: import net.refractions.udig.project.ui.internal.Trace;
010: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
011: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseWheelEvent;
012: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.widgets.Event;
016: import org.eclipse.swt.widgets.Listener;
017:
018: public class EventHandler implements Listener {
019: EventJob eventJob;
020:
021: ViewportPane pane;
022:
023: // ControlListener, MouseListener, MouseMoveListener, MouseTrackListener {
024:
025: /**
026: * @param java
027: */
028: public EventHandler(ViewportPane pane, EventJob eventJob) {
029: this .eventJob = eventJob;
030: this .pane = pane;
031: }
032:
033: /**
034: * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
035: */
036: public void handleEvent(Event event) {
037: switch (event.type) {
038: case SWT.MouseMove:
039: mouseMove(event);
040: break;
041: case SWT.MouseDown:
042: mouseDown(event);
043: ProjectUIPlugin.trace(Trace.VIEWPORT, getClass(),
044: "mouse down", null); //$NON-NLS-1$
045: break;
046: case SWT.MouseEnter:
047: mouseEnter(event);
048: break;
049: case SWT.MouseExit:
050: mouseExit(event);
051: break;
052: case SWT.MouseHover:
053: mouseHover(event);
054: break;
055: case SWT.MouseUp:
056: mouseUp(event);
057: ProjectUIPlugin.trace(Trace.VIEWPORT, getClass(),
058: "mouse up", null); //$NON-NLS-1$
059: break;
060: case SWT.MouseWheel:
061: mouseWheel(event);
062: break;
063: case SWT.Resize:
064: controlResized(event);
065: case SWT.KeyDown:
066: // System.out.println("keydown");
067: }
068: }
069:
070: /**
071: * TODO summary sentence for mouseWheel ...
072: *
073: * @param event
074: */
075: private void mouseWheel(Event e) {
076: MapMouseEvent m = new MapMouseWheelEvent(pane, e.x, e.y,
077: e.stateMask, getButtonsDown(e), getButton(e.button),
078: e.count);
079: eventJob.fire(EventJob.WHEEL, m);
080: }
081:
082: Dimension size;
083:
084: private long scheduledTime;
085:
086: /**
087: * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
088: */
089: public void controlResized(final Event e) {
090: if (e == null)
091: return;
092: synchronized (this ) {
093: scheduledTime = System.currentTimeMillis() + 400;
094: }
095: e.display.timerExec(500, new Runnable() {
096: public void run() {
097: long currentTimeMillis = System.currentTimeMillis();
098: long l;
099: synchronized (EventHandler.this ) {
100: l = scheduledTime;
101: }
102: if (l <= currentTimeMillis) {
103: eventJob.fire(EventJob.RESIZED,
104: new MapDisplayEvent(pane, size, pane
105: .getDisplaySize()));
106: size = pane.getDisplaySize();
107: }
108: }
109: });
110:
111: }
112:
113: /**
114: * @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
115: */
116: public void mouseDoubleClick(Event e) {
117: MapMouseEvent m = createMapMouseEvent(e);
118: m = new MapMouseEvent(pane, m.x, m.y, m.modifiers, m.buttons
119: | m.button, m.button);
120: eventJob.fire(EventJob.DOUBLE_CLICK, m);
121: }
122:
123: private MapMouseEvent createMapMouseEvent(Event e) {
124: return new MapMouseEvent(pane, e.x, e.y, e.stateMask,
125: getButtonsDown(e), getButton(e.button));
126: }
127:
128: private int getButtonsDown(Event e) {
129: int button1 = (e.stateMask & SWT.BUTTON1) != 0 ? 1 : -1;
130: int button2 = (e.stateMask & SWT.BUTTON2) != 0 ? 2 : -1;
131: int button3 = (e.stateMask & SWT.BUTTON3) != 0 ? 3 : -1;
132:
133: return getButton(button1) | getButton(button2)
134: | getButton(button3);
135: }
136:
137: private int getButton(int button) {
138: int state = 0;
139: if (button == 1)
140: state = state | MapMouseEvent.BUTTON1;
141: if (button == 2)
142: state = state | MapMouseEvent.BUTTON2;
143: if (button == 3)
144: state = state | MapMouseEvent.BUTTON3;
145: return state;
146: }
147:
148: /**
149: * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
150: */
151: public void mouseDown(Event e) {
152: MapMouseEvent m = createMapMouseEvent(e);
153: m = new MapMouseEvent(pane, m.x, m.y, m.modifiers, m.buttons
154: | m.button, m.button);
155: eventJob.fire(EventJob.PRESSED, m);
156: }
157:
158: /**
159: * @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
160: */
161: public void mouseUp(Event e) {
162: MapMouseEvent m = createMapMouseEvent(e);
163: if ((m.button & m.buttons) != 0)
164: m = new MapMouseEvent(m.source, m.x, m.y, m.modifiers,
165: m.buttons ^ m.button, m.button);
166: eventJob.fire(EventJob.RELEASED, m);
167: }
168:
169: /**
170: * @see org.eclipse.swt.events.MouseMoveListener#mouseMove(org.eclipse.swt.events.MouseEvent)
171: */
172: public void mouseMove(Event e) {
173: MapMouseEvent m = createMapMouseEvent(e);
174: if (m.buttons == 0)
175: eventJob.fire(EventJob.MOVED, m);
176: else {
177: eventJob.fire(EventJob.DRAGGED, m);
178: }
179: }
180:
181: /**
182: * @see org.eclipse.swt.events.MouseTrackListener#mouseEnter(org.eclipse.swt.events.MouseEvent)
183: */
184: public void mouseEnter(Event e) {
185: MapMouseEvent m = createMapMouseEvent(e);
186: eventJob.fire(EventJob.ENTERED, m);
187: }
188:
189: /**
190: * @see org.eclipse.swt.events.MouseTrackListener#mouseExit(org.eclipse.swt.events.MouseEvent)
191: */
192: public void mouseExit(Event e) {
193: MapMouseEvent m = createMapMouseEvent(e);
194: eventJob.fire(EventJob.EXITED, m);
195: }
196:
197: /**
198: * @see org.eclipse.swt.events.MouseTrackListener#mouseHover(org.eclipse.swt.events.MouseEvent)
199: */
200: public void mouseHover(Event e) {
201: MapMouseEvent m = createMapMouseEvent(e);
202: eventJob.fire(EventJob.HOVERED, m);
203: }
204: }
|