| 1 | package UtilsPlugin;
|
|---|
| 2 |
|
|---|
| 3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.BorderLayout;
|
|---|
| 6 | import java.awt.Component;
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.InputEvent;
|
|---|
| 9 | import java.awt.event.KeyEvent;
|
|---|
| 10 | import java.awt.event.MouseListener;
|
|---|
| 11 | import java.awt.event.MouseEvent;
|
|---|
| 12 | import java.awt.GridBagLayout;
|
|---|
| 13 | import java.awt.GridLayout;
|
|---|
| 14 | import java.awt.Toolkit;
|
|---|
| 15 |
|
|---|
| 16 | import javax.swing.Box;
|
|---|
| 17 | import javax.swing.event.DocumentListener;
|
|---|
| 18 | import javax.swing.event.DocumentEvent;
|
|---|
| 19 | import javax.swing.JComponent;
|
|---|
| 20 | import javax.swing.JDialog;
|
|---|
| 21 | import javax.swing.JLabel;
|
|---|
| 22 | import javax.swing.JOptionPane;
|
|---|
| 23 | import javax.swing.JPanel;
|
|---|
| 24 | import javax.swing.JTextField;
|
|---|
| 25 | import javax.swing.KeyStroke;
|
|---|
| 26 |
|
|---|
| 27 | import org.openstreetmap.josm.Main;
|
|---|
| 28 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 29 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 30 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 31 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 32 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
|---|
| 34 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 35 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
|---|
| 36 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 37 |
|
|---|
| 38 | public class JumpToAction extends JosmAction implements MouseListener {
|
|---|
| 39 | public JumpToAction() {
|
|---|
| 40 | super(tr("Jump To Position"), null, tr("Opens a dialog that allows to jump to a specific location"), Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
|
|---|
| 41 | KeyEvent.VK_G, Shortcut.GROUP_HOTKEY), true);
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public JTextField url = new JTextField();
|
|---|
| 45 | public JTextField lat = new JTextField();
|
|---|
| 46 | public JTextField lon = new JTextField();
|
|---|
| 47 | public void showJumpToDialog() {
|
|---|
| 48 | LatLon curPos=Main.proj.eastNorth2latlon(Main.map.mapView.getCenter());
|
|---|
| 49 | lat.setText(java.lang.Double.toString(curPos.lat()));
|
|---|
| 50 | lon.setText(java.lang.Double.toString(curPos.lon()));
|
|---|
| 51 | updateUrl(true);
|
|---|
| 52 |
|
|---|
| 53 | JPanel panel = new JPanel(new BorderLayout());
|
|---|
| 54 | panel.add(new JLabel("<html>"
|
|---|
| 55 | + tr("Enter Lat/Lon to jump to position.")
|
|---|
| 56 | + "<br>"
|
|---|
| 57 | + tr("You can also paste an URL from www.openstreetmap.org")
|
|---|
| 58 | + "<br>"
|
|---|
| 59 | + "</html>"),
|
|---|
| 60 | BorderLayout.NORTH);
|
|---|
| 61 |
|
|---|
| 62 | class osmURLListener implements DocumentListener {
|
|---|
| 63 | public void changedUpdate(DocumentEvent e) { parseURL(); }
|
|---|
| 64 | public void insertUpdate(DocumentEvent e) { parseURL(); }
|
|---|
| 65 | public void removeUpdate(DocumentEvent e) { parseURL(); }
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | class osmLonLatListener implements DocumentListener {
|
|---|
| 69 | public void changedUpdate(DocumentEvent e) { updateUrl(false); }
|
|---|
| 70 | public void insertUpdate(DocumentEvent e) { updateUrl(false); }
|
|---|
| 71 | public void removeUpdate(DocumentEvent e) { updateUrl(false); }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | osmLonLatListener x=new osmLonLatListener();
|
|---|
| 75 | lat.getDocument().addDocumentListener(x);
|
|---|
| 76 | lon.getDocument().addDocumentListener(x);
|
|---|
| 77 | url.getDocument().addDocumentListener(new osmURLListener());
|
|---|
| 78 |
|
|---|
| 79 | JPanel p = new JPanel(new GridBagLayout());
|
|---|
| 80 | panel.add(p, BorderLayout.NORTH);
|
|---|
| 81 |
|
|---|
| 82 | p.add(new JLabel(tr("Latitude")), GBC.eol());
|
|---|
| 83 | p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 84 |
|
|---|
| 85 | p.add(new JLabel(tr("Longitude")), GBC.eol());
|
|---|
| 86 | p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 87 |
|
|---|
| 88 | p.add(new JLabel(tr("URL")), GBC.eol());
|
|---|
| 89 | p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 90 |
|
|---|
| 91 | Object[] buttons = { tr("Jump there"), tr("Cancel") };
|
|---|
| 92 | LatLon ll = null;
|
|---|
| 93 | while(ll == null) {
|
|---|
| 94 | int option = JOptionPane.showOptionDialog(
|
|---|
| 95 | Main.parent,
|
|---|
| 96 | panel,
|
|---|
| 97 | tr("Jump to Position"),
|
|---|
| 98 | JOptionPane.OK_CANCEL_OPTION,
|
|---|
| 99 | JOptionPane.PLAIN_MESSAGE,
|
|---|
| 100 | null,
|
|---|
| 101 | buttons,
|
|---|
| 102 | buttons[0]);
|
|---|
| 103 |
|
|---|
| 104 | if (option != JOptionPane.OK_OPTION) return;
|
|---|
| 105 | try {
|
|---|
| 106 | ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
|
|---|
| 107 | } catch (Exception ex) {
|
|---|
| 108 | JOptionPane.showMessageDialog(Main.parent, tr("Could not parse Latitude or Longitude. Please check."), tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | Main.map.mapView.zoomTo(Main.proj.latlon2eastNorth(ll), Main.map.mapView.getScale());
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | private void parseURL() {
|
|---|
| 118 | if(!url.hasFocus()) return;
|
|---|
| 119 | Bounds b = OsmUrlToBounds.parse(url.getText());
|
|---|
| 120 | if (b != null) {
|
|---|
| 121 | lat.setText(java.lang.Double.toString((b.min.lat() + b.max.lat())/2));
|
|---|
| 122 | lon.setText(java.lang.Double.toString((b.min.lon() + b.max.lon())/2));
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | private void updateUrl(boolean force) {
|
|---|
| 127 | if(!lat.hasFocus() && !lon.hasFocus() && !force) return;
|
|---|
| 128 | try {
|
|---|
| 129 | double dlat = Double.parseDouble(lat.getText());
|
|---|
| 130 | double dlon = Double.parseDouble(lon.getText());
|
|---|
| 131 | int zoom = Main.map.mapView.zoom();
|
|---|
| 132 |
|
|---|
| 133 | int decimals = (int) Math.pow(10, (zoom / 3));
|
|---|
| 134 | dlat = Math.round(dlat * decimals);
|
|---|
| 135 | dlat /= decimals;
|
|---|
| 136 | dlon = Math.round(dlon * decimals);
|
|---|
| 137 | dlon /= decimals;
|
|---|
| 138 | url.setText("http://www.openstreetmap.org/?lat="+dlat+"&lon="+dlon+"&zoom="+zoom);
|
|---|
| 139 | } catch (NumberFormatException x) {}
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | public void actionPerformed(ActionEvent e) {
|
|---|
| 143 | showJumpToDialog();
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | public void mousePressed(MouseEvent e) {}
|
|---|
| 147 |
|
|---|
| 148 | public void mouseReleased(MouseEvent e) {}
|
|---|
| 149 |
|
|---|
| 150 | public void mouseEntered(MouseEvent e) {}
|
|---|
| 151 |
|
|---|
| 152 | public void mouseExited(MouseEvent e) {}
|
|---|
| 153 |
|
|---|
| 154 | public void mouseClicked(MouseEvent e) {
|
|---|
| 155 | showJumpToDialog();
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|