source: josm/trunk/src/org/openstreetmap/josm/actions/AutoScaleAction.java@ 1953

Last change on this file since 1953 was 1953, checked in by Gubaer, 16 years ago

fixed #3239: Zoom to layer no longer zooms to selected layer

  • Property svn:eol-style set to native
File size: 6.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.List;
12
13import javax.swing.JOptionPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
19import org.openstreetmap.josm.gui.OptionPaneUtil;
20import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
21import org.openstreetmap.josm.gui.layer.Layer;
22import org.openstreetmap.josm.tools.Shortcut;
23
24/**
25 * Toggles the autoScale feature of the mapView
26 * @author imi
27 */
28public class AutoScaleAction extends JosmAction {
29
30 public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
31 private final String mode;
32
33 private static int getModeShortcut(String mode) {
34 int shortcut = -1;
35
36 if (mode.equals("data")) {
37 shortcut = KeyEvent.VK_1;
38 }
39 if (mode.equals("layer")) {
40 shortcut = KeyEvent.VK_2;
41 }
42 if (mode.equals("selection")) {
43 shortcut = KeyEvent.VK_3;
44 }
45 if (mode.equals("conflict")) {
46 shortcut = KeyEvent.VK_4;
47 }
48 if (mode.equals("download")) {
49 shortcut = KeyEvent.VK_5;
50 }
51
52 return shortcut;
53 }
54
55 public AutoScaleAction(String mode) {
56 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
57 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
58 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
59 putValue("help", "Action/AutoScale/" + modeHelp);
60 this.mode = mode;
61 }
62
63 public void autoScale() {
64 if (Main.map != null) {
65 BoundingXYVisitor bbox = getBoundingBox();
66 if (bbox != null && bbox.getBounds() != null) {
67 Main.map.mapView.recalculateCenterScale(bbox);
68 }
69 }
70 putValue("active", true);
71 }
72
73 public void actionPerformed(ActionEvent e) {
74 autoScale();
75 }
76
77 protected Layer getActiveLayer() {
78 try {
79 return Main.map.mapView.getActiveLayer();
80 } catch(NullPointerException e) {
81 return null;
82 }
83 }
84
85 /**
86 * Replies the first selected layer in the layer list dialog. null, if no
87 * such layer exists, either because the layer list dialog is not yet created
88 * or because no layer is selected.
89 *
90 * @return the first selected layer in the layer list dialog
91 */
92 protected Layer getFirstSelectedLayer() {
93 if (LayerListDialog.getInstance() == null) return null;
94 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
95 if (layers.isEmpty()) return null;
96 return layers.get(0);
97 }
98
99 private BoundingXYVisitor getBoundingBox() {
100 BoundingXYVisitor v = new BoundingXYVisitor();
101 if (mode.equals("data")) {
102 for (Layer l : Main.map.mapView.getAllLayers()) {
103 l.visitBoundingBox(v);
104 }
105 } else if (mode.equals("layer")) {
106 if (getActiveLayer() == null)
107 return null;
108 // try to zoom to the first selected layer
109 //
110 Layer l = getFirstSelectedLayer();
111 if (l == null) return null;
112 l.visitBoundingBox(v);
113 } else if (mode.equals("selection") || mode.equals("conflict")) {
114 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
115 if (mode.equals("selection")) {
116 sel = getCurrentDataSet().getSelected();
117 } else if (mode.equals("conflict")) {
118 if (Main.map.conflictDialog.getConflicts() != null) {
119 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
120 }
121 }
122 if (sel.isEmpty()) {
123 OptionPaneUtil.showMessageDialog(
124 Main.parent,
125 (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
126 tr("Information"),
127 JOptionPane.INFORMATION_MESSAGE
128 );
129 return null;
130 }
131 for (OsmPrimitive osm : sel) {
132 osm.visit(v);
133 }
134 // increase bbox by 0.001 degrees on each side. this is required
135 // especially if the bbox contains one single node, but helpful
136 // in most other cases as well.
137 v.enlargeBoundingBox();
138 }
139 else if (mode.equals("download")) {
140 if (Main.pref.hasKey("osm-download.bounds")) {
141 try {
142 String bounds[] = Main.pref.get("osm-download.bounds").split(";");
143 double minlat = Double.parseDouble(bounds[0]);
144 double minlon = Double.parseDouble(bounds[1]);
145 double maxlat = Double.parseDouble(bounds[2]);
146 double maxlon = Double.parseDouble(bounds[3]);
147
148 v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
149 v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
150 }
151 catch (Exception e) {
152 e.printStackTrace();
153 }
154 }
155 }
156 return v;
157 }
158
159 @Override
160 protected void updateEnabledState() {
161 if ("selection".equals(mode)) {
162 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
163 } else if ("layer".equals(mode)) {
164 if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getAllLayersAsList().isEmpty()) {
165 setEnabled(false);
166 } else {
167 // FIXME: should also check for whether a layer is selected in the layer list dialog
168 setEnabled(true);
169 }
170 } else {
171 setEnabled(
172 Main.map != null
173 && Main.map.mapView != null
174 && Main.map.mapView.hasLayers()
175 );
176 }
177 }
178}
Note: See TracBrowser for help on using the repository browser.