[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;
|
---|
[403] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.event.ActionEvent;
|
---|
[458] | 8 | import java.awt.event.KeyEvent;
|
---|
[403] | 9 | import java.util.Collection;
|
---|
[1750] | 10 | import java.util.HashSet;
|
---|
[403] | 11 |
|
---|
| 12 | import javax.swing.JOptionPane;
|
---|
| 13 |
|
---|
| 14 | import org.openstreetmap.josm.Main;
|
---|
[1302] | 15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[403] | 16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
[1847] | 18 | import org.openstreetmap.josm.gui.OptionPaneUtil;
|
---|
[403] | 19 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
[1084] | 20 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[403] | 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Toggles the autoScale feature of the mapView
|
---|
| 24 | * @author imi
|
---|
| 25 | */
|
---|
| 26 | public class AutoScaleAction extends JosmAction {
|
---|
| 27 |
|
---|
[1302] | 28 | public static final String[] modes = { marktr("data"), marktr("layer"), marktr("selection"), marktr("conflict"), marktr("download") };
|
---|
[948] | 29 | private final String mode;
|
---|
[403] | 30 |
|
---|
[948] | 31 | private static int getModeShortcut(String mode) {
|
---|
| 32 | int shortcut = -1;
|
---|
[458] | 33 |
|
---|
[948] | 34 | if (mode.equals("data")) {
|
---|
| 35 | shortcut = KeyEvent.VK_1;
|
---|
| 36 | }
|
---|
| 37 | if (mode.equals("layer")) {
|
---|
| 38 | shortcut = KeyEvent.VK_2;
|
---|
| 39 | }
|
---|
| 40 | if (mode.equals("selection")) {
|
---|
| 41 | shortcut = KeyEvent.VK_3;
|
---|
| 42 | }
|
---|
| 43 | if (mode.equals("conflict")) {
|
---|
| 44 | shortcut = KeyEvent.VK_4;
|
---|
| 45 | }
|
---|
[1302] | 46 | if (mode.equals("download")) {
|
---|
| 47 | shortcut = KeyEvent.VK_5;
|
---|
| 48 | }
|
---|
[458] | 49 |
|
---|
[948] | 50 | return shortcut;
|
---|
| 51 | }
|
---|
[403] | 52 |
|
---|
[948] | 53 | public AutoScaleAction(String mode) {
|
---|
| 54 | super(tr("Zoom to {0}", tr(mode)), "dialogs/autoscale/" + mode, tr("Zoom the view to {0}.", tr(mode)),
|
---|
[1169] | 55 | Shortcut.registerShortcut("view:zoom"+mode, tr("View: {0}", tr("Zoom to {0}", tr(mode))), getModeShortcut(mode), Shortcut.GROUP_EDIT), true);
|
---|
[948] | 56 | String modeHelp = Character.toUpperCase(mode.charAt(0)) + mode.substring(1);
|
---|
| 57 | putValue("help", "Action/AutoScale/" + modeHelp);
|
---|
| 58 | this.mode = mode;
|
---|
| 59 | }
|
---|
[403] | 60 |
|
---|
[1868] | 61 | public void autoScale() {
|
---|
[948] | 62 | if (Main.map != null) {
|
---|
| 63 | BoundingXYVisitor bbox = getBoundingBox();
|
---|
[1797] | 64 | if (bbox != null && bbox.getBounds() != null) {
|
---|
[948] | 65 | Main.map.mapView.recalculateCenterScale(bbox);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | putValue("active", true);
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[1868] | 71 | public void actionPerformed(ActionEvent e) {
|
---|
| 72 | autoScale();
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[948] | 75 | private BoundingXYVisitor getBoundingBox() {
|
---|
| 76 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
| 77 | if (mode.equals("data")) {
|
---|
[1750] | 78 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
---|
[948] | 79 | l.visitBoundingBox(v);
|
---|
[1750] | 80 | }
|
---|
| 81 | } else if (mode.equals("layer")) {
|
---|
[948] | 82 | Main.map.mapView.getActiveLayer().visitBoundingBox(v);
|
---|
[1750] | 83 | } else if (mode.equals("selection") || mode.equals("conflict")) {
|
---|
| 84 | Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
---|
| 85 | if (mode.equals("selection")) {
|
---|
[1814] | 86 | sel = getCurrentDataSet().getSelected();
|
---|
[1750] | 87 | } else if (mode.equals("conflict")) {
|
---|
| 88 | if (Main.map.conflictDialog.getConflicts() != null) {
|
---|
| 89 | sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
[948] | 92 | if (sel.isEmpty()) {
|
---|
[1847] | 93 | OptionPaneUtil.showMessageDialog(
|
---|
| 94 | Main.parent,
|
---|
| 95 | (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
|
---|
| 96 | tr("Information"),
|
---|
| 97 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 98 | );
|
---|
[948] | 99 | return null;
|
---|
| 100 | }
|
---|
[1750] | 101 | for (OsmPrimitive osm : sel) {
|
---|
[948] | 102 | osm.visit(v);
|
---|
[1750] | 103 | }
|
---|
[1023] | 104 | // increase bbox by 0.001 degrees on each side. this is required
|
---|
| 105 | // especially if the bbox contains one single node, but helpful
|
---|
[948] | 106 | // in most other cases as well.
|
---|
| 107 | v.enlargeBoundingBox();
|
---|
| 108 | }
|
---|
[1302] | 109 | else if (mode.equals("download")) {
|
---|
| 110 | if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
| 111 | try {
|
---|
| 112 | String bounds[] = Main.pref.get("osm-download.bounds").split(";");
|
---|
| 113 | double minlat = Double.parseDouble(bounds[0]);
|
---|
| 114 | double minlon = Double.parseDouble(bounds[1]);
|
---|
| 115 | double maxlat = Double.parseDouble(bounds[2]);
|
---|
| 116 | double maxlon = Double.parseDouble(bounds[3]);
|
---|
[1662] | 117 |
|
---|
[1302] | 118 | v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
|
---|
| 119 | v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
|
---|
| 120 | }
|
---|
| 121 | catch (Exception e) {
|
---|
| 122 | e.printStackTrace();
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
[948] | 126 | return v;
|
---|
| 127 | }
|
---|
[1820] | 128 |
|
---|
| 129 | @Override
|
---|
| 130 | protected void updateEnabledState() {
|
---|
[1854] | 131 | if ("selection".equals(mode)) {
|
---|
| 132 | setEnabled(getCurrentDataSet() != null && ! getCurrentDataSet().getSelected().isEmpty());
|
---|
| 133 | } else {
|
---|
| 134 | setEnabled(
|
---|
| 135 | Main.map != null
|
---|
| 136 | && Main.map.mapView != null
|
---|
[1895] | 137 | && Main.map.mapView.hasLayers()
|
---|
[1854] | 138 | );
|
---|
| 139 | }
|
---|
[1820] | 140 | }
|
---|
[403] | 141 | }
|
---|