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

Last change on this file since 14182 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

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