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.BasicStroke;
036: import java.awt.Shape;
037: import java.awt.geom.Line2D;
038: import java.awt.geom.NoninvertibleTransformException;
039: import java.awt.geom.Point2D;
040:
041: import com.vividsolutions.jts.geom.Coordinate;
042: import com.vividsolutions.jts.geom.GeometryFactory;
043: import com.vividsolutions.jts.geom.LineString;
044: import com.vividsolutions.jump.feature.BasicFeature;
045: import com.vividsolutions.jump.feature.Feature;
046: import com.vividsolutions.jump.workbench.model.AbstractVectorLayerFinder;
047: import com.vividsolutions.jump.workbench.model.Layer;
048: import com.vividsolutions.jump.workbench.model.LayerManagerProxy;
049: import com.vividsolutions.jump.workbench.model.UndoableCommand;
050:
051: public abstract class VectorTool extends NClickTool {
052:
053: public VectorTool() {
054: super (2);
055: setStroke(new BasicStroke(1));
056: allowSnapping();
057: }
058:
059: protected Feature feature(LineString lineString, Layer layer,
060: UndoableCommand command) {
061: Feature feature = new BasicFeature(layer
062: .getFeatureCollectionWrapper().getFeatureSchema());
063: feature.setGeometry(lineString);
064: return feature;
065: }
066:
067: protected LineString lineString(Coordinate source,
068: Coordinate destination)
069: throws NoninvertibleTransformException {
070: return geometryFactory.createLineString(new Coordinate[] {
071: source, destination });
072: }
073:
074: protected GeometryFactory geometryFactory = new GeometryFactory();
075:
076: protected Shape getShape(Point2D source, Point2D destination) {
077: return new Line2D.Double(source, destination);
078: }
079:
080: protected abstract AbstractVectorLayerFinder createVectorLayerFinder(
081: LayerManagerProxy layerManagerProxy);
082:
083: protected void gestureFinished() throws Exception {
084: reportNothingToUndoYet();
085: //Don't want viewport to change at this stage. [Jon Aquino]
086: getPanel().setViewportInitialized(true);
087: execute(createCommand());
088: }
089:
090: protected UndoableCommand createCommand()
091: throws NoninvertibleTransformException {
092: final AbstractVectorLayerFinder vectorLayerFinder = createVectorLayerFinder(getPanel());
093: final boolean vectorLayerExistedOriginally = vectorLayerFinder
094: .getLayer() != null;
095: final LineString lineString = lineString(getModelSource(),
096: getModelDestination());
097: return new UndoableCommand(getName()) {
098: private Feature vector;
099: private boolean vectorLayerVisibleOriginally;
100:
101: public void execute() {
102: if (!vectorLayerExistedOriginally) {
103: vectorLayerFinder.createLayer();
104: }
105: if (vector == null) {
106: //Cache the vector because (1) we don't want to create a new feature
107: //when redo is pressed. [Jon Aquino]
108: vector = feature(lineString, vectorLayerFinder
109: .getLayer(), this );
110: }
111: vectorLayerFinder.getLayer()
112: .getFeatureCollectionWrapper().add(vector);
113: vectorLayerVisibleOriginally = vectorLayerFinder
114: .getLayer().isVisible();
115: vectorLayerFinder.getLayer().setVisible(true);
116: }
117:
118: public void unexecute() {
119: vectorLayerFinder.getLayer().setVisible(
120: vectorLayerVisibleOriginally);
121: vectorLayerFinder.getLayer()
122: .getFeatureCollectionWrapper().remove(vector);
123: if (!vectorLayerExistedOriginally) {
124: getPanel().getLayerManager().remove(
125: vectorLayerFinder.getLayer());
126: }
127: }
128: };
129: }
130:
131: }
|