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

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

see #8039, see #10456 - support read-only data layers

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