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

Last change on this file since 237 was 237, checked in by framm, 18 years ago

Major download dialog reorg (now uses tabs, modified World Chooser with keyboard control, plugin interface allows adding of tabs)
New Userlist dialog shows people having edited selected items

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