[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 |
|
---|
[948] | 61 | public void actionPerformed(ActionEvent e) {
|
---|
| 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 |
|
---|
| 71 | private BoundingXYVisitor getBoundingBox() {
|
---|
| 72 | BoundingXYVisitor v = new BoundingXYVisitor();
|
---|
| 73 | if (mode.equals("data")) {
|
---|
[1750] | 74 | for (Layer l : Main.map.mapView.getAllLayers()) {
|
---|
[948] | 75 | l.visitBoundingBox(v);
|
---|
[1750] | 76 | }
|
---|
| 77 | } else if (mode.equals("layer")) {
|
---|
[948] | 78 | Main.map.mapView.getActiveLayer().visitBoundingBox(v);
|
---|
[1750] | 79 | } else if (mode.equals("selection") || mode.equals("conflict")) {
|
---|
| 80 | Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
---|
| 81 | if (mode.equals("selection")) {
|
---|
[1814] | 82 | sel = getCurrentDataSet().getSelected();
|
---|
[1750] | 83 | } else if (mode.equals("conflict")) {
|
---|
| 84 | if (Main.map.conflictDialog.getConflicts() != null) {
|
---|
| 85 | sel = Main.map.conflictDialog.getConflicts().getMyConflictParties();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[948] | 88 | if (sel.isEmpty()) {
|
---|
[1847] | 89 | OptionPaneUtil.showMessageDialog(
|
---|
| 90 | Main.parent,
|
---|
| 91 | (mode.equals("selection") ? tr("Nothing selected to zoom to.") : tr("No conflicts to zoom to")),
|
---|
| 92 | tr("Information"),
|
---|
| 93 | JOptionPane.INFORMATION_MESSAGE
|
---|
| 94 | );
|
---|
[948] | 95 | return null;
|
---|
| 96 | }
|
---|
[1750] | 97 | for (OsmPrimitive osm : sel) {
|
---|
[948] | 98 | osm.visit(v);
|
---|
[1750] | 99 | }
|
---|
[1023] | 100 | // increase bbox by 0.001 degrees on each side. this is required
|
---|
| 101 | // especially if the bbox contains one single node, but helpful
|
---|
[948] | 102 | // in most other cases as well.
|
---|
| 103 | v.enlargeBoundingBox();
|
---|
| 104 | }
|
---|
[1302] | 105 | else if (mode.equals("download")) {
|
---|
| 106 | if (Main.pref.hasKey("osm-download.bounds")) {
|
---|
| 107 | try {
|
---|
| 108 | String bounds[] = Main.pref.get("osm-download.bounds").split(";");
|
---|
| 109 | double minlat = Double.parseDouble(bounds[0]);
|
---|
| 110 | double minlon = Double.parseDouble(bounds[1]);
|
---|
| 111 | double maxlat = Double.parseDouble(bounds[2]);
|
---|
| 112 | double maxlon = Double.parseDouble(bounds[3]);
|
---|
[1662] | 113 |
|
---|
[1302] | 114 | v.visit(Main.proj.latlon2eastNorth(new LatLon(minlat, minlon)));
|
---|
| 115 | v.visit(Main.proj.latlon2eastNorth(new LatLon(maxlat, maxlon)));
|
---|
| 116 | }
|
---|
| 117 | catch (Exception e) {
|
---|
| 118 | e.printStackTrace();
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
[948] | 122 | return v;
|
---|
| 123 | }
|
---|
[1820] | 124 |
|
---|
| 125 | @Override
|
---|
| 126 | protected void updateEnabledState() {
|
---|
| 127 | setEnabled(Main.map != null);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[403] | 130 | }
|
---|