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

Last change on this file since 19307 was 19307, checked in by taylor.smock, 6 months ago

Fix most new PMD issues

It would be better to use the newer switch syntax introduced in Java 14 (JEP 361),
but we currently target Java 11+. When we move to Java 17, this should be
reverted and the newer switch syntax should be used.

  • Property svn:eol-style set to native
File size: 17.5 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[403]2package org.openstreetmap.josm.actions;
3
[2477]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
[948]5import static org.openstreetmap.josm.tools.I18n.marktr;
[403]6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
[458]9import java.awt.event.KeyEvent;
[19050]10import java.awt.geom.Area;
[8171]11import java.util.ArrayList;
[403]12import java.util.Collection;
[1750]13import java.util.HashSet;
[1953]14import java.util.List;
[14221]15import java.util.Objects;
[11288]16import java.util.concurrent.TimeUnit;
[403]17
18import javax.swing.JOptionPane;
[5958]19import javax.swing.event.ListSelectionListener;
20import javax.swing.event.TreeSelectionListener;
[403]21
[2477]22import org.openstreetmap.josm.data.Bounds;
[8171]23import org.openstreetmap.josm.data.DataSource;
[3973]24import org.openstreetmap.josm.data.conflict.Conflict;
[19050]25import org.openstreetmap.josm.data.osm.DataSet;
[13926]26import org.openstreetmap.josm.data.osm.IPrimitive;
27import org.openstreetmap.josm.data.osm.OsmData;
[403]28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
[5958]30import org.openstreetmap.josm.data.validation.TestError;
[12630]31import org.openstreetmap.josm.gui.MainApplication;
[5958]32import org.openstreetmap.josm.gui.MapFrame;
33import org.openstreetmap.josm.gui.MapFrameListener;
[2759]34import org.openstreetmap.josm.gui.MapView;
[15963]35import org.openstreetmap.josm.gui.NavigatableComponent.ZoomChangeListener;
[19050]36import org.openstreetmap.josm.gui.dialogs.ConflictDialog;
[1953]37import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
[5958]38import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor;
[403]39import org.openstreetmap.josm.gui.layer.Layer;
[12846]40import org.openstreetmap.josm.spi.preferences.Config;
[1084]41import org.openstreetmap.josm.tools.Shortcut;
[18211]42import org.openstreetmap.josm.tools.Utils;
[403]43
44/**
45 * Toggles the autoScale feature of the mapView
46 * @author imi
[14221]47 * @since 17
[403]48 */
49public class AutoScaleAction extends JosmAction {
50
[8900]51 /**
52 * A list of things we can zoom to. The zoom target is given depending on the mode.
[14221]53 * @since 14221
[8900]54 */
[14221]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
[17786]99 * @throws IllegalArgumentException if English label is unknown
[14221]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.
[8900]113 */
[14221]114 private final AutoScaleMode mode;
[2685]115
[8171]116 /** Time of last zoom to bounds action */
117 protected long lastZoomTime = -1;
[17786]118 /** Last zoomed bounds */
[8171]119 protected int lastZoomArea = -1;
[5958]120
[2685]121 /**
122 * Zooms the current map view to the currently selected primitives.
[12636]123 * Does nothing if there either isn't a current map view or if there isn't a current data layer.
[2711]124 *
[2685]125 */
126 public static void zoomToSelection() {
[13926]127 OsmData<?, ?, ?, ?> dataSet = MainApplication.getLayerManager().getActiveData();
[10453]128 if (dataSet == null) {
[8171]129 return;
[10453]130 }
[13926]131 Collection<? extends IPrimitive> sel = dataSet.getSelected();
[2685]132 if (sel.isEmpty()) {
133 JOptionPane.showMessageDialog(
[14153]134 MainApplication.getMainFrame(),
[2685]135 tr("Nothing selected to zoom to."),
136 tr("Information"),
[8171]137 JOptionPane.INFORMATION_MESSAGE);
[2685]138 return;
139 }
[3251]140 zoomTo(sel);
141 }
142
[8900]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 */
[13926]147 public static void zoomTo(Collection<? extends IPrimitive> sel) {
[19050]148 BoundingXYVisitor bboxCalculator = new BoundingXYVisitor();
[2685]149 bboxCalculator.computeBoundingBox(sel);
150 if (bboxCalculator.getBounds() != null) {
[12630]151 MainApplication.getMap().mapView.zoomTo(bboxCalculator);
[2685]152 }
153 }
154
[8900]155 /**
156 * Performs the auto scale operation of the given mode without the need to create a new action.
[15007]157 * @param mode One of {@link AutoScaleMode}.
[14221]158 * @since 14221
[8900]159 */
[14221]160 public static void autoScale(AutoScaleMode mode) {
[3327]161 new AutoScaleAction(mode, false).autoScale();
162 }
163
[948]164 private static int getModeShortcut(String mode) {
165 int shortcut = -1;
[458]166
[7012]167 // TODO: convert this to switch/case and make sure the parsing still works
[8513]168 // CHECKSTYLE.OFF: LeftCurly
169 // CHECKSTYLE.OFF: RightCurly
[4921]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; }
[5958]176 else if (mode.equals("problem")) { shortcut = KeyEvent.VK_6; }
[4921]177 else if (mode.equals("previous")) { shortcut = KeyEvent.VK_8; }
178 else if (mode.equals("next")) { shortcut = KeyEvent.VK_9; }
[8513]179 // CHECKSTYLE.ON: LeftCurly
180 // CHECKSTYLE.ON: RightCurly
[458]181
[948]182 return shortcut;
183 }
[403]184
[3327]185 /**
[5958]186 * Constructs a new {@code AutoScaleAction}.
[14221]187 * @param mode The autoscale mode (one of {@link AutoScaleMode})
[11713]188 * @param marker Must be set to false. Used only to differentiate from default constructor
[3327]189 */
[14221]190 private AutoScaleAction(AutoScaleMode mode, boolean marker) {
[11713]191 super(marker);
[3327]192 this.mode = mode;
193 }
194
[5958]195 /**
196 * Constructs a new {@code AutoScaleAction}.
[15007]197 * @param mode The autoscale mode (one of {@link AutoScaleMode})
[14221]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);
[14397]208 setHelpId("Action/AutoScale/" + modeHelp);
[948]209 this.mode = mode;
[7012]210 switch (mode) {
[14221]211 case DATA:
[14397]212 setHelpId(ht("/Action/ZoomToData"));
[7012]213 break;
[14221]214 case LAYER:
[14397]215 setHelpId(ht("/Action/ZoomToLayer"));
[7012]216 break;
[14221]217 case SELECTION:
[14397]218 setHelpId(ht("/Action/ZoomToSelection"));
[7012]219 break;
[14221]220 case CONFLICT:
[14397]221 setHelpId(ht("/Action/ZoomToConflict"));
[7012]222 break;
[14221]223 case PROBLEM:
[14397]224 setHelpId(ht("/Action/ZoomToProblem"));
[7012]225 break;
[14221]226 case DOWNLOAD:
[14397]227 setHelpId(ht("/Action/ZoomToDownload"));
[7012]228 break;
[14221]229 case PREVIOUS:
[14397]230 setHelpId(ht("/Action/ZoomToPrevious"));
[7012]231 break;
[14221]232 case NEXT:
[14397]233 setHelpId(ht("/Action/ZoomToNext"));
[7012]234 break;
[2477]235 }
[5958]236 installAdapters();
[948]237 }
[403]238
[8900]239 /**
240 * Performs this auto scale operation for the mode this action is in.
241 */
[8171]242 public void autoScale() {
[12630]243 if (MainApplication.isDisplayingMapView()) {
[19050]244 MapView mapView = MainApplication.getMap().mapView;
[8171]245 switch (mode) {
[14221]246 case PREVIOUS:
[12630]247 mapView.zoomPrevious();
[7012]248 break;
[14221]249 case NEXT:
[12630]250 mapView.zoomNext();
[7012]251 break;
[14628]252 case PROBLEM:
253 modeProblem(new ValidatorBoundingXYVisitor());
254 break;
255 case DATA:
256 modeData(new BoundingXYVisitor());
257 break;
258 case LAYER:
259 modeLayer(new BoundingXYVisitor());
260 break;
261 case SELECTION:
262 case CONFLICT:
263 modeSelectionOrConflict(new BoundingXYVisitor());
264 break;
265 case DOWNLOAD:
[14985]266 modeDownload();
[14628]267 break;
[948]268 }
[14628]269 putValue("active", Boolean.TRUE);
[948]270 }
271 }
272
[6084]273 @Override
[1868]274 public void actionPerformed(ActionEvent e) {
275 autoScale();
276 }
277
[1953]278 /**
279 * Replies the first selected layer in the layer list dialog. null, if no
280 * such layer exists, either because the layer list dialog is not yet created
281 * or because no layer is selected.
[2512]282 *
[1953]283 * @return the first selected layer in the layer list dialog
284 */
285 protected Layer getFirstSelectedLayer() {
[12636]286 if (getLayerManager().getActiveLayer() == null) {
[9447]287 return null;
288 }
[18005]289 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
290 return layers.isEmpty() ? null : layers.get(0);
[1953]291 }
292
[14628]293 private static void modeProblem(ValidatorBoundingXYVisitor v) {
[12630]294 TestError error = MainApplication.getMap().validatorDialog.getSelectedError();
[10216]295 if (error == null)
[14628]296 return;
[11383]297 v.visit(error);
[10216]298 if (v.getBounds() == null)
[14628]299 return;
300 MainApplication.getMap().mapView.zoomTo(v);
[10216]301 }
302
[14628]303 private static void modeData(BoundingXYVisitor v) {
[12636]304 for (Layer l : MainApplication.getLayerManager().getLayers()) {
[10216]305 l.visitBoundingBox(v);
306 }
[14628]307 MainApplication.getMap().mapView.zoomTo(v);
[10216]308 }
309
[14628]310 private void modeLayer(BoundingXYVisitor v) {
[10216]311 // try to zoom to the first selected layer
[19050]312 Layer l = getFirstSelectedLayer();
[10216]313 if (l == null)
[14628]314 return;
[10216]315 l.visitBoundingBox(v);
[14628]316 MainApplication.getMap().mapView.zoomTo(v);
[10216]317 }
318
[14628]319 private void modeSelectionOrConflict(BoundingXYVisitor v) {
[13955]320 Collection<IPrimitive> sel = new HashSet<>();
[14221]321 if (AutoScaleMode.SELECTION == mode) {
[13955]322 OsmData<?, ?, ?, ?> dataSet = getLayerManager().getActiveData();
[10453]323 if (dataSet != null) {
[13955]324 sel.addAll(dataSet.getSelected());
[10453]325 }
[10216]326 } else {
[19050]327 ConflictDialog conflictDialog = MainApplication.getMap().conflictDialog;
[13955]328 Conflict<? extends IPrimitive> c = conflictDialog.getSelectedConflict();
[10216]329 if (c != null) {
330 sel.add(c.getMy());
[12630]331 } else if (conflictDialog.getConflicts() != null) {
[13955]332 sel.addAll(conflictDialog.getConflicts().getMyConflictParties());
[1750]333 }
[10216]334 }
335 if (sel.isEmpty()) {
336 JOptionPane.showMessageDialog(
[14153]337 MainApplication.getMainFrame(),
[14221]338 AutoScaleMode.SELECTION == mode ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"),
[10216]339 tr("Information"),
340 JOptionPane.INFORMATION_MESSAGE);
[14628]341 return;
[10216]342 }
[13955]343 for (IPrimitive osm : sel) {
344 osm.accept(v);
[10216]345 }
[14628]346 if (v.getBounds() == null) {
347 return;
348 }
[6608]349
[14734]350 MainApplication.getMap().mapView.zoomTo(v);
[10216]351 }
[8171]352
[14985]353 private void modeDownload() {
[11288]354 if (lastZoomTime > 0 &&
[12853]355 System.currentTimeMillis() - lastZoomTime > Config.getPref().getLong("zoom.bounds.reset.time", TimeUnit.SECONDS.toMillis(10))) {
[10216]356 lastZoomTime = -1;
357 }
[14985]358 Bounds bbox = null;
[19050]359 final DataSet dataset = getLayerManager().getActiveDataSet();
[10216]360 if (dataset != null) {
361 List<DataSource> dataSources = new ArrayList<>(dataset.getDataSources());
362 int s = dataSources.size();
363 if (s > 0) {
364 if (lastZoomTime == -1 || lastZoomArea == -1 || lastZoomArea > s) {
365 lastZoomArea = s-1;
[14985]366 bbox = dataSources.get(lastZoomArea).bounds;
[10216]367 } else if (lastZoomArea > 0) {
368 lastZoomArea -= 1;
[14985]369 bbox = dataSources.get(lastZoomArea).bounds;
[8171]370 } else {
371 lastZoomArea = -1;
[19050]372 Area sourceArea = getLayerManager().getActiveDataSet().getDataSourceArea();
[10216]373 if (sourceArea != null) {
[14985]374 bbox = new Bounds(sourceArea.getBounds2D());
[10216]375 }
[1302]376 }
[10216]377 lastZoomTime = System.currentTimeMillis();
378 } else {
379 lastZoomTime = -1;
380 lastZoomArea = -1;
[1302]381 }
[14985]382 if (bbox != null) {
383 MainApplication.getMap().mapView.zoomTo(bbox);
384 }
[1302]385 }
[948]386 }
[1820]387
388 @Override
389 protected void updateEnabledState() {
[13926]390 OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
[12630]391 MapFrame map = MainApplication.getMap();
[8171]392 switch (mode) {
[14221]393 case SELECTION:
[10383]394 setEnabled(ds != null && !ds.selectionEmpty());
[7012]395 break;
[14221]396 case LAYER:
[18005]397 setEnabled(map != null && getFirstSelectedLayer() != null);
[7012]398 break;
[14221]399 case CONFLICT:
[12630]400 setEnabled(map != null && map.conflictDialog.getSelectedConflict() != null);
[7012]401 break;
[14221]402 case DOWNLOAD:
[10382]403 setEnabled(ds != null && !ds.getDataSources().isEmpty());
[9447]404 break;
[14221]405 case PROBLEM:
[12630]406 setEnabled(map != null && map.validatorDialog.getSelectedError() != null);
[7012]407 break;
[14221]408 case PREVIOUS:
[12630]409 setEnabled(MainApplication.isDisplayingMapView() && map.mapView.hasZoomUndoEntries());
[7012]410 break;
[14221]411 case NEXT:
[12630]412 setEnabled(MainApplication.isDisplayingMapView() && map.mapView.hasZoomRedoEntries());
[7012]413 break;
414 default:
[10382]415 setEnabled(!getLayerManager().getLayers().isEmpty());
[1854]416 }
[1820]417 }
[2256]418
419 @Override
420 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
[14221]421 if (AutoScaleMode.SELECTION == mode) {
[18211]422 setEnabled(!Utils.isEmpty(selection));
[2256]423 }
424 }
[2759]425
426 @Override
[6890]427 protected final void installAdapters() {
[2759]428 super.installAdapters();
[5958]429 // make this action listen to zoom and mapframe change events
[2759]430 //
[9795]431 MapView.addZoomChangeListener(new ZoomChangeAdapter());
[12639]432 MainApplication.addMapFrameListener(new MapFrameAdapter());
[2759]433 initEnabledState();
434 }
435
436 /**
[5958]437 * Adapter for zoom change events
[2759]438 */
[19048]439 private final class ZoomChangeAdapter implements ZoomChangeListener {
[6084]440 @Override
[2759]441 public void zoomChanged() {
442 updateEnabledState();
443 }
444 }
445
[5958]446 /**
447 * Adapter for MapFrame change events
448 */
449 private class MapFrameAdapter implements MapFrameListener {
450 private ListSelectionListener conflictSelectionListener;
451 private TreeSelectionListener validatorSelectionListener;
452
[8836]453 MapFrameAdapter() {
[14221]454 if (AutoScaleMode.CONFLICT == mode) {
[10601]455 conflictSelectionListener = e -> updateEnabledState();
[14221]456 } else if (AutoScaleMode.PROBLEM == mode) {
[10601]457 validatorSelectionListener = e -> updateEnabledState();
[5958]458 }
459 }
460
[8171]461 @Override
462 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
[5958]463 if (conflictSelectionListener != null) {
464 if (newFrame != null) {
465 newFrame.conflictDialog.addListSelectionListener(conflictSelectionListener);
466 } else if (oldFrame != null) {
467 oldFrame.conflictDialog.removeListSelectionListener(conflictSelectionListener);
468 }
469 } else if (validatorSelectionListener != null) {
470 if (newFrame != null) {
471 newFrame.validatorDialog.addTreeSelectionListener(validatorSelectionListener);
472 } else if (oldFrame != null) {
473 oldFrame.validatorDialog.removeTreeSelectionListener(validatorSelectionListener);
474 }
475 }
[9447]476 updateEnabledState();
[5958]477 }
478 }
[403]479}
Note: See TracBrowser for help on using the repository browser.