001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui.cursortool;
034:
035: import java.awt.Cursor;
036: import java.awt.event.MouseEvent;
037: import java.util.Iterator;
038:
039: /**
040: * Permits only one CursorTool to be active at a time. As long as one CursorTool
041: * has a shape on the screen, all events are directed to that CursorTool alone.
042: * @see AndCompositeTool
043: */
044: public class OrCompositeTool extends CompositeTool {
045: public OrCompositeTool() {
046: this (new CursorTool[] {});
047: }
048:
049: public OrCompositeTool(CursorTool[] cursorTools) {
050: super (cursorTools);
051: }
052:
053: private CursorTool currentTool() {
054: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
055: CursorTool tool = (CursorTool) i.next();
056:
057: if (tool.isGestureInProgress()) {
058: return tool;
059: }
060: }
061:
062: return null;
063: }
064:
065: public Cursor getCursor() {
066: if (currentTool() == null) {
067: return firstCursorTool().getCursor();
068: }
069:
070: return super .getCursor();
071: }
072:
073: /**
074: * Clears the on-screen shapes of all tools other than the current tool.
075: * Called in case some other tools have something drawn on the screen
076: * (such as the SnapIndicatorTool).
077: */
078: private void clearOtherTools() {
079: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
080: CursorTool tool = (CursorTool) i.next();
081:
082: if (tool != currentTool()) {
083: tool.cancelGesture();
084: }
085: }
086: }
087:
088: public void mouseClicked(MouseEvent e) {
089: if (currentTool() != null) {
090: currentTool().mouseClicked(e);
091: clearOtherTools();
092:
093: return;
094: }
095:
096: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
097: CursorTool tool = (CursorTool) i.next();
098: tool.mouseClicked(e);
099:
100: if (currentTool() != null) {
101: clearOtherTools();
102:
103: return;
104: }
105: }
106: }
107:
108: public void mousePressed(MouseEvent e) {
109: if (currentTool() != null) {
110: currentTool().mousePressed(e);
111: clearOtherTools();
112:
113: return;
114: }
115:
116: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
117: CursorTool tool = (CursorTool) i.next();
118: tool.mousePressed(e);
119:
120: if (currentTool() != null) {
121: clearOtherTools();
122:
123: return;
124: }
125: }
126: }
127:
128: public String getName() {
129: return getName("|");
130: }
131:
132: public void mouseReleased(MouseEvent e) {
133: if (currentTool() != null) {
134: currentTool().mouseReleased(e);
135: clearOtherTools();
136:
137: return;
138: }
139:
140: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
141: CursorTool tool = (CursorTool) i.next();
142: tool.mouseReleased(e);
143:
144: if (currentTool() != null) {
145: clearOtherTools();
146:
147: return;
148: }
149: }
150: }
151:
152: public void mouseEntered(MouseEvent e) {
153: if (currentTool() != null) {
154: currentTool().mouseEntered(e);
155: clearOtherTools();
156:
157: return;
158: }
159:
160: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
161: CursorTool tool = (CursorTool) i.next();
162: tool.mouseEntered(e);
163:
164: if (currentTool() != null) {
165: clearOtherTools();
166:
167: return;
168: }
169: }
170: }
171:
172: public void mouseExited(MouseEvent e) {
173: if (currentTool() != null) {
174: currentTool().mouseExited(e);
175: clearOtherTools();
176:
177: return;
178: }
179:
180: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
181: CursorTool tool = (CursorTool) i.next();
182: tool.mouseExited(e);
183:
184: if (currentTool() != null) {
185: clearOtherTools();
186:
187: return;
188: }
189: }
190: }
191:
192: public void mouseDragged(MouseEvent e) {
193: if (currentTool() != null) {
194: currentTool().mouseDragged(e);
195: clearOtherTools();
196:
197: return;
198: }
199:
200: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
201: CursorTool tool = (CursorTool) i.next();
202: tool.mouseDragged(e);
203:
204: if (currentTool() != null) {
205: clearOtherTools();
206:
207: return;
208: }
209: }
210: }
211:
212: public void mouseMoved(MouseEvent e) {
213: if (currentTool() != null) {
214: currentTool().mouseMoved(e);
215: clearOtherTools();
216:
217: return;
218: }
219:
220: for (Iterator i = cursorTools.iterator(); i.hasNext();) {
221: CursorTool tool = (CursorTool) i.next();
222: tool.mouseMoved(e);
223:
224: if (currentTool() != null) {
225: clearOtherTools();
226:
227: return;
228: }
229: }
230: }
231: }
|