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