[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[403] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[758] | 5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
[403] | 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;
|
---|
| 14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 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 |
|
---|
| 20 | /**
|
---|
| 21 | * Toggles the autoScale feature of the mapView
|
---|
| 22 | * @author imi
|
---|
| 23 | */
|
---|
| 24 | public class AutoScaleAction extends JosmAction {
|
---|
| 25 |
|
---|
| 26 | public static final String[] modes = {
|
---|
| 27 | marktr("data"),
|
---|
[458] | 28 | marktr("layer"),
|
---|
[403] | 29 | marktr("selection"),
|
---|
| 30 | marktr("conflict")
|
---|
| 31 | };
|
---|
| 32 | private final String mode;
|
---|
| 33 |
|
---|
[458] | 34 | private static int getModeShortcut(String mode) {
|
---|
| 35 | int shortcut = -1;
|
---|
| 36 |
|
---|
| 37 | if(mode.equals("data")) {
|
---|
| 38 | shortcut = KeyEvent.VK_1;
|
---|
| 39 | }
|
---|
| 40 | if(mode.equals("layer")) {
|
---|
| 41 | shortcut = KeyEvent.VK_2;
|
---|
| 42 | }
|
---|
| 43 | if(mode.equals("selection")) {
|
---|
| 44 | shortcut = KeyEvent.VK_3;
|
---|
| 45 | }
|
---|
| 46 | if(mode.equals("conflict")) {
|
---|
| 47 | shortcut = KeyEvent.VK_4;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | return shortcut;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[403] | 53 | public AutoScaleAction(String mode) {
|
---|
[731] | 54 | super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/"+mode, tr("Zoom the view to {0}.", tr(mode)), AutoScaleAction.getModeShortcut(mode), 0, true);
|
---|
[403] | 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) {
|
---|
| 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 | } else if (mode.equals("layer"))
|
---|
| 76 | Main.map.mapView.getActiveLayer().visitBoundingBox(v);
|
---|
| 77 | else if (mode.equals("selection") || mode.equals("conflict")) {
|
---|
| 78 | Collection<OsmPrimitive> sel = mode.equals("selection") ? Main.ds.getSelected() : 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);
|
---|
[746] | 86 | // increase bbox by 0.001 degrees on each side. this is required
|
---|
| 87 | // especially if the bbox contains one single node, but helpful
|
---|
| 88 | // in most other cases as well.
|
---|
[754] | 89 | if (v.min != null && v.max != null) // && v.min.north() == v.max.north() && v.min.east() == v.max.east()) {
|
---|
| 90 | {
|
---|
[797] | 91 | EastNorth en = new EastNorth(0.0001,0.0001);
|
---|
[754] | 92 | v.min = new EastNorth(v.min.east()-en.east(), v.min.north()-en.north());
|
---|
| 93 | v.max = new EastNorth(v.max.east()+en.east(), v.max.north()+en.north());
|
---|
| 94 | }
|
---|
[403] | 95 | }
|
---|
| 96 | return v;
|
---|
| 97 | }
|
---|
| 98 | }
|
---|