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.snap;
034:
035: import java.awt.Color;
036: import java.awt.Shape;
037: import java.awt.event.MouseEvent;
038: import java.awt.geom.Ellipse2D;
039: import java.awt.geom.NoninvertibleTransformException;
040: import java.awt.geom.Point2D;
041: import java.util.Collection;
042:
043: import javax.swing.Icon;
044:
045: import com.vividsolutions.jts.util.Assert;
046: import com.vividsolutions.jump.workbench.ui.cursortool.AbstractCursorTool;
047:
048: /**
049: * Visually indicates the snap point with a coloured dot.
050: */
051: public class SnapIndicatorTool extends AbstractCursorTool {
052: private Point2D indicatorLocation;
053: private Color snappedColor;
054: private Color unsnappedColor;
055: private double diameter;
056:
057: public SnapIndicatorTool(Collection snapPolicies) {
058: this (Color.green, Color.red, 8, snapPolicies);
059: }
060:
061: public SnapIndicatorTool(Color snappedColor, Color unsnappedColor,
062: double diameter, Collection snapPolicies) {
063: getSnapManager().addPolicies(snapPolicies);
064: setFilling(true);
065: this .snappedColor = snappedColor;
066: this .unsnappedColor = unsnappedColor;
067: this .diameter = diameter;
068: }
069:
070: public Icon getIcon() {
071: return null;
072: }
073:
074: protected void gestureFinished() throws Exception {
075: Assert.shouldNeverReachHere();
076: }
077:
078: public void mouseDragged(MouseEvent e) {
079: mouseLocationChanged(e);
080: }
081:
082: public void mouseMoved(MouseEvent e) {
083: mouseLocationChanged(e);
084: }
085:
086: protected Shape getShape() throws NoninvertibleTransformException {
087: return new Ellipse2D.Double(indicatorLocation.getX()
088: - (diameter / 2), indicatorLocation.getY()
089: - (diameter / 2), diameter, diameter);
090: }
091:
092: private void mouseLocationChanged(MouseEvent e) {
093: try {
094: clearShape();
095: indicatorLocation = getPanel().getViewport().toViewPoint(
096: snap(e.getPoint()));
097: setColor(getSnapManager().wasSnapCoordinateFound() ? snappedColor
098: : unsnappedColor);
099: redrawShape();
100: } catch (Throwable t) {
101: getPanel().getContext().handleThrowable(t);
102: }
103: }
104:
105: public boolean isGestureInProgress() {
106: //Override the default implementation because, yes, the shape is on screen,
107: //but the user is not making a gesture. [Jon Aquino]
108: return false;
109: }
110: }
|