001: package com.vividsolutions.jump.workbench.ui.zoom;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005: import java.util.StringTokenizer;
006:
007: import javax.swing.JOptionPane;
008:
009: import com.vividsolutions.jts.geom.Coordinate;
010: import com.vividsolutions.jts.geom.Envelope;
011: import com.vividsolutions.jump.I18N;
012: import com.vividsolutions.jump.feature.Feature;
013: import com.vividsolutions.jump.geom.EnvelopeUtil;
014: import com.vividsolutions.jump.util.CoordinateArrays;
015: import com.vividsolutions.jump.util.StringUtil;
016: import com.vividsolutions.jump.workbench.WorkbenchContext;
017: import com.vividsolutions.jump.workbench.model.Layer;
018: import com.vividsolutions.jump.workbench.model.LayerManager;
019: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
020: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
021: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
022: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
023:
024: public class ZoomToCoordinatePlugIn extends AbstractPlugIn {
025: private Coordinate lastCoordinate = new Coordinate(0, 0);
026:
027: public boolean execute(PlugInContext context) throws Exception {
028: reportNothingToUndoYet(context);
029: Coordinate coordinate = prompt(context);
030: if (coordinate == null) {
031: return false;
032: }
033: lastCoordinate = coordinate;
034: context.getLayerViewPanel().getViewport().zoom(
035: toEnvelope(coordinate, context.getLayerManager()));
036:
037: return true;
038: }
039:
040: private Coordinate prompt(PlugInContext context) {
041: while (true) {
042: try {
043: return toCoordinate(JOptionPane
044: .showInputDialog(
045: context.getWorkbenchFrame(),
046: I18N
047: .get("ui.zoom.ZoomToCoordinatePlugIn.enter-coordinate-to-zoom-to"),
048: lastCoordinate.x + ", "
049: + lastCoordinate.y));
050: } catch (Exception e) {
051: JOptionPane.showMessageDialog(context
052: .getWorkbenchFrame(), e.getMessage(), context
053: .getWorkbenchFrame().getTitle(),
054: JOptionPane.ERROR_MESSAGE);
055: }
056: }
057:
058: }
059:
060: private Envelope toEnvelope(Coordinate coordinate,
061: LayerManager layerManager) {
062: int segments = 0;
063: int segmentSum = 0;
064: outer: for (Iterator i = layerManager.iterator(); i.hasNext();) {
065: Layer layer = (Layer) i.next();
066: for (Iterator j = layer.getFeatureCollectionWrapper()
067: .iterator(); j.hasNext();) {
068: Feature feature = (Feature) j.next();
069: Collection coordinateArrays = CoordinateArrays
070: .toCoordinateArrays(feature.getGeometry(),
071: false);
072: for (Iterator k = coordinateArrays.iterator(); k
073: .hasNext();) {
074: Coordinate[] coordinates = (Coordinate[]) k.next();
075: for (int a = 1; a < coordinates.length; a++) {
076: segments++;
077: segmentSum += coordinates[a]
078: .distance(coordinates[a - 1]);
079: if (segments > 100) {
080: break outer;
081: }
082: }
083: }
084: }
085: }
086: Envelope envelope = new Envelope(coordinate);
087: //Choose a reasonable magnification [Jon Aquino 10/22/2003]
088: if (segmentSum > 0) {
089: envelope = EnvelopeUtil.expand(envelope, segmentSum
090: / (double) segments);
091: } else {
092: envelope = EnvelopeUtil.expand(envelope, 50);
093: }
094: return envelope;
095: }
096:
097: private Coordinate toCoordinate(String s) throws Exception {
098: if (s == null) {
099: return null;
100: }
101: if (s.trim().length() == 0) {
102: return null;
103: }
104: s = StringUtil.replaceAll(s, ",", " ");
105: StringTokenizer tokenizer = new StringTokenizer(s);
106: String x = tokenizer.nextToken();
107: if (!StringUtil.isNumber(x)) {
108: throw new Exception("Not a number: " + x);
109: }
110: String y = tokenizer.nextToken();
111: if (!StringUtil.isNumber(y)) {
112: throw new Exception("Not a number: " + y);
113: }
114: return new Coordinate(Double.parseDouble(x), Double
115: .parseDouble(y));
116: }
117:
118: public MultiEnableCheck createEnableCheck(
119: final WorkbenchContext workbenchContext) {
120: EnableCheckFactory checkFactory = new EnableCheckFactory(
121: workbenchContext);
122:
123: return new MultiEnableCheck().add(checkFactory
124: .createWindowWithLayerViewPanelMustBeActiveCheck());
125: }
126: }
|