01: /*
02: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
03: * for visualizing and manipulating spatial features with geometry and attributes.
04: *
05: * JUMP is Copyright (C) 2003 Vivid Solutions
06: *
07: * This program implements extensions to JUMP and is
08: * Copyright (C) 2004 Integrated Systems Analysts, Inc.
09: *
10: * This program is free software; you can redistribute it and/or
11: * modify it under the terms of the GNU General Public License
12: * as published by the Free Software Foundation; either version 2
13: * of the License, or (at your option) any later version.
14: *
15: * This program is distributed in the hope that it will be useful,
16: * but WITHOUT ANY WARRANTY; without even the implied warranty of
17: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: * GNU General Public License for more details.
19: *
20: * You should have received a copy of the GNU General Public License
21: * along with this program; if not, write to the Free Software
22: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23: *
24: * For more information, contact:
25: *
26: * Integrated Systems Analysts, Inc.
27: * 630C Anchors St., Suite 101
28: * Fort Walton Beach, Florida
29: * USA
30: *
31: * (850)862-7321
32: */
33:
34: package org.openjump.core.ui.plugin.edittoolbox.cursortools;
35:
36: import java.awt.geom.NoninvertibleTransformException;
37: import java.util.ArrayList;
38:
39: import com.vividsolutions.jts.geom.Coordinate;
40: import com.vividsolutions.jts.geom.GeometryFactory;
41: import com.vividsolutions.jts.geom.Polygon;
42: import com.vividsolutions.jts.operation.valid.IsValidOp;
43: import com.vividsolutions.jump.workbench.ui.EditTransaction;
44:
45: public abstract class ConstrainedPolygonTool extends
46: ConstrainedMultiClickTool {
47: public ConstrainedPolygonTool() {
48: drawClosed = true;
49: }
50:
51: /**
52: * Callers should check whether the polygon returned is valid.
53: */
54: protected Polygon getPolygon()
55: throws NoninvertibleTransformException {
56: ArrayList closedPoints = new ArrayList(getCoordinates());
57:
58: if (!closedPoints.get(0).equals(
59: closedPoints.get(closedPoints.size() - 1))) {
60: closedPoints.add(new Coordinate((Coordinate) closedPoints
61: .get(0)));
62: }
63:
64: return new GeometryFactory().createPolygon(
65: new GeometryFactory()
66: .createLinearRing(toArray(closedPoints)), null);
67: }
68:
69: protected boolean checkPolygon()
70: throws NoninvertibleTransformException {
71: if (getCoordinates().size() < 3) {
72: getPanel().getContext().warnUser(
73: "The polygon must have at least 3 points");
74:
75: return false;
76: }
77:
78: IsValidOp isValidOp = new IsValidOp(getPolygon());
79:
80: if (!isValidOp.isValid()) {
81: getPanel().getContext().warnUser(
82: isValidOp.getValidationError().getMessage());
83:
84: if (getWorkbench().getBlackboard().get(
85: EditTransaction.ROLLING_BACK_INVALID_EDITS_KEY,
86: false)) {
87: return false;
88: }
89: }
90:
91: return true;
92: }
93: }
|