| 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.util.Collection;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.Main;
|
|---|
| 11 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 12 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
|---|
| 15 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Toggles the autoScale feature of the mapView
|
|---|
| 19 | * @author imi
|
|---|
| 20 | */
|
|---|
| 21 | public class AutoScaleAction extends JosmAction {
|
|---|
| 22 |
|
|---|
| 23 | public static final String[] modes = {
|
|---|
| 24 | marktr("data"),
|
|---|
| 25 | marktr("selection"),
|
|---|
| 26 | marktr("layer"),
|
|---|
| 27 | marktr("conflict")
|
|---|
| 28 | };
|
|---|
| 29 | private final String mode;
|
|---|
| 30 |
|
|---|
| 31 | public AutoScaleAction(String mode) {
|
|---|
| 32 | super(tr("Zoom to {0}", mode), "dialogs/autoscale/"+mode, tr("Zoom the view to {0}.", tr(mode)), 0, 0, true);
|
|---|
| 33 | String modeHelp = Character.toUpperCase(mode.charAt(0))+mode.substring(1);
|
|---|
| 34 | putValue("help", "Action/AutoScale/"+modeHelp);
|
|---|
| 35 | this.mode = mode;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public void actionPerformed(ActionEvent e) {
|
|---|
| 39 | if (Main.map != null)
|
|---|
| 40 | Main.map.mapView.recalculateCenterScale(getBoundingBox());
|
|---|
| 41 | putValue("active", true);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | private BoundingXYVisitor getBoundingBox() {
|
|---|
| 45 | BoundingXYVisitor v = new BoundingXYVisitor();
|
|---|
| 46 | if (mode.equals("data")) {
|
|---|
| 47 | for (Layer l : Main.map.mapView.getAllLayers())
|
|---|
| 48 | l.visitBoundingBox(v);
|
|---|
| 49 | } else if (mode.equals("layer"))
|
|---|
| 50 | Main.map.mapView.getActiveLayer().visitBoundingBox(v);
|
|---|
| 51 | else if (mode.equals("selection") || mode.equals("conflict")) {
|
|---|
| 52 | Collection<OsmPrimitive> sel = mode.equals("selection") ? Main.ds.getSelected() : Main.map.conflictDialog.conflicts.keySet();
|
|---|
| 53 | for (OsmPrimitive osm : sel)
|
|---|
| 54 | osm.visit(v);
|
|---|
| 55 | // special case to zoom nicely to one single node
|
|---|
| 56 | if (v.min != null && v.max != null && v.min.north() == v.max.north() && v.min.east() == v.max.east()) {
|
|---|
| 57 | EastNorth en = Main.proj.latlon2eastNorth(new LatLon(0.02, 0.02));
|
|---|
| 58 | v.min = new EastNorth(v.min.east()-en.east(), v.min.north()-en.north());
|
|---|
| 59 | v.max = new EastNorth(v.max.east()+en.east(), v.max.north()+en.north());
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | return v;
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|