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

Last change on this file since 9707 was 9447, checked in by simon04, 8 years ago

see #12335 - Improve "Zoom to layer" and "Zoom to download" enabled states (patch by kolesar)

  • Property svn:eol-style set to native
File size: 15.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
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.ArrayList;
11import java.util.Arrays;
12import java.util.Collection;
13import java.util.Collections;
14import java.util.HashSet;
15import java.util.List;
16
17import javax.swing.JOptionPane;
18import javax.swing.event.ListSelectionEvent;
19import javax.swing.event.ListSelectionListener;
20import javax.swing.event.TreeSelectionEvent;
21import javax.swing.event.TreeSelectionListener;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.data.Bounds;
25import org.openstreetmap.josm.data.DataSource;
26import org.openstreetmap.josm.data.conflict.Conflict;
27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
30import org.openstreetmap.josm.data.validation.TestError;
31import org.openstreetmap.josm.gui.MapFrame;
32import org.openstreetmap.josm.gui.MapFrameListener;
33import org.openstreetmap.josm.gui.MapView;
34import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
35import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor;
36import org.openstreetmap.josm.gui.layer.Layer;
37import org.openstreetmap.josm.tools.Shortcut;
38
39/**
40 * Toggles the autoScale feature of the mapView
41 * @author imi
42 */
43public class AutoScaleAction extends JosmAction {
44
45 /**
46 * A list of things we can zoom to. The zoom target is given depending on the mode.
47 */
48 public static final Collection<String> MODES = Collections.unmodifiableList(Arrays.asList(
49 marktr(/* ICON(dialogs/autoscale/) */ "data"),
50 marktr(/* ICON(dialogs/autoscale/) */ "layer"),
51 marktr(/* ICON(dialogs/autoscale/) */ "selection"),
52 marktr(/* ICON(dialogs/autoscale/) */ "conflict"),
53 marktr(/* ICON(dialogs/autoscale/) */ "download"),
54 marktr(/* ICON(dialogs/autoscale/) */ "problem"),
55 marktr(/* ICON(dialogs/autoscale/) */ "previous"),
56 marktr(/* ICON(dialogs/autoscale/) */ "next")));
57
58 /**
59 * One of {@link #MODES}. Defines what we are zooming to.
60 */
61 private final String mode;
62
63 protected transient ZoomChangeAdapter zoomChangeAdapter;
64 protected transient MapFrameAdapter mapFrameAdapter;
65 /** Time of last zoom to bounds action */
66 protected long lastZoomTime = -1;
67 /** Last zommed bounds */
68 protected int lastZoomArea = -1;
69
70 /**
71 * Zooms the current map view to the currently selected primitives.
72 * Does nothing if there either isn't a current map view or if there isn't a current data
73 * layer.
74 *
75 */
76 public static void zoomToSelection() {
77 if (Main.main == null || !Main.main.hasEditLayer())
78 return;
79 Collection<OsmPrimitive> sel = Main.main.getEditLayer().data.getSelected();
80 if (sel.isEmpty()) {
81 JOptionPane.showMessageDialog(
82 Main.parent,
83 tr("Nothing selected to zoom to."),
84 tr("Information"),
85 JOptionPane.INFORMATION_MESSAGE);
86 return;
87 }
88 zoomTo(sel);
89 }
90
91 /**
92 * Zooms the view to display the given set of primitives.
93 * @param sel The primitives to zoom to, e.g. the current selection.
94 */
95 public static void zoomTo(Collection<OsmPrimitive> sel) {
96 BoundingXYVisitor bboxCalculator = new BoundingXYVisitor();
97 bboxCalculator.computeBoundingBox(sel);
98 // increase bbox. This is required
99 // especially if the bbox contains one single node, but helpful
100 // in most other cases as well.
101 bboxCalculator.enlargeBoundingBox();
102 if (bboxCalculator.getBounds() != null) {
103 Main.map.mapView.zoomTo(bboxCalculator);
104 }
105 }
106
107 /**
108 * Performs the auto scale operation of the given mode without the need to create a new action.
109 * @param mode One of {@link #MODES}.
110 */
111 public static void autoScale(String mode) {
112 new AutoScaleAction(mode, false).autoScale();
113 }
114
115 private static int getModeShortcut(String mode) {
116 int shortcut = -1;
117
118 // TODO: convert this to switch/case and make sure the parsing still works
119 // CHECKSTYLE.OFF: LeftCurly
120 // CHECKSTYLE.OFF: RightCurly
121 /* leave as single line for shortcut overview parsing! */
122 if (mode.equals("data")) { shortcut = KeyEvent.VK_1; }
123 else if (mode.equals("layer")) { shortcut = KeyEvent.VK_2; }
124 else if (mode.equals("selection")) { shortcut = KeyEvent.VK_3; }
125 else if (mode.equals("conflict")) { shortcut = KeyEvent.VK_4; }
126 else if (mode.equals("download")) { shortcut = KeyEvent.VK_5; }
127 else if (mode.equals("problem")) { shortcut = KeyEvent.VK_6; }
128 else if (mode.equals("previous")) { shortcut = KeyEvent.VK_8; }
129 else if (mode.equals("next")) { shortcut = KeyEvent.VK_9; }
130 // CHECKSTYLE.ON: LeftCurly
131 // CHECKSTYLE.ON: RightCurly
132
133 return shortcut;
134 }
135
136 /**
137 * Constructs a new {@code AutoScaleAction}.
138 * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
139 * @param marker Used only to differentiate from default constructor
140 */
141 private AutoScaleAction(String mode, boolean marker) {
142 super(false);
143 this.mode = mode;
144 }
145
146 /**
147 * Constructs a new {@code AutoScaleAction}.
148 * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
149 */
150 public AutoScaleAction(final String mode) {
151 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
152 Shortcut.registerShortcut("view:zoom" + mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))),
153 getModeShortcut(mode), Shortcut.DIRECT), true, null, false);
154 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
155 putValue("help", "Action/AutoScale/" + modeHelp);
156 this.mode = mode;
157 switch (mode) {
158 case "data":
159 putValue("help", ht("/Action/ZoomToData"));
160 break;
161 case "layer":
162 putValue("help", ht("/Action/ZoomToLayer"));
163 break;
164 case "selection":
165 putValue("help", ht("/Action/ZoomToSelection"));
166 break;
167 case "conflict":
168 putValue("help", ht("/Action/ZoomToConflict"));
169 break;
170 case "problem":
171 putValue("help", ht("/Action/ZoomToProblem"));
172 break;
173 case "download":
174 putValue("help", ht("/Action/ZoomToDownload"));
175 break;
176 case "previous":
177 putValue("help", ht("/Action/ZoomToPrevious"));
178 break;
179 case "next":
180 putValue("help", ht("/Action/ZoomToNext"));
181 break;
182 default:
183 throw new IllegalArgumentException("Unknown mode: " + mode);
184 }
185 installAdapters();
186 }
187
188 /**
189 * Performs this auto scale operation for the mode this action is in.
190 */
191 public void autoScale() {
192 if (Main.isDisplayingMapView()) {
193 switch (mode) {
194 case "previous":
195 Main.map.mapView.zoomPrevious();
196 break;
197 case "next":
198 Main.map.mapView.zoomNext();
199 break;
200 default:
201 BoundingXYVisitor bbox = getBoundingBox();
202 if (bbox != null && bbox.getBounds() != null) {
203 Main.map.mapView.zoomTo(bbox);
204 }
205 }
206 }
207 putValue("active", Boolean.TRUE);
208 }
209
210 @Override
211 public void actionPerformed(ActionEvent e) {
212 autoScale();
213 }
214
215 /**
216 * Replies the first selected layer in the layer list dialog. null, if no
217 * such layer exists, either because the layer list dialog is not yet created
218 * or because no layer is selected.
219 *
220 * @return the first selected layer in the layer list dialog
221 */
222 protected Layer getFirstSelectedLayer() {
223 if (Main.main.getActiveLayer() == null) {
224 return null;
225 }
226 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
227 if (layers.isEmpty())
228 return null;
229 return layers.get(0);
230 }
231
232 private BoundingXYVisitor getBoundingBox() {
233 BoundingXYVisitor v = "problem".equals(mode) ? new ValidatorBoundingXYVisitor() : new BoundingXYVisitor();
234
235 switch (mode) {
236 case "problem":
237 TestError error = Main.map.validatorDialog.getSelectedError();
238 if (error == null)
239 return null;
240 ((ValidatorBoundingXYVisitor) v).visit(error);
241 if (v.getBounds() == null)
242 return null;
243 v.enlargeBoundingBox(Main.pref.getDouble("validator.zoom-enlarge-bbox", 0.0002));
244 break;
245 case "data":
246 for (Layer l : Main.map.mapView.getAllLayers()) {
247 l.visitBoundingBox(v);
248 }
249 break;
250 case "layer":
251 // try to zoom to the first selected layer
252 Layer l = getFirstSelectedLayer();
253 if (l == null)
254 return null;
255 l.visitBoundingBox(v);
256 break;
257 case "selection":
258 case "conflict":
259 Collection<OsmPrimitive> sel = new HashSet<>();
260 if ("selection".equals(mode)) {
261 sel = getCurrentDataSet().getSelected();
262 } else {
263 Conflict<? extends OsmPrimitive> c = Main.map.conflictDialog.getSelectedConflict();
264 if (c != null) {
265 sel.add(c.getMy());
266 } else if (Main.map.conflictDialog.getConflicts() != null) {
267 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
268 }
269 }
270 if (sel.isEmpty()) {
271 JOptionPane.showMessageDialog(
272 Main.parent,
273 "selection".equals(mode) ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"),
274 tr("Information"),
275 JOptionPane.INFORMATION_MESSAGE);
276 return null;
277 }
278 for (OsmPrimitive osm : sel) {
279 osm.accept(v);
280 }
281
282 // Increase the bounding box by up to 100% to give more context.
283 v.enlargeBoundingBoxLogarithmically(100);
284 // Make the bounding box at least 100 meter wide to
285 // ensure reasonable zoom level when zooming onto single nodes.
286 v.enlargeToMinSize(Main.pref.getDouble("zoom_to_selection_min_size_in_meter", 100));
287 break;
288 case "download":
289
290 if (lastZoomTime > 0 && System.currentTimeMillis() - lastZoomTime > Main.pref.getLong("zoom.bounds.reset.time", 10*1000)) {
291 lastZoomTime = -1;
292 }
293 final DataSet dataset = getCurrentDataSet();
294 if (dataset != null) {
295 List<DataSource> dataSources = new ArrayList<>(dataset.getDataSources());
296 int s = dataSources.size();
297 if (s > 0) {
298 if (lastZoomTime == -1 || lastZoomArea == -1 || lastZoomArea > s) {
299 lastZoomArea = s-1;
300 v.visit(dataSources.get(lastZoomArea).bounds);
301 } else if (lastZoomArea > 0) {
302 lastZoomArea -= 1;
303 v.visit(dataSources.get(lastZoomArea).bounds);
304 } else {
305 lastZoomArea = -1;
306 v.visit(new Bounds(Main.main.getCurrentDataSet().getDataSourceArea().getBounds2D()));
307 }
308 lastZoomTime = System.currentTimeMillis();
309 } else {
310 lastZoomTime = -1;
311 lastZoomArea = -1;
312 }
313 }
314 break;
315 }
316 return v;
317 }
318
319 @Override
320 protected void updateEnabledState() {
321 switch (mode) {
322 case "selection":
323 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getSelected().isEmpty());
324 break;
325 case "layer":
326 setEnabled(getFirstSelectedLayer() != null);
327 break;
328 case "conflict":
329 setEnabled(Main.map != null && Main.map.conflictDialog.getSelectedConflict() != null);
330 break;
331 case "download":
332 setEnabled(getCurrentDataSet() != null && !getCurrentDataSet().getDataSources().isEmpty());
333 break;
334 case "problem":
335 setEnabled(Main.map != null && Main.map.validatorDialog.getSelectedError() != null);
336 break;
337 case "previous":
338 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasZoomUndoEntries());
339 break;
340 case "next":
341 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasZoomRedoEntries());
342 break;
343 default:
344 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasLayers());
345 }
346 }
347
348 @Override
349 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
350 if ("selection".equals(mode)) {
351 setEnabled(selection != null && !selection.isEmpty());
352 }
353 }
354
355 @Override
356 protected final void installAdapters() {
357 super.installAdapters();
358 // make this action listen to zoom and mapframe change events
359 //
360 MapView.addZoomChangeListener(zoomChangeAdapter = new ZoomChangeAdapter());
361 Main.addMapFrameListener(mapFrameAdapter = new MapFrameAdapter());
362 initEnabledState();
363 }
364
365 /**
366 * Adapter for zoom change events
367 */
368 private class ZoomChangeAdapter implements MapView.ZoomChangeListener {
369 @Override
370 public void zoomChanged() {
371 updateEnabledState();
372 }
373 }
374
375 /**
376 * Adapter for MapFrame change events
377 */
378 private class MapFrameAdapter implements MapFrameListener {
379 private ListSelectionListener conflictSelectionListener;
380 private TreeSelectionListener validatorSelectionListener;
381
382 MapFrameAdapter() {
383 if ("conflict".equals(mode)) {
384 conflictSelectionListener = new ListSelectionListener() {
385 @Override
386 public void valueChanged(ListSelectionEvent e) {
387 updateEnabledState();
388 }
389 };
390 } else if ("problem".equals(mode)) {
391 validatorSelectionListener = new TreeSelectionListener() {
392 @Override
393 public void valueChanged(TreeSelectionEvent e) {
394 updateEnabledState();
395 }
396 };
397 }
398 }
399
400 @Override
401 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
402 if (conflictSelectionListener != null) {
403 if (newFrame != null) {
404 newFrame.conflictDialog.addListSelectionListener(conflictSelectionListener);
405 } else if (oldFrame != null) {
406 oldFrame.conflictDialog.removeListSelectionListener(conflictSelectionListener);
407 }
408 } else if (validatorSelectionListener != null) {
409 if (newFrame != null) {
410 newFrame.validatorDialog.addTreeSelectionListener(validatorSelectionListener);
411 } else if (oldFrame != null) {
412 oldFrame.validatorDialog.removeTreeSelectionListener(validatorSelectionListener);
413 }
414 }
415 updateEnabledState();
416 }
417 }
418}
Note: See TracBrowser for help on using the repository browser.