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

Last change on this file since 6131 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

  • Property svn:eol-style set to native
File size: 13.6 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.util.Collection;
11import java.util.HashSet;
12import java.util.List;
13
14import javax.swing.JOptionPane;
15import javax.swing.event.ListSelectionEvent;
16import javax.swing.event.ListSelectionListener;
17import javax.swing.event.TreeSelectionEvent;
18import javax.swing.event.TreeSelectionListener;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.Bounds;
22import org.openstreetmap.josm.data.conflict.Conflict;
23import org.openstreetmap.josm.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
25import org.openstreetmap.josm.data.validation.TestError;
26import org.openstreetmap.josm.gui.MapFrame;
27import org.openstreetmap.josm.gui.MapFrameListener;
28import org.openstreetmap.josm.gui.MapView;
29import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
30import org.openstreetmap.josm.gui.dialogs.ValidatorDialog.ValidatorBoundingXYVisitor;
31import org.openstreetmap.josm.gui.layer.Layer;
32import org.openstreetmap.josm.tools.Shortcut;
33
34/**
35 * Toggles the autoScale feature of the mapView
36 * @author imi
37 */
38public class AutoScaleAction extends JosmAction {
39
40 public static final String[] MODES = {
41 marktr("data"),
42 marktr("layer"),
43 marktr("selection"),
44 marktr("conflict"),
45 marktr("download"),
46 marktr("problem"),
47 marktr("previous"),
48 marktr("next")};
49
50 private final String mode;
51
52 protected ZoomChangeAdapter zoomChangeAdapter;
53 protected MapFrameAdapter mapFrameAdapter;
54
55 /**
56 * Zooms the current map view to the currently selected primitives.
57 * Does nothing if there either isn't a current map view or if there isn't a current data
58 * layer.
59 *
60 */
61 public static void zoomToSelection() {
62 if (Main.main == null || Main.main.getEditLayer() == null) return;
63 if (Main.map == null || Main.map.mapView == null) return;
64 Collection<OsmPrimitive> sel = Main.main.getEditLayer().data.getSelected();
65 if (sel.isEmpty()) {
66 JOptionPane.showMessageDialog(
67 Main.parent,
68 tr("Nothing selected to zoom to."),
69 tr("Information"),
70 JOptionPane.INFORMATION_MESSAGE
71 );
72 return;
73 }
74 zoomTo(sel);
75 }
76
77 public static void zoomTo(Collection<OsmPrimitive> sel) {
78 BoundingXYVisitor bboxCalculator = new BoundingXYVisitor();
79 bboxCalculator.computeBoundingBox(sel);
80 // increase bbox by 0.001 degrees on each side. this is required
81 // especially if the bbox contains one single node, but helpful
82 // in most other cases as well.
83 bboxCalculator.enlargeBoundingBox();
84 if (bboxCalculator.getBounds() != null) {
85 Main.map.mapView.recalculateCenterScale(bboxCalculator);
86 }
87 }
88
89 public static void autoScale(String mode) {
90 new AutoScaleAction(mode, false).autoScale();
91 }
92
93 private static int getModeShortcut(String mode) {
94 int shortcut = -1;
95
96 /* leave as single line for shortcut overview parsing! */
97 if (mode.equals("data")) { shortcut = KeyEvent.VK_1; }
98 else if (mode.equals("layer")) { shortcut = KeyEvent.VK_2; }
99 else if (mode.equals("selection")) { shortcut = KeyEvent.VK_3; }
100 else if (mode.equals("conflict")) { shortcut = KeyEvent.VK_4; }
101 else if (mode.equals("download")) { shortcut = KeyEvent.VK_5; }
102 else if (mode.equals("problem")) { shortcut = KeyEvent.VK_6; }
103 else if (mode.equals("previous")) { shortcut = KeyEvent.VK_8; }
104 else if (mode.equals("next")) { shortcut = KeyEvent.VK_9; }
105
106 return shortcut;
107 }
108
109 /**
110 * Constructs a new {@code AutoScaleAction}.
111 * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
112 * @param marker Used only to differentiate from default constructor
113 */
114 private AutoScaleAction(String mode, boolean marker) {
115 super(false);
116 this.mode = mode;
117 }
118
119 /**
120 * Constructs a new {@code AutoScaleAction}.
121 * @param mode The autoscale mode (one of {@link AutoScaleAction#MODES})
122 */
123 public AutoScaleAction(final String mode) {
124 super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
125 Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.DIRECT),
126 true, null, false);
127 String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
128 putValue("help", "Action/AutoScale/" + modeHelp);
129 this.mode = mode;
130 if (mode.equals("data")) {
131 putValue("help", ht("/Action/ZoomToData"));
132 } else if (mode.equals("layer")) {
133 putValue("help", ht("/Action/ZoomToLayer"));
134 } else if (mode.equals("selection")) {
135 putValue("help", ht("/Action/ZoomToSelection"));
136 } else if (mode.equals("conflict")) {
137 putValue("help", ht("/Action/ZoomToConflict"));
138 } else if (mode.equals("problem")) {
139 putValue("help", ht("/Action/ZoomToProblem"));
140 } else if (mode.equals("download")) {
141 putValue("help", ht("/Action/ZoomToDownload"));
142 } else if (mode.equals("previous")) {
143 putValue("help", ht("/Action/ZoomToPrevious"));
144 } else if (mode.equals("next")) {
145 putValue("help", ht("/Action/ZoomToNext"));
146 } else {
147 throw new IllegalArgumentException("Unknown mode: "+mode);
148 }
149 installAdapters();
150 }
151
152 public void autoScale() {
153 if (Main.isDisplayingMapView()) {
154 if (mode.equals("previous")) {
155 Main.map.mapView.zoomPrevious();
156 } else if (mode.equals("next")) {
157 Main.map.mapView.zoomNext();
158 } else {
159 BoundingXYVisitor bbox = getBoundingBox();
160 if (bbox != null && bbox.getBounds() != null) {
161 Main.map.mapView.recalculateCenterScale(bbox);
162 }
163 }
164 }
165 putValue("active", true);
166 }
167
168 @Override
169 public void actionPerformed(ActionEvent e) {
170 autoScale();
171 }
172
173 protected Layer getActiveLayer() {
174 try {
175 return Main.map.mapView.getActiveLayer();
176 } catch(NullPointerException e) {
177 return null;
178 }
179 }
180
181 /**
182 * Replies the first selected layer in the layer list dialog. null, if no
183 * such layer exists, either because the layer list dialog is not yet created
184 * or because no layer is selected.
185 *
186 * @return the first selected layer in the layer list dialog
187 */
188 protected Layer getFirstSelectedLayer() {
189 if (LayerListDialog.getInstance() == null) return null;
190 List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
191 if (layers.isEmpty()) return null;
192 return layers.get(0);
193 }
194
195 private BoundingXYVisitor getBoundingBox() {
196 BoundingXYVisitor v = mode.equals("problem") ? new ValidatorBoundingXYVisitor() : new BoundingXYVisitor();
197
198 if (mode.equals("problem")) {
199 TestError error = Main.map.validatorDialog.getSelectedError();
200 if (error == null) return null;
201 ((ValidatorBoundingXYVisitor) v).visit(error);
202 if (v.getBounds() == null) return null;
203 v.enlargeBoundingBox(Main.pref.getDouble("validator.zoom-enlarge-bbox", 0.0002));
204 } else if (mode.equals("data")) {
205 for (Layer l : Main.map.mapView.getAllLayers()) {
206 l.visitBoundingBox(v);
207 }
208 } else if (mode.equals("layer")) {
209 if (getActiveLayer() == null)
210 return null;
211 // try to zoom to the first selected layer
212 //
213 Layer l = getFirstSelectedLayer();
214 if (l == null) return null;
215 l.visitBoundingBox(v);
216 } else if (mode.equals("selection") || mode.equals("conflict")) {
217 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
218 if (mode.equals("selection")) {
219 sel = getCurrentDataSet().getSelected();
220 } else if (mode.equals("conflict")) {
221 Conflict<? extends OsmPrimitive> c = Main.map.conflictDialog.getSelectedConflict();
222 if (c != null) {
223 sel.add(c.getMy());
224 } else if (Main.map.conflictDialog.getConflicts() != null) {
225 sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
226 }
227 }
228 if (sel.isEmpty()) {
229 JOptionPane.showMessageDialog(
230 Main.parent,
231 (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
232 tr("Information"),
233 JOptionPane.INFORMATION_MESSAGE
234 );
235 return null;
236 }
237 for (OsmPrimitive osm : sel) {
238 osm.accept(v);
239 }
240 // increase bbox by 0.001 degrees on each side. this is required
241 // especially if the bbox contains one single node, but helpful
242 // in most other cases as well.
243 v.enlargeBoundingBox();
244 }
245 else if (mode.equals("download")) {
246 if (!Main.pref.get("osm-download.bounds").isEmpty()) {
247 try {
248 v.visit(new Bounds(Main.pref.get("osm-download.bounds"), ";"));
249 } catch (Exception e) {
250 e.printStackTrace();
251 }
252 }
253 }
254 return v;
255 }
256
257 @Override
258 protected void updateEnabledState() {
259 if ("selection".equals(mode)) {
260 setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
261 } else if ("layer".equals(mode)) {
262 if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getAllLayersAsList().isEmpty()) {
263 setEnabled(false);
264 } else {
265 // FIXME: should also check for whether a layer is selected in the layer list dialog
266 setEnabled(true);
267 }
268 } else if ("conflict".equals(mode)) {
269 setEnabled(Main.map != null && Main.map.conflictDialog.getSelectedConflict() != null);
270 } else if ("problem".equals(mode)) {
271 setEnabled(Main.map != null && Main.map.validatorDialog.getSelectedError() != null);
272 } else if ("previous".equals(mode)) {
273 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasZoomUndoEntries());
274 } else if ("next".equals(mode)) {
275 setEnabled(Main.isDisplayingMapView() && Main.map.mapView.hasZoomRedoEntries());
276 } else {
277 setEnabled(
278 Main.isDisplayingMapView()
279 && Main.map.mapView.hasLayers()
280 );
281 }
282 }
283
284 @Override
285 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
286 if ("selection".equals(mode)) {
287 setEnabled(selection != null && !selection.isEmpty());
288 }
289 }
290
291 @Override
292 protected void installAdapters() {
293 super.installAdapters();
294 // make this action listen to zoom and mapframe change events
295 //
296 MapView.addZoomChangeListener(zoomChangeAdapter = new ZoomChangeAdapter());
297 Main.addMapFrameListener(mapFrameAdapter = new MapFrameAdapter());
298 initEnabledState();
299 }
300
301 /**
302 * Adapter for zoom change events
303 */
304 private class ZoomChangeAdapter implements MapView.ZoomChangeListener {
305 @Override
306 public void zoomChanged() {
307 updateEnabledState();
308 }
309 }
310
311 /**
312 * Adapter for MapFrame change events
313 */
314 private class MapFrameAdapter implements MapFrameListener {
315 private ListSelectionListener conflictSelectionListener;
316 private TreeSelectionListener validatorSelectionListener;
317
318 public MapFrameAdapter() {
319 if (mode.equals("conflict")) {
320 conflictSelectionListener = new ListSelectionListener() {
321 @Override public void valueChanged(ListSelectionEvent e) {
322 updateEnabledState();
323 }
324 };
325 } else if (mode.equals("problem")) {
326 validatorSelectionListener = new TreeSelectionListener() {
327 @Override public void valueChanged(TreeSelectionEvent e) {
328 updateEnabledState();
329 }
330 };
331 }
332 }
333
334 @Override public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
335 if (conflictSelectionListener != null) {
336 if (newFrame != null) {
337 newFrame.conflictDialog.addListSelectionListener(conflictSelectionListener);
338 } else if (oldFrame != null) {
339 oldFrame.conflictDialog.removeListSelectionListener(conflictSelectionListener);
340 }
341 } else if (validatorSelectionListener != null) {
342 if (newFrame != null) {
343 newFrame.validatorDialog.addTreeSelectionListener(validatorSelectionListener);
344 } else if (oldFrame != null) {
345 oldFrame.validatorDialog.removeTreeSelectionListener(validatorSelectionListener);
346 }
347 }
348 }
349 }
350}
Note: See TracBrowser for help on using the repository browser.