[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;
|
---|
| 5 | import static org.xnap.commons.i18n.I18n.marktr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
| 8 | import java.util.Collection;
|
---|
| 9 |
|
---|
| 10 | import javax.swing.JOptionPane;
|
---|
| 11 |
|
---|
| 12 | import org.openstreetmap.josm.Main;
|
---|
| 13 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
| 14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 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;
|
---|
| 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 = {
|
---|
| 26 | marktr("data"),
|
---|
| 27 | marktr("selection"),
|
---|
| 28 | marktr("layer"),
|
---|
| 29 | marktr("conflict")
|
---|
| 30 | };
|
---|
| 31 | private final String mode;
|
---|
| 32 |
|
---|
| 33 | public AutoScaleAction(String mode) {
|
---|
| 34 | super(tr("Zoom to {0}", mode), "dialogs/autoscale/"+mode, tr("Zoom the view to {0}.", tr(mode)), 0, 0, true);
|
---|
| 35 | String modeHelp = Character.toUpperCase(mode.charAt(0))+mode.substring(1);
|
---|
| 36 | putValue("help", "Action/AutoScale/"+modeHelp);
|
---|
| 37 | this.mode = mode;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | public void actionPerformed(ActionEvent e) {
|
---|
| 41 | if (Main.map != null) {
|
---|
| 42 | BoundingXYVisitor bbox = getBoundingBox();
|
---|
| 43 | if (bbox != null) {
|
---|
| 44 | Main.map.mapView.recalculateCenterScale(bbox);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 | putValue("active", true);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | private BoundingXYVisitor getBoundingBox() {
|
---|
| 51 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
| 52 | if (mode.equals("data")) {
|
---|
| 53 | for (Layer l : Main.map.mapView.getAllLayers())
|
---|
| 54 | l.visitBoundingBox(v);
|
---|
| 55 | } else if (mode.equals("layer"))
|
---|
| 56 | Main.map.mapView.getActiveLayer().visitBoundingBox(v);
|
---|
| 57 | else if (mode.equals("selection") || mode.equals("conflict")) {
|
---|
| 58 | Collection<OsmPrimitive> sel = mode.equals("selection") ? Main.ds.getSelected() : Main.map.conflictDialog.conflicts.keySet();
|
---|
| 59 | if (sel.isEmpty()) {
|
---|
| 60 | JOptionPane.showMessageDialog(Main.parent,
|
---|
| 61 | mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to"));
|
---|
| 62 | return null;
|
---|
| 63 | }
|
---|
| 64 | for (OsmPrimitive osm : sel)
|
---|
| 65 | osm.visit(v);
|
---|
| 66 | // special case to zoom nicely to one single node
|
---|
| 67 | if (v.min != null && v.max != null && v.min.north() == v.max.north() && v.min.east() == v.max.east()) {
|
---|
| 68 | EastNorth en = Main.proj.latlon2eastNorth(new LatLon(0.02, 0.02));
|
---|
| 69 | v.min = new EastNorth(v.min.east()-en.east(), v.min.north()-en.north());
|
---|
| 70 | v.max = new EastNorth(v.max.east()+en.east(), v.max.north()+en.north());
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 | return v;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|