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