| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 | import java.util.HashSet;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.JOptionPane;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
|---|
| 19 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
|---|
| 20 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 21 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Toggles the autoScale feature of the mapView
|
|---|
| 25 | * @author imi
|
|---|
| 26 | */
|
|---|
| 27 | public class AutoScaleAction extends JosmAction {
|
|---|
| 28 |
|
|---|
| 29 | public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
|
|---|
| 30 | private final String mode;
|
|---|
| 31 |
|
|---|
| 32 | private static int getModeShortcut(String mode) {
|
|---|
| 33 | int shortcut = -1;
|
|---|
| 34 |
|
|---|
| 35 | if (mode.equals("data")) {
|
|---|
| 36 | shortcut = KeyEvent.VK_1;
|
|---|
| 37 | }
|
|---|
| 38 | if (mode.equals("layer")) {
|
|---|
| 39 | shortcut = KeyEvent.VK_2;
|
|---|
| 40 | }
|
|---|
| 41 | if (mode.equals("selection")) {
|
|---|
| 42 | shortcut = KeyEvent.VK_3;
|
|---|
| 43 | }
|
|---|
| 44 | if (mode.equals("conflict")) {
|
|---|
| 45 | shortcut = KeyEvent.VK_4;
|
|---|
| 46 | }
|
|---|
| 47 | if (mode.equals("download")) {
|
|---|
| 48 | shortcut = KeyEvent.VK_5;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | return shortcut;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | public AutoScaleAction(String mode) {
|
|---|
| 55 | super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
|
|---|
| 56 | Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
|
|---|
| 57 | String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
|
|---|
| 58 | putValue("help", "Action/AutoScale/" + modeHelp);
|
|---|
| 59 | this.mode = mode;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public void autoScale() {
|
|---|
| 63 | if (Main.map != null) {
|
|---|
| 64 | BoundingXYVisitor bbox = getBoundingBox();
|
|---|
| 65 | if (bbox != null && bbox.getBounds() != null) {
|
|---|
| 66 | Main.map.mapView.recalculateCenterScale(bbox);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | putValue("active", true);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | public void actionPerformed(ActionEvent e) {
|
|---|
| 73 | autoScale();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | protected Layer getActiveLayer() {
|
|---|
| 77 | try {
|
|---|
| 78 | return Main.map.mapView.getActiveLayer();
|
|---|
| 79 | } catch(NullPointerException e) {
|
|---|
| 80 | return null;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Replies the first selected layer in the layer list dialog. null, if no
|
|---|
| 86 | * such layer exists, either because the layer list dialog is not yet created
|
|---|
| 87 | * or because no layer is selected.
|
|---|
| 88 | *
|
|---|
| 89 | * @return the first selected layer in the layer list dialog
|
|---|
| 90 | */
|
|---|
| 91 | protected Layer getFirstSelectedLayer() {
|
|---|
| 92 | if (LayerListDialog.getInstance() == null) return null;
|
|---|
| 93 | List<Layer> layers = LayerListDialog.getInstance().getModel().getSelectedLayers();
|
|---|
| 94 | if (layers.isEmpty()) return null;
|
|---|
| 95 | return layers.get(0);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | private BoundingXYVisitor getBoundingBox() {
|
|---|
| 99 | BoundingXYVisitor v = new BoundingXYVisitor();
|
|---|
| 100 | if (mode.equals("data")) {
|
|---|
| 101 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
|---|
| 102 | l.visitBoundingBox(v);
|
|---|
| 103 | }
|
|---|
| 104 | } else if (mode.equals("layer")) {
|
|---|
| 105 | if (getActiveLayer() == null)
|
|---|
| 106 | return null;
|
|---|
| 107 | // try to zoom to the first selected layer
|
|---|
| 108 | //
|
|---|
| 109 | Layer l = getFirstSelectedLayer();
|
|---|
| 110 | if (l == null) return null;
|
|---|
| 111 | l.visitBoundingBox(v);
|
|---|
| 112 | } else if (mode.equals("selection") || mode.equals("conflict")) {
|
|---|
| 113 | Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
|---|
| 114 | if (mode.equals("selection")) {
|
|---|
| 115 | sel = getCurrentDataSet().getSelected();
|
|---|
| 116 | } else if (mode.equals("conflict")) {
|
|---|
| 117 | if (Main.map.conflictDialog.getConflicts() != null) {
|
|---|
| 118 | sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | if (sel.isEmpty()) {
|
|---|
| 122 | JOptionPane.showMessageDialog(
|
|---|
| 123 | Main.parent,
|
|---|
| 124 | (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
|
|---|
| 125 | tr("Information"),
|
|---|
| 126 | JOptionPane.INFORMATION_MESSAGE
|
|---|
| 127 | );
|
|---|
| 128 | return null;
|
|---|
| 129 | }
|
|---|
| 130 | for (OsmPrimitive osm : sel) {
|
|---|
| 131 | osm.visit(v);
|
|---|
| 132 | }
|
|---|
| 133 | // increase bbox by 0.001 degrees on each side. this is required
|
|---|
| 134 | // especially if the bbox contains one single node, but helpful
|
|---|
| 135 | // in most other cases as well.
|
|---|
| 136 | v.enlargeBoundingBox();
|
|---|
| 137 | }
|
|---|
| 138 | else if (mode.equals("download")) {
|
|---|
| 139 | if (Main.pref.hasKey("osm-download.bounds")) {
|
|---|
| 140 | try {
|
|---|
| 141 | String bounds[] = Main.pref.get("osm-download.bounds").split(";");
|
|---|
| 142 | double minlat = Double.parseDouble(bounds[0]);
|
|---|
| 143 | double minlon = Double.parseDouble(bounds[1]);
|
|---|
| 144 | double maxlat = Double.parseDouble(bounds[2]);
|
|---|
| 145 | double maxlon = Double.parseDouble(bounds[3]);
|
|---|
| 146 |
|
|---|
| 147 | v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
|
|---|
| 148 | v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
|
|---|
| 149 | }
|
|---|
| 150 | catch (Exception e) {
|
|---|
| 151 | e.printStackTrace();
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 | return v;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | @Override
|
|---|
| 159 | protected void updateEnabledState() {
|
|---|
| 160 | if ("selection".equals(mode)) {
|
|---|
| 161 | setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
|
|---|
| 162 | } else if ("layer".equals(mode)) {
|
|---|
| 163 | if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getAllLayersAsList().isEmpty()) {
|
|---|
| 164 | setEnabled(false);
|
|---|
| 165 | } else {
|
|---|
| 166 | // FIXME: should also check for whether a layer is selected in the layer list dialog
|
|---|
| 167 | setEnabled(true);
|
|---|
| 168 | }
|
|---|
| 169 | } else {
|
|---|
| 170 | setEnabled(
|
|---|
| 171 | Main.map != null
|
|---|
| 172 | && Main.map.mapView != null
|
|---|
| 173 | && Main.map.mapView.hasLayers()
|
|---|
| 174 | );
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | @Override
|
|---|
| 179 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 180 | if ("selection".equals(mode)) {
|
|---|
| 181 | setEnabled(selection != null && !selection.isEmpty());
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|