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

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

findbugs - BC_UNCONFIRMED_CAST

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