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