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) Stefan Steiniger.
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: * Stefan Steiniger
026: * perriger@gmx.de
027: */
028: /*****************************************************
029: * created: 04.01.2005
030: * last modified: 01.10.2005 [scale obtained now from other class
031: * and change in layout]
032: *
033: * description:
034: * zooms to a given map scale, which is received from an input dialog
035: *
036: *****************************************************/package org.openjump.core.ui.plugin.view;
037:
038: import org.openjump.core.ui.util.ScreenScale;
039:
040: import com.vividsolutions.jts.geom.Coordinate;
041: import com.vividsolutions.jts.geom.Envelope;
042: import com.vividsolutions.jts.geom.Geometry;
043: import com.vividsolutions.jts.geom.GeometryFactory;
044: import com.vividsolutions.jump.I18N;
045: import com.vividsolutions.jump.workbench.WorkbenchContext;
046: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
047: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
048: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
049: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
050: import com.vividsolutions.jump.workbench.ui.GUIUtil;
051: import com.vividsolutions.jump.workbench.ui.MenuNames;
052: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
053: import com.vividsolutions.jump.workbench.ui.Viewport;
054:
055: /**
056: * @description
057: * zooms to a given map scale, received from a input dialog
058: *
059: * @author sstein
060: *
061: */
062: public class ZoomToScalePlugIn extends AbstractPlugIn {
063:
064: private String T1 = "scale"; //[sstein] this string is not used anymore
065: int scale = 0;
066: double oldHorizontalScale = 0; // is calculated for panel-width (not heigth!!)
067: double modelWidth = 0;
068: double panelWidth = 0;
069: String text = I18N
070: .get("org.openjump.core.ui.plugin.view.ZoomToScalePlugIn.set-new-scale-to-zoom")
071: + ": 1 : ";
072:
073: public void initialize(PlugInContext context) throws Exception {
074:
075: this .T1 = I18N
076: .get("org.openjump.core.ui.plugin.view.ZoomToScalePlugIn.scale")
077: + ": ";
078: context
079: .getFeatureInstaller()
080: .addMainMenuItemWithJava14Fix(
081: this ,
082: new String[] { MenuNames.VIEW },
083: I18N
084: .get("org.openjump.core.ui.plugin.view.ZoomToScalePlugIn.zoom-to-scale")
085: + "{pos:9}",
086: false,
087: null,
088: createEnableCheck(context.getWorkbenchContext()));
089: }
090:
091: public static MultiEnableCheck createEnableCheck(
092: WorkbenchContext workbenchContext) {
093: EnableCheckFactory checkFactory = new EnableCheckFactory(
094: workbenchContext);
095:
096: return new MultiEnableCheck().add(checkFactory
097: .createAtLeastNLayersMustExistCheck(1));
098: }
099:
100: public boolean execute(PlugInContext context) throws Exception {
101:
102: Viewport port = context.getLayerViewPanel().getViewport();
103: this .oldHorizontalScale = ScreenScale
104: .getHorizontalMapScale(port);
105:
106: MultiInputDialog dialog = new MultiInputDialog(
107: context.getWorkbenchFrame(),
108: I18N
109: .get("org.openjump.core.ui.plugin.view.ZoomToScalePlugIn.zoom-to-scale"),
110: true);
111: setDialogValues(dialog, context);
112: GUIUtil.centreOnWindow(dialog);
113: dialog.setVisible(true);
114: if (!dialog.wasOKPressed()) {
115: return false;
116: }
117: getDialogValues(dialog);
118:
119: //-- get zoom factor
120: double factor = this .scale / this .oldHorizontalScale;
121:
122: //--calculating new screen using the envelope of the corner LineString
123: Envelope oldEnvelope = port.getEnvelopeInModelCoordinates();
124:
125: double xc = 0.5 * (oldEnvelope.getMaxX() + oldEnvelope
126: .getMinX());
127: double yc = 0.5 * (oldEnvelope.getMaxY() + oldEnvelope
128: .getMinY());
129: double xmin = xc - 1 / 2.0 * factor * oldEnvelope.getWidth();
130: double xmax = xc + 1 / 2.0 * factor * oldEnvelope.getWidth();
131: double ymin = yc - 1 / 2.0 * factor * oldEnvelope.getHeight();
132: double ymax = yc + 1 / 2.0 * factor * oldEnvelope.getHeight();
133: Coordinate[] coords = new Coordinate[] {
134: new Coordinate(xmin, ymin), new Coordinate(xmax, ymax) };
135: Geometry g1 = new GeometryFactory().createLineString(coords);
136: port.zoom(g1.getEnvelopeInternal());
137:
138: return true;
139: }
140:
141: private void setDialogValues(MultiInputDialog dialog,
142: PlugInContext context) {
143: //dialog.addLabel("actual scale in horizontal direction: " + (int)this.oldHorizontalScale);
144: dialog
145: .addLabel(I18N
146: .get("org.openjump.core.ui.plugin.view.ZoomToScalePlugIn.actual-scale-in-horizontal-direction")
147: + " 1 : " + (int) this .oldHorizontalScale);
148: //dialog.addLabel("set new scale to zoom:");
149: dialog.addIntegerField(text, 25000, 7, text);
150: }
151:
152: private void getDialogValues(MultiInputDialog dialog) {
153: this.scale = dialog.getInteger(text);
154: }
155:
156: }
|