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: * JUMP is Copyright (C) 2003 Vivid Solutions
006: *
007: * This program implements extensions to JUMP and is
008: * Copyright (C) 2004 Integrated Systems Analysts, Inc.
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: *
024: * For more information, contact:
025: *
026: * Integrated Systems Analysts, Inc.
027: * 630C Anchors St., Suite 101
028: * Fort Walton Beach, Florida
029: * USA
030: *
031: * (850)862-7321
032: */
033:
034: package org.openjump.core.ui.plugin.edittoolbox.cursortools;
035:
036: import java.awt.geom.NoninvertibleTransformException;
037: import java.util.ArrayList;
038: import javax.swing.Icon;
039: import javax.swing.ImageIcon;
040:
041: import org.openjump.core.geomutils.Arc;
042: import org.openjump.core.geomutils.MathVector;
043:
044: import com.vividsolutions.jts.geom.Coordinate;
045: import com.vividsolutions.jts.geom.LineString;
046: import com.vividsolutions.jts.operation.valid.IsValidOp;
047: import com.vividsolutions.jump.I18N;
048: import com.vividsolutions.jump.workbench.ui.EditTransaction;
049: import com.vividsolutions.jump.workbench.ui.LayerNamePanelProxy;
050: import com.vividsolutions.jump.workbench.ui.cursortool.CursorTool;
051: import com.vividsolutions.jump.workbench.ui.cursortool.editing.FeatureDrawingUtil;
052:
053: public class DrawConstrainedArcTool extends
054: ConstrainedMultiClickArcTool {
055: private FeatureDrawingUtil featureDrawingUtil;
056: final static String drawConstrainedArc = I18N
057: .get("org.openjump.core.ui.plugin.edittoolbox.cursortools.DrawConstrainedArcTool.Draw-Constrained-Arc");
058: final static String theArcMustHaveAtLeast3Points = I18N
059: .get("org.openjump.core.ui.plugin.edittoolbox.cursortools.DrawConstrainedArcTool.The-arc-must-have-at-least-3-points");
060:
061: private DrawConstrainedArcTool(FeatureDrawingUtil featureDrawingUtil) {
062: drawClosed = false;
063: this .featureDrawingUtil = featureDrawingUtil;
064: }
065:
066: public static CursorTool create(
067: LayerNamePanelProxy layerNamePanelProxy) {
068: FeatureDrawingUtil featureDrawingUtil = new FeatureDrawingUtil(
069: layerNamePanelProxy);
070:
071: return featureDrawingUtil.prepare(new DrawConstrainedArcTool(
072: featureDrawingUtil), true);
073: }
074:
075: public String getName() {
076: return drawConstrainedArc;
077: }
078:
079: public Icon getIcon() {
080: return new ImageIcon(getClass().getResource(
081: "DrawArcConstrained.gif"));
082: }
083:
084: protected void gestureFinished() throws Exception {
085: reportNothingToUndoYet();
086:
087: if (!checkArc()) {
088: return;
089: }
090:
091: execute(featureDrawingUtil.createAddCommand(getArc(),
092: isRollingBackInvalidEdits(), getPanel(), this ));
093: }
094:
095: protected LineString getArc()
096: throws NoninvertibleTransformException {
097: ArrayList points = new ArrayList(getCoordinates());
098:
099: if (points.size() > 1) {
100: Coordinate a = (Coordinate) points.get(0);
101: Coordinate b = (Coordinate) points.get(1);
102: Coordinate c = tentativeCoordinate;
103:
104: if (points.size() > 2) {
105: c = (Coordinate) points.get(points.size() - 1);
106: }
107:
108: MathVector v1 = (new MathVector(b))
109: .vectorBetween(new MathVector(a));
110: MathVector v2 = (new MathVector(c))
111: .vectorBetween(new MathVector(a));
112: double arcAngle = v1.angleDeg(v2);
113: Arc arc = new Arc(a, b, fullAngle);
114: return arc.getLineString();
115: }
116: return null;
117: }
118:
119: protected boolean checkArc() throws NoninvertibleTransformException {
120: if (getCoordinates().size() < 3) {
121: getPanel().getContext().warnUser(
122: theArcMustHaveAtLeast3Points);
123:
124: return false;
125: }
126:
127: IsValidOp isValidOp = new IsValidOp(getArc());
128:
129: if (!isValidOp.isValid()) {
130: getPanel().getContext().warnUser(
131: isValidOp.getValidationError().getMessage());
132:
133: if (getWorkbench().getBlackboard().get(
134: EditTransaction.ROLLING_BACK_INVALID_EDITS_KEY,
135: false)) {
136: return false;
137: }
138: }
139:
140: return true;
141: }
142: }
|