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.tools;
035:
036: import java.awt.Cursor;
037: import java.awt.event.MouseEvent;
038: import java.awt.geom.NoninvertibleTransformException;
039: import java.text.DecimalFormat;
040: import java.util.ArrayList;
041: import java.util.List;
042:
043: import javax.swing.Icon;
044: import javax.swing.ImageIcon;
045:
046: import com.vividsolutions.jts.geom.Coordinate;
047: import com.vividsolutions.jump.I18N;
048: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
049: import com.vividsolutions.jump.workbench.ui.cursortool.MultiClickTool;
050:
051: public class MeasureM_FTool extends MultiClickTool {
052: private final static String sDistance = I18N
053: .get("org.openjump.core.ui.plugin.tools.MeasureM_FTool.Distance");
054: private final static String sMeters = I18N
055: .get("org.openjump.core.ui.plugin.tools.MeasureM_FTool.meters");
056: private final static String sFeet = I18N
057: .get("org.openjump.core.ui.plugin.tools.MeasureM_FTool.feet");
058:
059: private List savedCoordinates = new ArrayList();
060: private Coordinate currCoord;
061:
062: public MeasureM_FTool() {
063: allowSnapping();
064: }
065:
066: public Icon getIcon() {
067: return new ImageIcon(getClass().getResource("RulerM_F.gif"));
068: }
069:
070: public Cursor getCursor() {
071: for (int i = 0; i < savedCoordinates.size(); i++) {
072: add((Coordinate) savedCoordinates.get(i));
073: }
074: return createCursor(new ImageIcon(getClass().getResource(
075: "RulerCursorM_F.gif")).getImage());
076: }
077:
078: public void mouseLocationChanged(MouseEvent e) {
079: try {
080: if (isShapeOnScreen()) {
081: ArrayList currentCoordinates = new ArrayList(
082: getCoordinates());
083: currentCoordinates.add(getPanel().getViewport()
084: .toModelCoordinate(e.getPoint()));
085: display(currentCoordinates, getPanel());
086: }
087:
088: currCoord = snap(e.getPoint());
089: super .mouseLocationChanged(e);
090: } catch (Throwable t) {
091: getPanel().getContext().handleThrowable(t);
092: }
093: }
094:
095: public void mousePressed(MouseEvent e) {
096: super .mousePressed(e);
097: savedCoordinates = new ArrayList(getCoordinates());
098: }
099:
100: protected void gestureFinished()
101: throws NoninvertibleTransformException {
102: reportNothingToUndoYet();
103: savedCoordinates.clear();
104:
105: //Status bar is cleared before #gestureFinished is called. So redisplay
106: //the length. [Jon Aquino]
107: display(getCoordinates(), getPanel());
108: }
109:
110: private void display(List coordinates, LayerViewPanel panel)
111: throws NoninvertibleTransformException {
112: display(distance(coordinates), panel);
113: }
114:
115: private void display(double distance, LayerViewPanel panel) {
116: DecimalFormat df3 = new DecimalFormat("###,###,##0.0##");
117: String distString = df3.format(distance / 0.3048);
118: panel.getContext().setStatusMessage(
119: sDistance + ": " + panel.format(distance) + " "
120: + sMeters + " " + " = " + distString + " feet");
121: }
122:
123: private double distance(List coordinates) {
124: double distance = 0;
125:
126: for (int i = 1; i < coordinates.size(); i++) {
127: distance += ((Coordinate) coordinates.get(i - 1))
128: .distance((Coordinate) coordinates.get(i));
129: }
130:
131: if ((currCoord != null) && (coordinates.size() > 1)) {
132: distance -= ((Coordinate) coordinates.get(coordinates
133: .size() - 2)).distance((Coordinate) coordinates
134: .get(coordinates.size() - 1));
135: distance += ((Coordinate) coordinates.get(coordinates
136: .size() - 2)).distance(currCoord);
137: }
138:
139: return distance;
140: }
141: }
|