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

Last change on this file since 6289 was 6289, checked in by Don-vip, 11 years ago

Sonar/Findbugs - fix various problems

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