| 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.tr;
|
|---|
| 5 | import static org.xnap.commons.i18n.I18n.marktr;
|
|---|
| 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.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"),
|
|---|
| 28 | marktr("layer"),
|
|---|
| 29 | marktr("selection"),
|
|---|
| 30 | marktr("conflict")
|
|---|
| 31 | };
|
|---|
| 32 | private final String mode;
|
|---|
| 33 |
|
|---|
| 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 |
|
|---|
| 53 | public AutoScaleAction(String mode) {
|
|---|
| 54 | super(tr("Zoom to {0}", mode), "dialogs/autoscale/"+mode, tr("Zoom the view to {0}.", tr(mode)), AutoScaleAction.getModeShortcut(mode), 0, 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) {
|
|---|
| 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);
|
|---|
| 86 | // special case to zoom nicely to one single node
|
|---|
| 87 | if (v.min != null && v.max != null && v.min.north() == v.max.north() && v.min.east() == v.max.east()) {
|
|---|
| 88 | EastNorth en = Main.proj.latlon2eastNorth(new LatLon(0.001, 0.001));
|
|---|
| 89 | v.min = new EastNorth(v.min.east()-en.east(), v.min.north()-en.north());
|
|---|
| 90 | v.max = new EastNorth(v.max.east()+en.east(), v.max.north()+en.north());
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | return v;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|