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