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.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.FocusAdapter;
|
---|
9 | import java.awt.event.FocusEvent;
|
---|
10 | import java.awt.event.FocusListener;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.awt.event.KeyListener;
|
---|
13 |
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.JTextArea;
|
---|
17 | import javax.swing.JTextField;
|
---|
18 | import javax.swing.SwingUtilities;
|
---|
19 | import javax.swing.event.DocumentListener;
|
---|
20 | import javax.swing.event.DocumentEvent;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.data.Bounds;
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
25 |
|
---|
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 |
|
---|
44 | public void addGui(final DownloadDialog gui) {
|
---|
45 |
|
---|
46 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
47 |
|
---|
48 | final FocusListener dialogUpdater = new FocusAdapter() {
|
---|
49 | @Override public void focusLost(FocusEvent e) {
|
---|
50 | SwingUtilities.invokeLater(new Runnable() {
|
---|
51 | public void run() {
|
---|
52 | try {
|
---|
53 | double minlat = Double.parseDouble(latlon[0].getText());
|
---|
54 | double minlon = Double.parseDouble(latlon[1].getText());
|
---|
55 | double maxlat = Double.parseDouble(latlon[2].getText());
|
---|
56 | double maxlon = Double.parseDouble(latlon[3].getText());
|
---|
57 | if (minlat != gui.minlat || minlon != gui.minlon || maxlat != gui.maxlat || maxlon != gui.maxlon) {
|
---|
58 | gui.minlat = minlat; gui.minlon = minlon;
|
---|
59 | gui.maxlat = maxlat; gui.maxlon = maxlon;
|
---|
60 | gui.boundingBoxChanged(BoundingBoxSelection.this);
|
---|
61 | }
|
---|
62 | } catch (NumberFormatException x) {
|
---|
63 | // ignore
|
---|
64 | }
|
---|
65 | updateUrl(gui);
|
---|
66 | }
|
---|
67 | });
|
---|
68 | }
|
---|
69 | };
|
---|
70 |
|
---|
71 | for (JTextField f : latlon) {
|
---|
72 | f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height));
|
---|
73 | f.addFocusListener(dialogUpdater);
|
---|
74 | }
|
---|
75 |
|
---|
76 | class osmUrlRefresher implements DocumentListener {
|
---|
77 | public void changedUpdate(DocumentEvent e) { parseURL(gui); }
|
---|
78 | public void insertUpdate(DocumentEvent e) { parseURL(gui); }
|
---|
79 | public void removeUpdate(DocumentEvent e) { parseURL(gui); }
|
---|
80 | }
|
---|
81 |
|
---|
82 | KeyListener osmUrlKeyListener = new KeyListener() {
|
---|
83 | public void keyPressed(KeyEvent keyEvent) {}
|
---|
84 | public void keyReleased(KeyEvent keyEvent) {
|
---|
85 | if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER && parseURL(gui))
|
---|
86 | gui.closeDownloadDialog(true);
|
---|
87 | }
|
---|
88 | public void keyTyped(KeyEvent keyEvent) {}
|
---|
89 | };
|
---|
90 |
|
---|
91 | osmUrl.addKeyListener(osmUrlKeyListener);
|
---|
92 | osmUrl.getDocument().addDocumentListener(new osmUrlRefresher());
|
---|
93 |
|
---|
94 | // select content on receiving focus. this seems to be the default in the
|
---|
95 | // windows look+feel but not for others. needs invokeLater to avoid strange
|
---|
96 | // side effects that will cancel out the newly made selection otherwise.
|
---|
97 | osmUrl.addFocusListener(new FocusAdapter() {
|
---|
98 | @Override public void focusGained(FocusEvent e) {
|
---|
99 | SwingUtilities.invokeLater(new Runnable() {
|
---|
100 | public void run() {
|
---|
101 | osmUrl.selectAll();
|
---|
102 | }
|
---|
103 | });
|
---|
104 | }
|
---|
105 | });
|
---|
106 | osmUrl.setLineWrap(true);
|
---|
107 | osmUrl.setBorder(latlon[0].getBorder());
|
---|
108 |
|
---|
109 | dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0));
|
---|
110 | dlg.add(latlon[0], GBC.std().insets(0,20,0,0));
|
---|
111 | dlg.add(new JLabel(tr("min lon")), GBC.std().insets(10,20,5,0));
|
---|
112 | dlg.add(latlon[1], GBC.eol().insets(0,20,0,0));
|
---|
113 | dlg.add(new JLabel(tr("max lat")), GBC.std().insets(10,0,5,0));
|
---|
114 | dlg.add(latlon[2], GBC.std());
|
---|
115 | dlg.add(new JLabel(tr("max lon")), GBC.std().insets(10,0,5,0));
|
---|
116 | dlg.add(latlon[3], GBC.eol());
|
---|
117 |
|
---|
118 | dlg.add(new JLabel(tr("URL from www.openstreetmap.org (you can paste an URL here to download the area)")), GBC.eol().insets(10,20,5,0));
|
---|
119 | dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill());
|
---|
120 | dlg.add(showUrl, GBC.eop().insets(10,0,5,5));
|
---|
121 | showUrl.setEditable(false);
|
---|
122 | showUrl.setBackground(dlg.getBackground());
|
---|
123 | showUrl.addFocusListener(new FocusAdapter(){
|
---|
124 | @Override
|
---|
125 | public void focusGained(FocusEvent e) {
|
---|
126 | showUrl.selectAll();
|
---|
127 | }
|
---|
128 | });
|
---|
129 |
|
---|
130 | gui.tabpane.addTab(tr("Bounding Box"), dlg);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Called when bounding box is changed by one of the other download dialog tabs.
|
---|
135 | */
|
---|
136 | public void boundingBoxChanged(DownloadDialog gui) {
|
---|
137 | updateBboxFields(gui);
|
---|
138 | updateUrl(gui);
|
---|
139 | }
|
---|
140 |
|
---|
141 | private boolean parseURL(DownloadDialog gui) {
|
---|
142 | Bounds b = OsmUrlToBounds.parse(osmUrl.getText());
|
---|
143 | if(b == null) return false;
|
---|
144 | gui.minlon = b.min.lon();
|
---|
145 | gui.minlat = b.min.lat();
|
---|
146 | gui.maxlon = b.max.lon();
|
---|
147 | gui.maxlat = b.max.lat();
|
---|
148 | gui.boundingBoxChanged(BoundingBoxSelection.this);
|
---|
149 | updateBboxFields(gui);
|
---|
150 | updateUrl(gui);
|
---|
151 | return true;
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void updateBboxFields(DownloadDialog gui) {
|
---|
155 | latlon[0].setText(Double.toString(gui.minlat));
|
---|
156 | latlon[1].setText(Double.toString(gui.minlon));
|
---|
157 | latlon[2].setText(Double.toString(gui.maxlat));
|
---|
158 | latlon[3].setText(Double.toString(gui.maxlon));
|
---|
159 | for (JTextField f : latlon)
|
---|
160 | f.setCaretPosition(0);
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void updateUrl(DownloadDialog gui) {
|
---|
164 | double lat = (gui.minlat + gui.maxlat)/2;
|
---|
165 | double lon = (gui.minlon + gui.maxlon)/2;
|
---|
166 | // convert to mercator (for calculation of zoom only)
|
---|
167 | double latMin = Math.log(Math.tan(Math.PI/4.0+gui.minlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
168 | double latMax = Math.log(Math.tan(Math.PI/4.0+gui.maxlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
169 | double size = Math.max(Math.abs(latMax-latMin), Math.abs(gui.maxlon-gui.minlon));
|
---|
170 | int zoom = 0;
|
---|
171 | while (zoom <= 20) {
|
---|
172 | if (size >= 180)
|
---|
173 | break;
|
---|
174 | size *= 2;
|
---|
175 | zoom++;
|
---|
176 | }
|
---|
177 | // Truncate lat and lon to something more sensible
|
---|
178 | int decimals = (int) Math.pow(10, (zoom / 3));
|
---|
179 | lat = Math.round(lat * decimals);
|
---|
180 | lat /= decimals;
|
---|
181 | lon = Math.round(lon * decimals);
|
---|
182 | lon /= decimals;
|
---|
183 | showUrl.setText("http://www.openstreetmap.org/?lat="+lat+"&lon="+lon+"&zoom="+zoom);
|
---|
184 | }
|
---|
185 | }
|
---|