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

Last change on this file since 15963 was 15963, checked in by Don-vip, 4 years ago

see #17516 - update to error-prone 2.3.5-SNAPSHOT plus following patches for Java 13 compatibility (https://github.com/don-vip/error-prone/commits/jdk13):

  • Property svn:eol-style set to native
File size: 17.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.awt.geom.Area;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.HashSet;
14import java.util.List;
15import java.util.Objects;
16import java.util.concurrent.TimeUnit;
17
18import javax.swing.JOptionPane;
19import javax.swing.event.ListSelectionListener;
20import javax.swing.event.TreeSelectionListener;
21
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.DataSource;
24import org.openstreetmap.josm.data.conflict.Conflict;
25import org.openstreetmap.josm.data.osm.DataSet;
26import org.openstreetmap.josm.data.osm.IPrimitive;
27import org.openstreetmap.josm.data.osm.OsmData;
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.MainApplication;
32import org.openstreetmap.josm.gui.MapFrame;
33import org.openstreetmap.josm.gui.MapFrameListener;
34import org.openstreetmap.josm.gui.MapView;
35import org.openstreetmap.josm.gui.NavigatableComponent.ZoomChangeListener;
36import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
37import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
38import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor;
39import org.openstreetmap.josm.gui.layer.Layer;
40import org.openstreetmap.josm.spi.preferences.Config;
41import org.openstreetmap.josm.tools.Logging;
42import org.openstreetmap.josm.tools.Shortcut;
43
44/**
45 * Toggles the autoScale feature of the mapView
46 * @author imi
47 * @since 17
48 */
49public class AutoScaleAction extends JosmAction {
50
51 /**
52 * A list of things we can zoom to. The zoom target is given depending on the mode.
53 * @since 14221
54 */
55 public enum AutoScaleMode {
56 /** Zoom the window so that all the data fills the window area */
57 DATA(marktr(/* ICON(dialogs/autoscale/) */ "data")),
58 /** Zoom the window so that all the data on the currently selected layer fills the window area */
59 LAYER(marktr(/* ICON(dialogs/autoscale/) */ "layer")),
60 /** Zoom the window so that only data which is currently selected fills the window area */
61 SELECTION(marktr(/* ICON(dialogs/autoscale/) */ "selection")),
62 /** Zoom to the first selected conflict */
63 CONFLICT(marktr(/* ICON(dialogs/autoscale/) */ "conflict")),
64 /** Zoom the view to last downloaded data */
65 DOWNLOAD(marktr(/* ICON(dialogs/autoscale/) */ "download")),
66 /** Zoom the view to problem */
67 PROBLEM(marktr(/* ICON(dialogs/autoscale/) */ "problem")),
68 /** Zoom to the previous zoomed to scale and location (zoom undo) */
69 PREVIOUS(marktr(/* ICON(dialogs/autoscale/) */ "previous")),
70 /** Zoom to the next zoomed to scale and location (zoom redo) */
71 NEXT(marktr(/* ICON(dialogs/autoscale/) */ "next"));
72
73 private final String label;
74
75 AutoScaleMode(String label) {
76 this.label = label;
77 }
78
79 /**
80 * Returns the English label. Used for retrieving icons.
81 * @return the English label
82 */
83 public String getEnglishLabel() {
84 return label;
85 }
86
87 /**
88 * Returns the localized label. Used for display
89 * @return the localized label
90 */
91 public String getLocalizedLabel() {
92 return tr(label);
93 }
94
95 /**
96 * Returns {@code AutoScaleMode} for a given English label
97 * @param englishLabel English label
98 * @return {@code AutoScaleMode} for given English label
99 * @throws IllegalArgumentException if Engligh label is unknown
100 */
101 public static AutoScaleMode of(String englishLabel) {
102 for (AutoScaleMode v : values()) {
103 if (Objects.equals(v.label, englishLabel)) {
104 return v;
105 }
106 }
107 throw new IllegalArgumentException(englishLabel);
108 }
109 }
110
111 /**
112 * One of {@link AutoScaleMode}. Defines what we are zooming to.
113 */
114 private final AutoScaleMode mode;
115
116 /** Time of last zoom to bounds action */
117 protected long lastZoomTime = -1;
118 /** Last zommed bounds */
119 protected int lastZoomArea = -1;
120
121 /**
122 * Zooms the current map view to the currently selected primitives.
123 * Does nothing if there either isn't a current map view or if there isn't a current data layer.
124 *
125 */
126 public static void zoomToSelection() {
127 OsmData<?, ?, ?, ?> dataSet = MainApplication.getLayerManager().getActiveData();
128 if (dataSet == null) {
129 return;
130 }
131 Collection<? extends IPrimitive> sel = dataSet.getSelected();
132 if (sel.isEmpty()) {
133 JOptionPane.showMessageDialog(
134 MainApplication.getMainFrame(),
135 tr("Nothing selected to zoom to."),
136 tr("Information"),
137 JOptionPane.INFORMATION_MESSAGE);
138 return;
139 }
140 zoomTo(sel);
141 }
142
143 /**
144 * Zooms the view to display the given set of primitives.
145 * @param sel The primitives to zoom to, e.g. the current selection.
146 */
147 public static void zoomTo(Collection<? extends IPrimitive> sel) {
148 BoundingXYVisitor bboxCalculator = new BoundingXYVisitor();
149 bboxCalculator.computeBoundingBox(sel);
150 if (bboxCalculator.getBounds() != null) {
151 MainApplication.getMap().mapView.zoomTo(bboxCalculator);
152 }
153 }
154
155 /**
156 * Performs the auto scale operation of the given mode without the need to create a new action.
157 * @param mode One of {@link AutoScaleMode}.
158 * @since 14221
159 */
160 public static void autoScale(AutoScaleMode mode) {
161 new AutoScaleAction(mode, false).autoScale();
162 }
163
164 private static int getModeShortcut(String mode) {
165 int shortcut = -1;
166
167 // TODO: convert this to switch/case and make sure the parsing still works
168 // CHECKSTYLE.OFF: LeftCurly
169 // CHECKSTYLE.OFF: RightCurly
170 /* leave as single line for shortcut overview parsing! */
171 if (mode.equals("data")) { shortcut = KeyEvent.VK_1; }
172 else if (mode.equals("layer")) { shortcut = KeyEvent.VK_2; }
173 else if (mode.equals("selection")) { shortcut = KeyEvent.VK_3; }
174 else if (mode.equals("conflict")) { shortcut = KeyEvent.VK_4; }
175 else if (mode.equals("download")) { shortcut = KeyEvent.VK_5; }
176 else if (mode.equals("problem")) { shortcut = KeyEvent.VK_6; }
177 else if (mode.equals("previous")) { shortcut = KeyEvent.VK_8; }
178 else if (mode.equals("next")) { shortcut = KeyEvent.VK_9; }
179 // CHECKSTYLE.ON: LeftCurly
180 // CHECKSTYLE.ON: RightCurly
181
182 return shortcut;
183 }
184
185 /**
186 * Constructs a new {@code AutoScaleAction}.
187 * @param mode The autoscale mode (one of {@link AutoScaleMode})
188 * @param marker Must be set to false. Used only to differentiate from default constructor
189 */
190 private AutoScaleAction(AutoScaleMode mode, boolean marker) {
191 super(marker);
192 this.mode = mode;
193 }
194
195 /**
196 * Constructs a new {@code AutoScaleAction}.
197 * @param mode The autoscale mode (one of {@link AutoScaleMode})
198 * @since 14221
199 */
200 public AutoScaleAction(final AutoScaleMode mode) {
201 super(tr("Zoom to {0}", mode.getLocalizedLabel()), "dialogs/autoscale/" + mode.getEnglishLabel(),
202 tr("Zoom the view to {0}.", mode.getLocalizedLabel()),
203 Shortcut.registerShortcut("view:zoom" + mode.getEnglishLabel(),
204 tr("View: {0}", tr("Zoom to {0}", mode.getLocalizedLabel())),
205 getModeShortcut(mode.getEnglishLabel()), Shortcut.DIRECT), true, null, false);
206 String label = mode.getEnglishLabel();
207 String modeHelp = Character.toUpperCase(label.charAt(0)) + label.substring(1);
208 setHelpId("Action/AutoScale/" + modeHelp);
209 this.mode = mode;
210 switch (mode) {
211 case DATA:
212 setHelpId(ht("/Action/ZoomToData"));
213 break;
214 case LAYER:
215 setHelpId(ht("/Action/ZoomToLayer"));
216 break;
217 case SELECTION:
218 setHelpId(ht("/Action/ZoomToSelection"));
219 break;
220 case CONFLICT:
221 setHelpId(ht("/Action/ZoomToConflict"));
222 break;
223 case PROBLEM:
224 setHelpId(ht("/Action/ZoomToProblem"));
225 break;
226 case DOWNLOAD:
227 setHelpId(ht("/Action/ZoomToDownload"));
228 break;
229 case PREVIOUS:
230 setHelpId(ht("/Action/ZoomToPrevious"));
231 break;
232 case NEXT:
233 setHelpId(ht("/Action/ZoomToNext"));
234 break;
235 default:
236 throw new IllegalArgumentException("Unknown mode: " + mode);
237 }
238 installAdapters();
239 }
240
241 /**
242 * Performs this auto scale operation for the mode this action is in.
243 */
244 public void autoScale() {
245 if (MainApplication.isDisplayingMapView()) {
246 MapView mapView = MainApplication.getMap().mapView;
247 switch (mode) {
248 case PREVIOUS:
249 mapView.zoomPrevious();
250 break;
251 case NEXT:
252 mapView.zoomNext();
253 break;
254 case PROBLEM:
255 modeProblem(new ValidatorBoundingXYVisitor());
256 break;
257 case DATA:
258 modeData(new BoundingXYVisitor());
259 break;
260 case LAYER:
261 modeLayer(new BoundingXYVisitor());
262 break;
263 case SELECTION:
264 case CONFLICT:
265 modeSelectionOrConflict(new BoundingXYVisitor());
266 break;
267 case DOWNLOAD:
268 modeDownload();
269 break;
270 }
271 putValue("active", Boolean.TRUE);
272 }
273 }
274
275 @Override
276 public void actionPerformed(ActionEvent e) {
277 autoScale();
278 }
279
280 /**
281 * Replies the first selected layer in the layer list dialog. null, if no
282 * such layer exists, either because the layer list dialog is not yet created
283 * or because no layer is selected.
284 *
285 * @return the first selected layer in the layer list dialog
286 */
287 protected Layer getFirstSelectedLayer() {
288 if (getLayerManager().getActiveLayer() == null) {
289 return null;
290 }
291 try {
292 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
293 if (!layers.isEmpty())
294 return layers.get(0);
295 } catch (IllegalStateException e) {
296 Logging.error(e);
297 }
298 return null;
299 }
300
301 private static void modeProblem(ValidatorBoundingXYVisitor v) {
302 TestError error = MainApplication.getMap().validatorDialog.getSelectedError();
303 if (error == null)
304 return;
305 v.visit(error);
306 if (v.getBounds() == null)
307 return;
308 MainApplication.getMap().mapView.zoomTo(v);
309 }
310
311 private static void modeData(BoundingXYVisitor v) {
312 for (Layer l : MainApplication.getLayerManager().getLayers()) {
313 l.visitBoundingBox(v);
314 }
315 MainApplication.getMap().mapView.zoomTo(v);
316 }
317
318 private void modeLayer(BoundingXYVisitor v) {
319 // try to zoom to the first selected layer
320 Layer l = getFirstSelectedLayer();
321 if (l == null)
322 return;
323 l.visitBoundingBox(v);
324 MainApplication.getMap().mapView.zoomTo(v);
325 }
326
327 private void modeSelectionOrConflict(BoundingXYVisitor v) {
328 Collection<IPrimitive> sel = new HashSet<>();
329 if (AutoScaleMode.SELECTION == mode) {
330 OsmData<?, ?, ?, ?> dataSet = getLayerManager().getActiveData();
331 if (dataSet != null) {
332 sel.addAll(dataSet.getSelected());
333 }
334 } else {
335 ConflictDialog conflictDialog = MainApplication.getMap().conflictDialog;
336 Conflict<? extends IPrimitive> c = conflictDialog.getSelectedConflict();
337 if (c != null) {
338 sel.add(c.getMy());
339 } else if (conflictDialog.getConflicts() != null) {
340 sel.addAll(conflictDialog.getConflicts().getMyConflictParties());
341 }
342 }
343 if (sel.isEmpty()) {
344 JOptionPane.showMessageDialog(
345 MainApplication.getMainFrame(),
346 AutoScaleMode.SELECTION == mode ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"),
347 tr("Information"),
348 JOptionPane.INFORMATION_MESSAGE);
349 return;
350 }
351 for (IPrimitive osm : sel) {
352 osm.accept(v);
353 }
354 if (v.getBounds() == null) {
355 return;
356 }
357
358 MainApplication.getMap().mapView.zoomTo(v);
359 }
360
361 private void modeDownload() {
362 if (lastZoomTime > 0 &&
363 System.currentTimeMillis() - lastZoomTime > Config.getPref().getLong("zoom.bounds.reset.time", TimeUnit.SECONDS.toMillis(10))) {
364 lastZoomTime = -1;
365 }
366 Bounds bbox = null;
367 final DataSet dataset = getLayerManager().getActiveDataSet();
368 if (dataset != null) {
369 List<DataSource> dataSources = new ArrayList<>(dataset.getDataSources());
370 int s = dataSources.size();
371 if (s > 0) {
372 if (lastZoomTime == -1 || lastZoomArea == -1 || lastZoomArea > s) {
373 lastZoomArea = s-1;
374 bbox = dataSources.get(lastZoomArea).bounds;
375 } else if (lastZoomArea > 0) {
376 lastZoomArea -= 1;
377 bbox = dataSources.get(lastZoomArea).bounds;
378 } else {
379 lastZoomArea = -1;
380 Area sourceArea = getLayerManager().getActiveDataSet().getDataSourceArea();
381 if (sourceArea != null) {
382 bbox = new Bounds(sourceArea.getBounds2D());
383 }
384 }
385 lastZoomTime = System.currentTimeMillis();
386 } else {
387 lastZoomTime = -1;
388 lastZoomArea = -1;
389 }
390 if (bbox != null) {
391 MainApplication.getMap().mapView.zoomTo(bbox);
392 }
393 }
394 }
395
396 @Override
397 protected void updateEnabledState() {
398 OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
399 MapFrame map = MainApplication.getMap();
400 switch (mode) {
401 case SELECTION:
402 setEnabled(ds != null && !ds.selectionEmpty());
403 break;
404 case LAYER:
405 setEnabled(getFirstSelectedLayer() != null);
406 break;
407 case CONFLICT:
408 setEnabled(map != null && map.conflictDialog.getSelectedConflict() != null);
409 break;
410 case DOWNLOAD:
411 setEnabled(ds != null && !ds.getDataSources().isEmpty());
412 break;
413 case PROBLEM:
414 setEnabled(map != null && map.validatorDialog.getSelectedError() != null);
415 break;
416 case PREVIOUS:
417 setEnabled(MainApplication.isDisplayingMapView() && map.mapView.hasZoomUndoEntries());
418 break;
419 case NEXT:
420 setEnabled(MainApplication.isDisplayingMapView() && map.mapView.hasZoomRedoEntries());
421 break;
422 default:
423 setEnabled(!getLayerManager().getLayers().isEmpty());
424 }
425 }
426
427 @Override
428 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
429 if (AutoScaleMode.SELECTION == mode) {
430 setEnabled(selection != null && !selection.isEmpty());
431 }
432 }
433
434 @Override
435 protected final void installAdapters() {
436 super.installAdapters();
437 // make this action listen to zoom and mapframe change events
438 //
439 MapView.addZoomChangeListener(new ZoomChangeAdapter());
440 MainApplication.addMapFrameListener(new MapFrameAdapter());
441 initEnabledState();
442 }
443
444 /**
445 * Adapter for zoom change events
446 */
447 private class ZoomChangeAdapter implements ZoomChangeListener {
448 @Override
449 public void zoomChanged() {
450 updateEnabledState();
451 }
452 }
453
454 /**
455 * Adapter for MapFrame change events
456 */
457 private class MapFrameAdapter implements MapFrameListener {
458 private ListSelectionListener conflictSelectionListener;
459 private TreeSelectionListener validatorSelectionListener;
460
461 MapFrameAdapter() {
462 if (AutoScaleMode.CONFLICT == mode) {
463 conflictSelectionListener = e -> updateEnabledState();
464 } else if (AutoScaleMode.PROBLEM == mode) {
465 validatorSelectionListener = e -> updateEnabledState();
466 }
467 }
468
469 @Override
470 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
471 if (conflictSelectionListener != null) {
472 if (newFrame != null) {
473 newFrame.conflictDialog.addListSelectionListener(conflictSelectionListener);
474 } else if (oldFrame != null) {
475 oldFrame.conflictDialog.removeListSelectionListener(conflictSelectionListener);
476 }
477 } else if (validatorSelectionListener != null) {
478 if (newFrame != null) {
479 newFrame.validatorDialog.addTreeSelectionListener(validatorSelectionListener);
480 } else if (oldFrame != null) {
481 oldFrame.validatorDialog.removeTreeSelectionListener(validatorSelectionListener);
482 }
483 }
484 updateEnabledState();
485 }
486 }
487}
Note: See TracBrowser for help on using the repository browser.