001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit;
016:
017: import java.util.HashSet;
018: import java.util.LinkedList;
019: import java.util.Queue;
020: import java.util.Set;
021: import java.util.concurrent.ConcurrentLinkedQueue;
022:
023: import net.refractions.udig.project.ui.render.displayAdapter.MapMouseEvent;
024: import net.refractions.udig.project.ui.render.displayAdapter.ViewportPane;
025: import net.refractions.udig.tools.edit.support.Point;
026:
027: import org.eclipse.swt.SWT;
028: import org.eclipse.swt.widgets.Display;
029:
030: /**
031: * Keeps track of the what mouse events have happened. Must be in event thread to call this method.
032: *
033: * @author jones
034: * @since 1.1.0
035: */
036: public class MouseTracker {
037:
038: private static final int MAX_QUEUE_SIZE = 10;
039: private Point dragStarted;
040: private Queue<MapMouseEvent> previousEvents = new ConcurrentLinkedQueue<MapMouseEvent>();
041: private Point currentPoint;
042: private volatile Set<EventType> updateTriggers = new HashSet<EventType>();
043: private EditToolHandler handler;
044:
045: public MouseTracker(EditToolHandler handler2) {
046: this .handler = handler2;
047: }
048:
049: /**
050: * Called by EditToolhandler to updates the state.
051: *
052: * @param e The most recent event
053: * @param type the type of event
054: */
055: protected void updateState(MapMouseEvent e, EventType type) {
056: checkAccess();
057: previousEvents.add(e);
058:
059: if (previousEvents.size() > MAX_QUEUE_SIZE)
060: previousEvents.remove();
061:
062: switch (type) {
063: case PRESSED:
064: break;
065: case DRAGGED:
066: currentPoint = Point.valueOf(e.x, e.y);
067: break;
068: case DOUBLE_CLICK:
069:
070: break;
071: case ENTERED:
072:
073: break;
074: case EXITED:
075:
076: break;
077: case MOVED:
078: currentPoint = dragStarted = Point.valueOf(e.x, e.y);
079: break;
080: case RELEASED:
081: break;
082:
083: default:
084: break;
085: }
086:
087: if (updateTriggers.contains(type)) {
088: if (e.source instanceof ViewportPane) {
089: handler.repaint();
090: }
091: }
092: }
093:
094: private void checkAccess() {
095: if (Display.getCurrent() == null)
096: SWT.error(SWT.ERROR_THREAD_INVALID_ACCESS);
097: }
098:
099: /**
100: * @return Returns the point where the drag started or null if not dragging.
101: */
102: public Point getDragStarted() {
103: checkAccess();
104: return dragStarted;
105: }
106:
107: /**
108: * This is a copy modifying queue will have no effect on original queue
109: *
110: * @return Returns the queue of previous events.
111: */
112: public Queue<MapMouseEvent> getPreviousEvents() {
113: checkAccess();
114: return new LinkedList<MapMouseEvent>(previousEvents);
115: }
116:
117: /**
118: * @param dragStarted The dragStarted to set.
119: */
120: protected void setDragStarted(Point dragStarted) {
121: checkAccess();
122: this .dragStarted = dragStarted;
123: }
124:
125: /**
126: * Returns the previous events queue. This is a thread-safe queue.
127: *
128: * @param previousEvents The previousEvents to set.
129: */
130: protected Queue<MapMouseEvent> getModifiablePreviousEvents() {
131: checkAccess();
132: return previousEvents;
133: }
134:
135: /**
136: * Returns the current location of the mouse.
137: * @return
138: */
139: public Point getCurrentPoint() {
140: checkAccess();
141: return currentPoint;
142: }
143:
144: }
|