| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.JOptionPane;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.Main;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
|---|
| 16 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 17 | import org.openstreetmap.josm.tools.ShortCut;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Toggles the autoScale feature of the mapView
|
|---|
| 21 | * @author imi
|
|---|
| 22 | */
|
|---|
| 23 | public class AutoScaleAction extends JosmAction {
|
|---|
| 24 |
|
|---|
| 25 | public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict") };
|
|---|
| 26 | private final String mode;
|
|---|
| 27 |
|
|---|
| 28 | private static int getModeShortcut(String mode) {
|
|---|
| 29 | int shortcut = -1;
|
|---|
| 30 |
|
|---|
| 31 | if (mode.equals("data")) {
|
|---|
| 32 | shortcut = KeyEvent.VK_1;
|
|---|
| 33 | }
|
|---|
| 34 | if (mode.equals("layer")) {
|
|---|
| 35 | shortcut = KeyEvent.VK_2;
|
|---|
| 36 | }
|
|---|
| 37 | if (mode.equals("selection")) {
|
|---|
| 38 | shortcut = KeyEvent.VK_3;
|
|---|
| 39 | }
|
|---|
| 40 | if (mode.equals("conflict")) {
|
|---|
| 41 | shortcut = KeyEvent.VK_4;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | return shortcut;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | public AutoScaleAction(String mode) {
|
|---|
| 48 | super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
|
|---|
| 49 | ShortCut.registerShortCut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), ShortCut.GROUP_EDIT), true);
|
|---|
| 50 | String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
|
|---|
| 51 | putValue("help", "Action/AutoScale/" + modeHelp);
|
|---|
| 52 | this.mode = mode;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | public void actionPerformed(ActionEvent e) {
|
|---|
| 56 | if (Main.map != null) {
|
|---|
| 57 | BoundingXYVisitor bbox = getBoundingBox();
|
|---|
| 58 | if (bbox != null) {
|
|---|
| 59 | Main.map.mapView.recalculateCenterScale(bbox);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | putValue("active", true);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | private BoundingXYVisitor getBoundingBox() {
|
|---|
| 66 | BoundingXYVisitor v = new BoundingXYVisitor();
|
|---|
| 67 | if (mode.equals("data")) {
|
|---|
| 68 | for (Layer l : Main.map.mapView.getAllLayers())
|
|---|
| 69 | l.visitBoundingBox(v);
|
|---|
| 70 | } else if (mode.equals("layer"))
|
|---|
| 71 | Main.map.mapView.getActiveLayer().visitBoundingBox(v);
|
|---|
| 72 | else if (mode.equals("selection") || mode.equals("conflict")) {
|
|---|
| 73 | Collection<OsmPrimitive> sel = mode.equals("selection") ? Main.ds.getSelected()
|
|---|
| 74 | : Main.map.conflictDialog.conflicts.keySet();
|
|---|
| 75 | if (sel.isEmpty()) {
|
|---|
| 76 | JOptionPane.showMessageDialog(Main.parent,
|
|---|
| 77 | mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"));
|
|---|
| 78 | return null;
|
|---|
| 79 | }
|
|---|
| 80 | for (OsmPrimitive osm : sel)
|
|---|
| 81 | osm.visit(v);
|
|---|
| 82 | // increase bbox by 0.001 degrees on each side. this is required
|
|---|
| 83 | // especially if the bbox contains one single node, but helpful
|
|---|
| 84 | // in most other cases as well.
|
|---|
| 85 | v.enlargeBoundingBox();
|
|---|
| 86 | }
|
|---|
| 87 | return v;
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|