1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.download;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.FocusAdapter;
|
---|
10 | import java.awt.event.FocusEvent;
|
---|
11 | import java.awt.event.FocusListener;
|
---|
12 | import java.awt.event.MouseAdapter;
|
---|
13 | import java.util.HashMap;
|
---|
14 |
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JTextArea;
|
---|
18 | import javax.swing.JTextField;
|
---|
19 | import javax.swing.SwingUtilities;
|
---|
20 | import javax.swing.event.DocumentListener;
|
---|
21 | import javax.swing.event.DocumentEvent;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.tools.GBC;
|
---|
26 | /**
|
---|
27 | * Bounding box selector.
|
---|
28 | *
|
---|
29 | * Provides max/min lat/lon input fields as well as the "URL from www.openstreetmap.org" text field.
|
---|
30 | *
|
---|
31 | * @author Frederik Ramm <frederik@remote.org>
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class BoundingBoxSelection implements DownloadSelection {
|
---|
35 |
|
---|
36 | private JTextField[] latlon = new JTextField[] {
|
---|
37 | new JTextField(11),
|
---|
38 | new JTextField(11),
|
---|
39 | new JTextField(11),
|
---|
40 | new JTextField(11) };
|
---|
41 | final JTextArea osmUrl = new JTextArea();
|
---|
42 | final JTextArea showUrl = new JTextArea();
|
---|
43 | String noteUrl = tr("You can paste an URL here to download the area.");
|
---|
44 |
|
---|
45 | public void addGui(final DownloadDialog gui) {
|
---|
46 |
|
---|
47 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
48 | osmUrl.setText(noteUrl);
|
---|
49 |
|
---|
50 | final FocusListener dialogUpdater = new FocusAdapter() {
|
---|
51 | @Override public void focusLost(FocusEvent e) {
|
---|
52 | SwingUtilities.invokeLater(new Runnable() {
|
---|
53 | public void run() {
|
---|
54 | try {
|
---|
55 | double minlat = Double.parseDouble(latlon[0].getText());
|
---|
56 | double minlon = Double.parseDouble(latlon[1].getText());
|
---|
57 | double maxlat = Double.parseDouble(latlon[2].getText());
|
---|
58 | double maxlon = Double.parseDouble(latlon[3].getText());
|
---|
59 | if (minlat != gui.minlat || minlon != gui.minlon || maxlat != gui.maxlat || maxlon != gui.maxlon) {
|
---|
60 | gui.minlat = minlat; gui.minlon = minlon;
|
---|
61 | gui.maxlat = maxlat; gui.maxlon = maxlon;
|
---|
62 | gui.boundingBoxChanged(BoundingBoxSelection.this);
|
---|
63 | }
|
---|
64 | } catch (NumberFormatException x) {
|
---|
65 | // ignore
|
---|
66 | }
|
---|
67 | updateUrl(gui);
|
---|
68 | }
|
---|
69 | });
|
---|
70 | }
|
---|
71 | };
|
---|
72 |
|
---|
73 | for (JTextField f : latlon) {
|
---|
74 | f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height));
|
---|
75 | f.addFocusListener(dialogUpdater);
|
---|
76 | }
|
---|
77 | class osmUrlRefresher implements DocumentListener {
|
---|
78 | public void changedUpdate(DocumentEvent e) { dowork(); }
|
---|
79 | public void insertUpdate(DocumentEvent e) { dowork(); }
|
---|
80 | public void removeUpdate(DocumentEvent e) { dowork(); }
|
---|
81 | private void dowork() {
|
---|
82 | Bounds b = osmurl2bounds(osmUrl.getText());
|
---|
83 | if (b != null) {
|
---|
84 | gui.minlon = b.min.lon();
|
---|
85 | gui.minlat = b.min.lat();
|
---|
86 | gui.maxlon = b.max.lon();
|
---|
87 | gui.maxlat = b.max.lat();
|
---|
88 | gui.boundingBoxChanged(BoundingBoxSelection.this);
|
---|
89 | updateBboxFields(gui);
|
---|
90 | updateUrl(gui);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | osmUrl.getDocument().addDocumentListener(new osmUrlRefresher());
|
---|
96 |
|
---|
97 | // select content on receiving focus. this seems to be the default in the
|
---|
98 | // windows look+feel but not for others. needs invokeLater to avoid strange
|
---|
99 | // side effects that will cancel out the newly made selection otherwise.
|
---|
100 | osmUrl.addFocusListener(new FocusAdapter() {
|
---|
101 | @Override public void focusGained(FocusEvent e) {
|
---|
102 | SwingUtilities.invokeLater(new Runnable() {
|
---|
103 | public void run() {
|
---|
104 | osmUrl.selectAll();
|
---|
105 | }
|
---|
106 | });
|
---|
107 | }
|
---|
108 | });
|
---|
109 | osmUrl.setLineWrap(true);
|
---|
110 | osmUrl.setBorder(latlon[0].getBorder());
|
---|
111 |
|
---|
112 | dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0));
|
---|
113 | dlg.add(latlon[0], GBC.std().insets(0,20,0,0));
|
---|
114 | dlg.add(new JLabel(tr("min lon")), GBC.std().insets(10,20,5,0));
|
---|
115 | dlg.add(latlon[1], GBC.eol().insets(0,20,0,0));
|
---|
116 | dlg.add(new JLabel(tr("max lat")), GBC.std().insets(10,0,5,0));
|
---|
117 | dlg.add(latlon[2], GBC.std());
|
---|
118 | dlg.add(new JLabel(tr("max lon")), GBC.std().insets(10,0,5,0));
|
---|
119 | dlg.add(latlon[3], GBC.eol());
|
---|
120 |
|
---|
121 | dlg.add(new JLabel(tr("URL from www.openstreetmap.org")), GBC.eol().insets(10,20,5,0));
|
---|
122 | dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill());
|
---|
123 | dlg.add(showUrl, GBC.eop().insets(10,0,5,5));
|
---|
124 | showUrl.setEditable(false);
|
---|
125 | showUrl.setBackground(dlg.getBackground());
|
---|
126 | showUrl.addFocusListener(new FocusAdapter(){
|
---|
127 | @Override
|
---|
128 | public void focusGained(FocusEvent e) {
|
---|
129 | showUrl.selectAll();
|
---|
130 | }
|
---|
131 | });
|
---|
132 |
|
---|
133 | gui.tabpane.addTab(tr("Bounding Box"), dlg);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Called when bounding box is changed by one of the other download dialog tabs.
|
---|
138 | */
|
---|
139 | public void boundingBoxChanged(DownloadDialog gui) {
|
---|
140 | updateBboxFields(gui);
|
---|
141 | updateUrl(gui);
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void updateBboxFields(DownloadDialog gui) {
|
---|
145 | latlon[0].setText(Double.toString(gui.minlat));
|
---|
146 | latlon[1].setText(Double.toString(gui.minlon));
|
---|
147 | latlon[2].setText(Double.toString(gui.maxlat));
|
---|
148 | latlon[3].setText(Double.toString(gui.maxlon));
|
---|
149 | for (JTextField f : latlon)
|
---|
150 | f.setCaretPosition(0);
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void updateUrl(DownloadDialog gui) {
|
---|
154 | double lat = (gui.minlat + gui.maxlat)/2;
|
---|
155 | double lon = (gui.minlon + gui.maxlon)/2;
|
---|
156 | // convert to mercator (for calculation of zoom only)
|
---|
157 | double latMin = Math.log(Math.tan(Math.PI/4.0+gui.minlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
158 | double latMax = Math.log(Math.tan(Math.PI/4.0+gui.maxlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
159 | double size = Math.max(Math.abs(latMax-latMin), Math.abs(gui.maxlon-gui.minlon));
|
---|
160 | int zoom = 0;
|
---|
161 | while (zoom <= 20) {
|
---|
162 | if (size >= 180)
|
---|
163 | break;
|
---|
164 | size *= 2;
|
---|
165 | zoom++;
|
---|
166 | }
|
---|
167 | showUrl.setText("http://www.openstreetmap.org/index.html?mlat="+lat+"&mlon="+lon+"&zoom="+zoom);
|
---|
168 | }
|
---|
169 |
|
---|
170 | public static Bounds osmurl2bounds(String url) {
|
---|
171 | int i = url.indexOf('?');
|
---|
172 | if (i == -1)
|
---|
173 | return null;
|
---|
174 | String[] args = url.substring(i+1).split("&");
|
---|
175 | HashMap<String, String> map = new HashMap<String, String>();
|
---|
176 | for (String arg : args) {
|
---|
177 | int eq = arg.indexOf('=');
|
---|
178 | if (eq != -1) {
|
---|
179 | map.put(arg.substring(0, eq), arg.substring(eq + 1));
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | Bounds b = null;
|
---|
184 | try {
|
---|
185 | if (map.containsKey("bbox")) {
|
---|
186 | String bbox[] = map.get("bbox").split(",");
|
---|
187 | b = new Bounds(
|
---|
188 | new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0])),
|
---|
189 | new LatLon(Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2])));
|
---|
190 |
|
---|
191 | } else {
|
---|
192 | double size = 180.0 / Math.pow(2, Integer.parseInt(map.get("zoom")));
|
---|
193 | b = new Bounds(
|
---|
194 | new LatLon(parseDouble(map, "lat") - size/2, parseDouble(map, "lon") - size),
|
---|
195 | new LatLon(parseDouble(map, "lat") + size/2, parseDouble(map, "lon") + size));
|
---|
196 | }
|
---|
197 | } catch (NumberFormatException x) {
|
---|
198 | } catch (NullPointerException x) {
|
---|
199 | }
|
---|
200 | return b;
|
---|
201 | }
|
---|
202 |
|
---|
203 | private static double parseDouble(HashMap<String, String> map, String key) {
|
---|
204 | if (map.containsKey(key))
|
---|
205 | return Double.parseDouble(map.get(key));
|
---|
206 | return Double.parseDouble(map.get("m"+key));
|
---|
207 | }
|
---|
208 | }
|
---|