001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.marktr;
006import static org.openstreetmap.josm.tools.I18n.tr;
007
008import java.awt.event.ActionEvent;
009import java.awt.event.KeyEvent;
010import java.awt.geom.Area;
011import java.util.ArrayList;
012import java.util.Arrays;
013import java.util.Collection;
014import java.util.Collections;
015import java.util.HashSet;
016import java.util.List;
017
018import javax.swing.JOptionPane;
019import javax.swing.event.ListSelectionListener;
020import javax.swing.event.TreeSelectionListener;
021
022import org.openstreetmap.josm.Main;
023import org.openstreetmap.josm.data.Bounds;
024import org.openstreetmap.josm.data.DataSource;
025import org.openstreetmap.josm.data.conflict.Conflict;
026import org.openstreetmap.josm.data.osm.DataSet;
027import org.openstreetmap.josm.data.osm.OsmPrimitive;
028import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
029import org.openstreetmap.josm.data.validation.TestError;
030import org.openstreetmap.josm.gui.MapFrame;
031import org.openstreetmap.josm.gui.MapFrameListener;
032import org.openstreetmap.josm.gui.MapView;
033import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
034import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor;
035import org.openstreetmap.josm.gui.layer.Layer;
036import org.openstreetmap.josm.tools.Shortcut;
037
038/**
039 * Toggles the autoScale feature of the mapView
040 * @author imi
041 */
042public class AutoScaleAction extends JosmAction {
043
044    /**
045     * A list of things we can zoom to. The zoom target is given depending on the mode.
046     */
047    public static final Collection<String> MODES = Collections.unmodifiableList(Arrays.asList(
048        marktr(/* ICON(dialogs/autoscale/) */ "data"),
049        marktr(/* ICON(dialogs/autoscale/) */ "layer"),
050        marktr(/* ICON(dialogs/autoscale/) */ "selection"),
051        marktr(/* ICON(dialogs/autoscale/) */ "conflict"),
052        marktr(/* ICON(dialogs/autoscale/) */ "download"),
053        marktr(/* ICON(dialogs/autoscale/) */ "problem"),
054        marktr(/* ICON(dialogs/autoscale/) */ "previous"),
055        marktr(/* ICON(dialogs/autoscale/) */ "next")));
056
057    /**
058     * One of {@link #MODES}. Defines what we are zooming to.
059     */
060    private final String mode;
061
062    /** Time of last zoom to bounds action */
063    protected long lastZoomTime = -1;
064    /** Last zommed bounds */
065    protected int lastZoomArea = -1;
066
067    /**
068     * Zooms the current map view to the currently selected primitives.
069     * Does nothing if there either isn't a current map view or if there isn't a current data
070     * layer.
071     *
072     */
073    public static void zoomToSelection() {
074        DataSet dataSet = Main.getLayerManager().getEditDataSet();
075        if (dataSet == null) {
076            return;
077        }
078        Collection<OsmPrimitive> sel = dataSet.getSelected();
079        if (sel.isEmpty()) {
080            JOptionPane.showMessageDialog(
081                    Main.parent,
082                    tr("Nothing selected to zoom to."),
083                    tr("Information"),
084                    JOptionPane.INFORMATION_MESSAGE);
085            return;
086        }
087        zoomTo(sel);
088    }
089
090    /**
091     * Zooms the view to display the given set of primitives.
092     * @param sel The primitives to zoom to, e.g. the current selection.
093     */
094    public static void zoomTo(Collection<OsmPrimitive> sel) {
095        BoundingXYVisitor bboxCalculator = new BoundingXYVisitor();
096        bboxCalculator.computeBoundingBox(sel);
097        // increase bbox. This is required
098        // especially if the bbox contains one single node, but helpful
099        // 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            DataSet dataSet = getLayerManager().getEditDataSet();
282            if (dataSet != null) {
283                sel = dataSet.getSelected();
284            }
285        } else {
286            Conflict<? extends OsmPrimitive> c = Main.map.conflictDialog.getSelectedConflict();
287            if (c != null) {
288                sel.add(c.getMy());
289            } else if (Main.map.conflictDialog.getConflicts() != null) {
290                sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
291            }
292        }
293        if (sel.isEmpty()) {
294            JOptionPane.showMessageDialog(
295                    Main.parent,
296                    "selection".equals(mode) ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"),
297                    tr("Information"),
298                    JOptionPane.INFORMATION_MESSAGE);
299            return null;
300        }
301        for (OsmPrimitive osm : sel) {
302            osm.accept(v);
303        }
304
305        // Increase the bounding box by up to 100% to give more context.
306        v.enlargeBoundingBoxLogarithmically(100);
307        // Make the bounding box at least 100 meter wide to
308        // ensure reasonable zoom level when zooming onto single nodes.
309        v.enlargeToMinSize(Main.pref.getDouble("zoom_to_selection_min_size_in_meter", 100));
310        return v;
311    }
312
313    private BoundingXYVisitor modeDownload(BoundingXYVisitor v) {
314        if (lastZoomTime > 0 && System.currentTimeMillis() - lastZoomTime > Main.pref.getLong("zoom.bounds.reset.time", 10L*1000L)) {
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}