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.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 |
|
---|
12 | import javax.swing.JOptionPane;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
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.OptionPaneUtil;
|
---|
19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
20 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Toggles the autoScale feature of the mapView
|
---|
24 | * @author imi
|
---|
25 | */
|
---|
26 | public class AutoScaleAction extends JosmAction {
|
---|
27 |
|
---|
28 | public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
|
---|
29 | private final String mode;
|
---|
30 |
|
---|
31 | private static int getModeShortcut(String mode) {
|
---|
32 | int shortcut = -1;
|
---|
33 |
|
---|
34 | if (mode.equals("data")) {
|
---|
35 | shortcut = KeyEvent.VK_1;
|
---|
36 | }
|
---|
37 | if (mode.equals("layer")) {
|
---|
38 | shortcut = KeyEvent.VK_2;
|
---|
39 | }
|
---|
40 | if (mode.equals("selection")) {
|
---|
41 | shortcut = KeyEvent.VK_3;
|
---|
42 | }
|
---|
43 | if (mode.equals("conflict")) {
|
---|
44 | shortcut = KeyEvent.VK_4;
|
---|
45 | }
|
---|
46 | if (mode.equals("download")) {
|
---|
47 | shortcut = KeyEvent.VK_5;
|
---|
48 | }
|
---|
49 |
|
---|
50 | return shortcut;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public AutoScaleAction(String mode) {
|
---|
54 | super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
|
---|
55 | Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
|
---|
56 | String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
|
---|
57 | putValue("help", "Action/AutoScale/" + modeHelp);
|
---|
58 | this.mode = mode;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public void actionPerformed(ActionEvent e) {
|
---|
62 | if (Main.map != null) {
|
---|
63 | BoundingXYVisitor bbox = getBoundingBox();
|
---|
64 | if (bbox != null && bbox.getBounds() != null) {
|
---|
65 | Main.map.mapView.recalculateCenterScale(bbox);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | putValue("active", true);
|
---|
69 | }
|
---|
70 |
|
---|
71 | private BoundingXYVisitor getBoundingBox() {
|
---|
72 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
73 | if (mode.equals("data")) {
|
---|
74 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
---|
75 | l.visitBoundingBox(v);
|
---|
76 | }
|
---|
77 | } else if (mode.equals("layer")) {
|
---|
78 | Main.map.mapView.getActiveLayer().visitBoundingBox(v);
|
---|
79 | } else if (mode.equals("selection") || mode.equals("conflict")) {
|
---|
80 | Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
---|
81 | if (mode.equals("selection")) {
|
---|
82 | sel = getCurrentDataSet().getSelected();
|
---|
83 | } else if (mode.equals("conflict")) {
|
---|
84 | if (Main.map.conflictDialog.getConflicts() != null) {
|
---|
85 | sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | if (sel.isEmpty()) {
|
---|
89 | OptionPaneUtil.showMessageDialog(
|
---|
90 | Main.parent,
|
---|
91 | (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
|
---|
92 | tr("Information"),
|
---|
93 | JOptionPane.INFORMATION_MESSAGE
|
---|
94 | );
|
---|
95 | return null;
|
---|
96 | }
|
---|
97 | for (OsmPrimitive osm : sel) {
|
---|
98 | osm.visit(v);
|
---|
99 | }
|
---|
100 | // increase bbox by 0.001 degrees on each side. this is required
|
---|
101 | // especially if the bbox contains one single node, but helpful
|
---|
102 | // in most other cases as well.
|
---|
103 | v.enlargeBoundingBox();
|
---|
104 | }
|
---|
105 | else if (mode.equals("download")) {
|
---|
106 | if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
107 | try {
|
---|
108 | String bounds[] = Main.pref.get("osm-download.bounds").split(";");
|
---|
109 | double minlat = Double.parseDouble(bounds[0]);
|
---|
110 | double minlon = Double.parseDouble(bounds[1]);
|
---|
111 | double maxlat = Double.parseDouble(bounds[2]);
|
---|
112 | double maxlon = Double.parseDouble(bounds[3]);
|
---|
113 |
|
---|
114 | v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
|
---|
115 | v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
|
---|
116 | }
|
---|
117 | catch (Exception e) {
|
---|
118 | e.printStackTrace();
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return v;
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | protected void updateEnabledState() {
|
---|
127 | if ("selection".equals(mode)) {
|
---|
128 | setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
|
---|
129 | } else {
|
---|
130 | setEnabled(
|
---|
131 | Main.map != null
|
---|
132 | && Main.map.mapView != null
|
---|
133 | && Main.map.mapView.getAllLayers().size() > 0
|
---|
134 | );
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|