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