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.geom.NoninvertibleTransformException;
036: import java.util.ArrayList;
037: import java.util.Collection;
038: import java.util.Iterator;
039:
040: import com.vividsolutions.jts.geom.Coordinate;
041: import com.vividsolutions.jts.geom.Envelope;
042: import com.vividsolutions.jts.geom.GeometryFactory;
043: import com.vividsolutions.jts.geom.Polygon;
044: import com.vividsolutions.jump.geom.CoordUtil;
045:
046: public abstract class RectangleTool extends DragTool {
047: public RectangleTool() {
048: }
049:
050: protected Polygon getRectangle()
051: throws NoninvertibleTransformException {
052: Envelope e = new Envelope(getModelSource().x,
053: getModelDestination().x, getModelSource().y,
054: getModelDestination().y);
055:
056: return new GeometryFactory()
057: .createPolygon(
058: new GeometryFactory()
059: .createLinearRing(new Coordinate[] {
060: new Coordinate(e.getMinX(), e
061: .getMinY()),
062: new Coordinate(e.getMinX(), e
063: .getMaxY()),
064: new Coordinate(e.getMaxX(), e
065: .getMaxY()),
066: new Coordinate(e.getMaxX(), e
067: .getMinY()),
068: new Coordinate(e.getMinX(), e
069: .getMinY()) }), null);
070: }
071:
072: private Collection verticesToSnap(Coordinate source,
073: Coordinate destination) {
074: ArrayList verticesToSnap = new ArrayList();
075: verticesToSnap.add(destination);
076: verticesToSnap.add(new Coordinate(source.x, destination.y));
077: verticesToSnap.add(new Coordinate(destination.x, source.y));
078:
079: return verticesToSnap;
080: }
081:
082: protected void setModelDestination(Coordinate modelDestination) {
083: for (Iterator i = verticesToSnap(getModelSource(),
084: modelDestination).iterator(); i.hasNext();) {
085: Coordinate vertex = (Coordinate) i.next();
086: Coordinate snappedVertex = snap(vertex);
087:
088: if (getSnapManager().wasSnapCoordinateFound()) {
089: this .modelDestination = CoordUtil.add(modelDestination,
090: CoordUtil.subtract(snappedVertex, vertex));
091: return;
092: }
093:
094: }
095: this .modelDestination = modelDestination;
096: return;
097: }
098:
099: protected void setModelSource(Coordinate modelSource) {
100: this.modelSource = snap(modelSource);
101: }
102:
103: }
|