source: josm/trunk/src/org/openstreetmap/josm/actions/AutoScaleAction.java@ 3251

Last change on this file since 3251 was 3251, checked in by stoecker, 15 years ago

fixed #5016 - object download did not focus object

  • Property svn:eol-style set to native
File size: 9.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Collection;
11import java.util.HashSet;
12import java.util.List;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
20import org.openstreetmap.josm.gui.MapView;
21import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
22import org.openstreetmap.josm.gui.layer.Layer;
23import org.openstreetmap.josm.tools.Shortcut;
24
25/**
26 * Toggles the autoScale feature of the mapView
27 * @author imi
28 */
29public class AutoScaleAction extends JosmAction {
30
31 public static final String[] MODES = {
32 marktr("data"),
33 marktr("layer"),
34 marktr("selection"),
35 marktr("conflict"),
36 marktr("download"),
37 marktr("previous"),
38 marktr("next")};
39
40 /**
41 * Zooms the current map view to the currently selected primitives.
42 * Does nothing if there either isn't a current map view or if there isn't a current data
43 * layer.
44 *
45 */
46 public static void zoomToSelection() {
47 if (Main.main == null || Main.main.getEditLayer() == null) return;
48 if (Main.map == null || Main.map.mapView == null) return;
49 Collection<OsmPrimitive> sel = Main.main.getEditLayer().data.getSelected();
50 if (sel.isEmpty()) {
51 JOptionPane.showMessageDialog(
52 Main.parent,
53 tr("Nothing selected to zoom to."),
54 tr("Information"),
55 JOptionPane.INFORMATION_MESSAGE
56 );
57 return;
58 }
59 zoomTo(sel);
60 }
61
62 public static void zoomTo(Collection<OsmPrimitive> sel) {
63 BoundingXYVisitor bboxCalculator = new BoundingXYVisitor();
64 bboxCalculator.computeBoundingBox(sel);
65 // increase bbox by 0.001 degrees on each side. this is required
66 // especially if the bbox contains one single node, but helpful
67 // in most other cases as well.
68 bboxCalculator.enlargeBoundingBox();
69 if (bboxCalculator.getBounds() != null) {
70 Main.map.mapView.recalculateCenterScale(bboxCalculator);
71 }
72 }
73
74 private final String mode;
75
76 private static int getModeShortcut(String mode) {
77 int shortcut = -1;
78
79 if (mode.equals("data")) {
80 shortcut = KeyEvent.VK_1;
81 }
82 if (mode.equals("layer")) {
83 shortcut = KeyEvent.VK_2;
84 }
85 if (mode.equals("selection")) {
86 shortcut = KeyEvent.VK_3;
87 }
88 if (mode.equals("conflict")) {
89 shortcut = KeyEvent.VK_4;
90 }
91 if (mode.equals("download")) {
92 shortcut = KeyEvent.VK_5;
93 }
94 if (mode.equals("previous")) {
95 shortcut = KeyEvent.VK_8;
96 }
97 if (mode.equals("next")) {
98 shortcut = KeyEvent.VK_9;
99 }
100
101 return shortcut;
102 }
103
104 public AutoScaleAction(String mode) {
105 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
106 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
107 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
108 putValue("help", "Action/AutoScale/" + modeHelp);
109 this.mode = mode;
110 if (mode.equals("data")) {
111 putValue("help", ht("/Action/ZoomToData"));
112 } else if (mode.equals("layer")) {
113 putValue("help", ht("/Action/ZoomToLayer"));
114 } else if (mode.equals("selection")) {
115 putValue("help", ht("/Action/ZoomToSelection"));
116 } else if (mode.equals("conflict")) {
117 putValue("help", ht("/Action/ZoomToConflict"));
118 } else if (mode.equals("download")) {
119 putValue("help", ht("/Action/ZoomToDownload"));
120 } else if (mode.equals("previous")) {
121 putValue("help", ht("/Action/ZoomPrevious"));
122 } else if (mode.equals("next")) {
123 putValue("help", ht("/Action/ZoomNext"));
124 }
125 }
126
127 public void autoScale() {
128 if (Main.map != null) {
129 if (mode.equals("previous")) {
130 Main.map.mapView.zoomPrevious();
131 } else if (mode.equals("next")) {
132 Main.map.mapView.zoomNext();
133 } else {
134 BoundingXYVisitor bbox = getBoundingBox();
135 if (bbox != null && bbox.getBounds() != null) {
136 Main.map.mapView.recalculateCenterScale(bbox);
137 }
138 }
139 }
140 putValue("active", true);
141 }
142
143 public void actionPerformed(ActionEvent e) {
144 autoScale();
145 }
146
147 protected Layer getActiveLayer() {
148 try {
149 return Main.map.mapView.getActiveLayer();
150 } catch(NullPointerException e) {
151 return null;
152 }
153 }
154
155 /**
156 * Replies the first selected layer in the layer list dialog. null, if no
157 * such layer exists, either because the layer list dialog is not yet created
158 * or because no layer is selected.
159 *
160 * @return the first selected layer in the layer list dialog
161 */
162 protected Layer getFirstSelectedLayer() {
163 if (LayerListDialog.getInstance() == null) return null;
164 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
165 if (layers.isEmpty()) return null;
166 return layers.get(0);
167 }
168
169 private BoundingXYVisitor getBoundingBox() {
170 BoundingXYVisitor v = new BoundingXYVisitor();
171 if (mode.equals("data")) {
172 for (Layer l : Main.map.mapView.getAllLayers()) {
173 l.visitBoundingBox(v);
174 }
175 } else if (mode.equals("layer")) {
176 if (getActiveLayer() == null)
177 return null;
178 // try to zoom to the first selected layer
179 //
180 Layer l = getFirstSelectedLayer();
181 if (l == null) return null;
182 l.visitBoundingBox(v);
183 } else if (mode.equals("selection") || mode.equals("conflict")) {
184 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
185 if (mode.equals("selection")) {
186 sel = getCurrentDataSet().getSelected();
187 } else if (mode.equals("conflict")) {
188 if (Main.map.conflictDialog.getConflicts() != null) {
189 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
190 }
191 }
192 if (sel.isEmpty()) {
193 JOptionPane.showMessageDialog(
194 Main.parent,
195 (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
196 tr("Information"),
197 JOptionPane.INFORMATION_MESSAGE
198 );
199 return null;
200 }
201 for (OsmPrimitive osm : sel) {
202 osm.visit(v);
203 }
204 // increase bbox by 0.001 degrees on each side. this is required
205 // especially if the bbox contains one single node, but helpful
206 // in most other cases as well.
207 v.enlargeBoundingBox();
208 }
209 else if (mode.equals("download")) {
210 if (Main.pref.hasKey("osm-download.bounds")) {
211 try {
212 v.visit(new Bounds(Main.pref.get("osm-download.bounds"), ";"));
213 } catch (Exception e) {
214 e.printStackTrace();
215 }
216 }
217 }
218 return v;
219 }
220
221 @Override
222 protected void updateEnabledState() {
223 if ("selection".equals(mode)) {
224 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
225 } else if ("layer".equals(mode)) {
226 if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getAllLayersAsList().isEmpty()) {
227 setEnabled(false);
228 } else {
229 // FIXME: should also check for whether a layer is selected in the layer list dialog
230 setEnabled(true);
231 }
232 } else if ("previous".equals(mode)) {
233 setEnabled(Main.map != null && Main.map.mapView != null && Main.map.mapView.hasZoomUndoEntries());
234 } else if ("next".equals(mode)) {
235 setEnabled(Main.map != null && Main.map.mapView != null && Main.map.mapView.hasZoomRedoEntries());
236 } else {
237 setEnabled(
238 Main.isDisplayingMapView()
239 && Main.map.mapView.hasLayers()
240 );
241 }
242 }
243
244 @Override
245 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
246 if ("selection".equals(mode)) {
247 setEnabled(selection != null && !selection.isEmpty());
248 }
249 }
250
251 @Override
252 protected void installAdapters() {
253 super.installAdapters();
254 // make this action listen to zoom change events
255 //
256 zoomChangeAdapter = new ZoomChangeAdapter();
257 MapView.addZoomChangeListener(zoomChangeAdapter);
258 initEnabledState();
259 }
260
261 /**
262 * Adapter for selection change events
263 *
264 */
265 private class ZoomChangeAdapter implements MapView.ZoomChangeListener {
266 public void zoomChanged() {
267 updateEnabledState();
268 }
269 }
270
271 private ZoomChangeAdapter zoomChangeAdapter;
272}
Note: See TracBrowser for help on using the repository browser.