source: josm/trunk/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java@ 679

Last change on this file since 679 was 679, checked in by stoecker, 16 years ago

finished XML based translations of presets fixes #960
correct bounding box illegal access fixes #1044
do no longer modify objects when unnessesary (can result in 0 objects
modified in Undo :-)
correct typo fixes #730
some I18N corrections

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