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