1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.util.Collection;
|
---|
6 |
|
---|
7 | import javax.swing.AbstractAction;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
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.MapFrame;
|
---|
16 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
17 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Toggles the autoScale feature of the mapView
|
---|
21 | * @author imi
|
---|
22 | */
|
---|
23 | public class AutoScaleAction extends GroupAction {
|
---|
24 |
|
---|
25 | private enum AutoScaleMode {data, selection, layer, conflict}
|
---|
26 | private AutoScaleMode mode = AutoScaleMode.data;
|
---|
27 | private final MapFrame mapFrame;
|
---|
28 |
|
---|
29 | private class Action extends AbstractAction {
|
---|
30 | private final AutoScaleMode mode;
|
---|
31 | public Action(AutoScaleMode mode) {
|
---|
32 | super("Auto Scale: "+mode, ImageProvider.get("dialogs/autoscale/"+mode));
|
---|
33 | putValue(SHORT_DESCRIPTION, "Auto zoom the view to "+mode+". Disabled if the view is moved.");
|
---|
34 | this.mode = mode;
|
---|
35 | }
|
---|
36 | public void actionPerformed(ActionEvent e) {
|
---|
37 | AutoScaleAction.this.mode = mode;
|
---|
38 | if (mapFrame.mapView.isAutoScale())
|
---|
39 | mapFrame.mapView.recalculateCenterScale();
|
---|
40 | else
|
---|
41 | mapFrame.mapView.setAutoScale(true);
|
---|
42 | putValue("active", true);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public AutoScaleAction(final MapFrame mapFrame) {
|
---|
47 | super(KeyEvent.VK_A, 0);
|
---|
48 | for (AutoScaleMode mode : AutoScaleMode.values())
|
---|
49 | actions.add(new Action(mode));
|
---|
50 | setCurrent(0);
|
---|
51 | this.mapFrame = mapFrame;
|
---|
52 | Main.ds.addSelectionChangedListener(new SelectionChangedListener(){
|
---|
53 | public void selectionChanged(Collection<OsmPrimitive> newSelection) {
|
---|
54 | if (mode == AutoScaleMode.selection)
|
---|
55 | mapFrame.mapView.recalculateCenterScale();
|
---|
56 | }
|
---|
57 | });
|
---|
58 | }
|
---|
59 |
|
---|
60 | public BoundingXYVisitor getBoundingBox() {
|
---|
61 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
62 | switch (mode) {
|
---|
63 | case data:
|
---|
64 | for (Layer l : mapFrame.mapView.getAllLayers())
|
---|
65 | l.visitBoundingBox(v);
|
---|
66 | break;
|
---|
67 | case layer:
|
---|
68 | mapFrame.mapView.getActiveLayer().visitBoundingBox(v);
|
---|
69 | break;
|
---|
70 | case selection:
|
---|
71 | case conflict:
|
---|
72 | Collection<OsmPrimitive> sel = mode == AutoScaleMode.selection ? Main.ds.getSelected() : mapFrame.conflictDialog.conflicts.keySet();
|
---|
73 | for (OsmPrimitive osm : sel)
|
---|
74 | osm.visit(v);
|
---|
75 | // special case to zoom nicely to one single node
|
---|
76 | if (v.min != null && v.max != null && v.min.north() == v.max.north() && v.min.east() == v.max.east()) {
|
---|
77 | EastNorth en = Main.proj.latlon2eastNorth(new LatLon(0.02, 0.02));
|
---|
78 | v.min = new EastNorth(v.min.east()-en.east(), v.min.north()-en.north());
|
---|
79 | v.max = new EastNorth(v.max.east()+en.east(), v.max.north()+en.north());
|
---|
80 | }
|
---|
81 | break;
|
---|
82 | }
|
---|
83 | return v;
|
---|
84 | }
|
---|
85 | }
|
---|