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