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