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

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

add AbstractOsmDataLayer, MainLayerManager.getActiveData, Main.getInProgressISelection

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