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.KeyEvent;
|
---|
13 | import java.awt.event.KeyListener;
|
---|
14 | import java.awt.event.MouseAdapter;
|
---|
15 | import java.util.HashMap;
|
---|
16 |
|
---|
17 | import javax.swing.JLabel;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JTextArea;
|
---|
20 | import javax.swing.JTextField;
|
---|
21 | import javax.swing.SwingUtilities;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.tools.GBC;
|
---|
26 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Bounding box selector.
|
---|
30 | *
|
---|
31 | * Provides max/min lat/lon input fields as well as the "URL from www.openstreetmap.org" text field.
|
---|
32 | *
|
---|
33 | * @author Frederik Ramm <frederik@remote.org>
|
---|
34 | *
|
---|
35 | */
|
---|
36 | public class BoundingBoxSelection implements DownloadSelection {
|
---|
37 |
|
---|
38 | private JTextField[] latlon = new JTextField[] {
|
---|
39 | new JTextField(11),
|
---|
40 | new JTextField(11),
|
---|
41 | new JTextField(11),
|
---|
42 | new JTextField(11) };
|
---|
43 | final JTextArea osmUrl = new JTextArea();
|
---|
44 | final JTextArea showUrl = new JTextArea();
|
---|
45 |
|
---|
46 | public void addGui(final DownloadDialog gui) {
|
---|
47 |
|
---|
48 | JPanel dlg = new JPanel(new GridBagLayout());
|
---|
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 |
|
---|
78 | KeyListener osmUrlKeyListener = new KeyListener() {
|
---|
79 | public void keyPressed(KeyEvent keyEvent) {}
|
---|
80 |
|
---|
81 | public void keyReleased(KeyEvent keyEvent) {
|
---|
82 | Bounds b = OsmUrlToBounds.parse(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 | if(keyEvent.getKeyCode() == keyEvent.VK_ENTER)
|
---|
92 | gui.closeDownloadDialog(true);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void keyTyped(KeyEvent keyEvent) {}
|
---|
97 | };
|
---|
98 |
|
---|
99 | osmUrl.addKeyListener(osmUrlKeyListener);
|
---|
100 |
|
---|
101 | // select content on receiving focus. this seems to be the default in the
|
---|
102 | // windows look+feel but not for others. needs invokeLater to avoid strange
|
---|
103 | // side effects that will cancel out the newly made selection otherwise.
|
---|
104 | osmUrl.addFocusListener(new FocusAdapter() {
|
---|
105 | @Override public void focusGained(FocusEvent e) {
|
---|
106 | SwingUtilities.invokeLater(new Runnable() {
|
---|
107 | public void run() {
|
---|
108 | osmUrl.selectAll();
|
---|
109 | }
|
---|
110 | });
|
---|
111 | }
|
---|
112 | });
|
---|
113 | osmUrl.setLineWrap(true);
|
---|
114 | osmUrl.setBorder(latlon[0].getBorder());
|
---|
115 |
|
---|
116 | dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0));
|
---|
117 | dlg.add(latlon[0], GBC.std().insets(0,20,0,0));
|
---|
118 | dlg.add(new JLabel(tr("min lon")), GBC.std().insets(10,20,5,0));
|
---|
119 | dlg.add(latlon[1], GBC.eol().insets(0,20,0,0));
|
---|
120 | dlg.add(new JLabel(tr("max lat")), GBC.std().insets(10,0,5,0));
|
---|
121 | dlg.add(latlon[2], GBC.std());
|
---|
122 | dlg.add(new JLabel(tr("max lon")), GBC.std().insets(10,0,5,0));
|
---|
123 | dlg.add(latlon[3], GBC.eol());
|
---|
124 |
|
---|
125 | 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));
|
---|
126 | dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill());
|
---|
127 | dlg.add(showUrl, GBC.eop().insets(10,0,5,5));
|
---|
128 | showUrl.setEditable(false);
|
---|
129 | showUrl.setBackground(dlg.getBackground());
|
---|
130 | showUrl.addFocusListener(new FocusAdapter(){
|
---|
131 | @Override
|
---|
132 | public void focusGained(FocusEvent e) {
|
---|
133 | showUrl.selectAll();
|
---|
134 | }
|
---|
135 | });
|
---|
136 |
|
---|
137 | gui.tabpane.addTab(tr("Bounding Box"), dlg);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Called when bounding box is changed by one of the other download dialog tabs.
|
---|
142 | */
|
---|
143 | public void boundingBoxChanged(DownloadDialog gui) {
|
---|
144 | updateBboxFields(gui);
|
---|
145 | updateUrl(gui);
|
---|
146 | }
|
---|
147 |
|
---|
148 | private void updateBboxFields(DownloadDialog gui) {
|
---|
149 | latlon[0].setText(Double.toString(gui.minlat));
|
---|
150 | latlon[1].setText(Double.toString(gui.minlon));
|
---|
151 | latlon[2].setText(Double.toString(gui.maxlat));
|
---|
152 | latlon[3].setText(Double.toString(gui.maxlon));
|
---|
153 | for (JTextField f : latlon)
|
---|
154 | f.setCaretPosition(0);
|
---|
155 | }
|
---|
156 |
|
---|
157 | private void updateUrl(DownloadDialog gui) {
|
---|
158 | double lat = (gui.minlat + gui.maxlat)/2;
|
---|
159 | double lon = (gui.minlon + gui.maxlon)/2;
|
---|
160 | // convert to mercator (for calculation of zoom only)
|
---|
161 | double latMin = Math.log(Math.tan(Math.PI/4.0+gui.minlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
162 | double latMax = Math.log(Math.tan(Math.PI/4.0+gui.maxlat/180.0*Math.PI/2.0))*180.0/Math.PI;
|
---|
163 | double size = Math.max(Math.abs(latMax-latMin), Math.abs(gui.maxlon-gui.minlon));
|
---|
164 | int zoom = 0;
|
---|
165 | while (zoom <= 20) {
|
---|
166 | if (size >= 180)
|
---|
167 | break;
|
---|
168 | size *= 2;
|
---|
169 | zoom++;
|
---|
170 | }
|
---|
171 | // Truncate lat and lon to something more sensible
|
---|
172 | int decimals = (int) Math.pow(10, (zoom / 3));
|
---|
173 | lat = Math.round(lat * decimals);
|
---|
174 | lat /= decimals;
|
---|
175 | lon = Math.round(lon * decimals);
|
---|
176 | lon /= decimals;
|
---|
177 | showUrl.setText("http://www.openstreetmap.org/?lat="+lat+"&lon="+lon+"&zoom="+zoom);
|
---|
178 | }
|
---|
179 | }
|
---|