1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 |
|
---|
11 | import javax.swing.BorderFactory;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 | import javax.swing.event.DocumentEvent;
|
---|
15 | import javax.swing.event.DocumentListener;
|
---|
16 | import javax.swing.text.JTextComponent;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.data.Bounds;
|
---|
19 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.tools.GBC;
|
---|
22 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | *
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class BoundingBoxSelectionPanel extends JPanel {
|
---|
29 |
|
---|
30 | private JosmTextField[] tfLatLon = null;
|
---|
31 | private final JosmTextField tfOsmUrl = new JosmTextField();
|
---|
32 |
|
---|
33 | protected void buildInputFields() {
|
---|
34 | tfLatLon = new JosmTextField[4];
|
---|
35 | for (int i = 0; i < 4; i++) {
|
---|
36 | tfLatLon[i] = new JosmTextField(11);
|
---|
37 | tfLatLon[i].setMinimumSize(new Dimension(100, new JosmTextField().getMinimumSize().height));
|
---|
38 | SelectAllOnFocusGainedDecorator.decorate(tfLatLon[i]);
|
---|
39 | }
|
---|
40 | LatitudeValidator.decorate(tfLatLon[0]);
|
---|
41 | LatitudeValidator.decorate(tfLatLon[2]);
|
---|
42 | LongitudeValidator.decorate(tfLatLon[1]);
|
---|
43 | LongitudeValidator.decorate(tfLatLon[3]);
|
---|
44 | }
|
---|
45 |
|
---|
46 | protected final void build() {
|
---|
47 | buildInputFields();
|
---|
48 | setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
49 | setLayout(new GridBagLayout());
|
---|
50 | tfOsmUrl.getDocument().addDocumentListener(new OsmUrlRefresher());
|
---|
51 |
|
---|
52 | // select content on receiving focus. this seems to be the default in the
|
---|
53 | // windows look+feel but not for others. needs invokeLater to avoid strange
|
---|
54 | // side effects that will cancel out the newly made selection otherwise.
|
---|
55 | tfOsmUrl.addFocusListener(new SelectAllOnFocusGainedDecorator());
|
---|
56 |
|
---|
57 | add(new JLabel(tr("Min. latitude")), GBC.std().insets(0, 0, 3, 5));
|
---|
58 | add(tfLatLon[0], GBC.std().insets(0, 0, 3, 5));
|
---|
59 | add(new JLabel(tr("Min. longitude")), GBC.std().insets(0, 0, 3, 5));
|
---|
60 | add(tfLatLon[1], GBC.eol());
|
---|
61 | add(new JLabel(tr("Max. latitude")), GBC.std().insets(0, 0, 3, 5));
|
---|
62 | add(tfLatLon[2], GBC.std().insets(0, 0, 3, 5));
|
---|
63 | add(new JLabel(tr("Max. longitude")), GBC.std().insets(0, 0, 3, 5));
|
---|
64 | add(tfLatLon[3], GBC.eol());
|
---|
65 |
|
---|
66 | GridBagConstraints gc = new GridBagConstraints();
|
---|
67 | gc.gridx = 0;
|
---|
68 | gc.gridy = 2;
|
---|
69 | gc.gridwidth = 4;
|
---|
70 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
71 | gc.weightx = 1.0;
|
---|
72 | gc.insets = new Insets(10, 0, 0, 3);
|
---|
73 | add(new JMultilineLabel(tr("URL from www.openstreetmap.org (you can paste a download URL here to specify a bounding box)")), gc);
|
---|
74 |
|
---|
75 | gc.gridy = 3;
|
---|
76 | gc.insets = new Insets(3, 0, 0, 3);
|
---|
77 | add(tfOsmUrl, gc);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Constructs a new {@code BoundingBoxSelectionPanel}.
|
---|
82 | */
|
---|
83 | public BoundingBoxSelectionPanel() {
|
---|
84 | build();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void setBoundingBox(Bounds area) {
|
---|
88 | updateBboxFields(area);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public Bounds getBoundingBox() {
|
---|
92 | double minlon, minlat, maxlon, maxlat;
|
---|
93 | try {
|
---|
94 | minlat = Double.parseDouble(tfLatLon[0].getText().trim());
|
---|
95 | minlon = Double.parseDouble(tfLatLon[1].getText().trim());
|
---|
96 | maxlat = Double.parseDouble(tfLatLon[2].getText().trim());
|
---|
97 | maxlon = Double.parseDouble(tfLatLon[3].getText().trim());
|
---|
98 | } catch (NumberFormatException e) {
|
---|
99 | return null;
|
---|
100 | }
|
---|
101 | if (!LatLon.isValidLon(minlon) || !LatLon.isValidLon(maxlon)
|
---|
102 | || !LatLon.isValidLat(minlat) || !LatLon.isValidLat(maxlat))
|
---|
103 | return null;
|
---|
104 | if (minlon > maxlon)
|
---|
105 | return null;
|
---|
106 | if (minlat > maxlat)
|
---|
107 | return null;
|
---|
108 | return new Bounds(minlon, minlat, maxlon, maxlat);
|
---|
109 | }
|
---|
110 |
|
---|
111 | private boolean parseURL() {
|
---|
112 | Bounds b = OsmUrlToBounds.parse(tfOsmUrl.getText());
|
---|
113 | if (b == null) return false;
|
---|
114 | updateBboxFields(b);
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void updateBboxFields(Bounds area) {
|
---|
119 | if (area == null) return;
|
---|
120 | tfLatLon[0].setText(area.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
121 | tfLatLon[1].setText(area.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
122 | tfLatLon[2].setText(area.getMax().latToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
123 | tfLatLon[3].setText(area.getMax().lonToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
124 | }
|
---|
125 |
|
---|
126 | private static class LatitudeValidator extends AbstractTextComponentValidator {
|
---|
127 |
|
---|
128 | public static void decorate(JTextComponent tc) {
|
---|
129 | new LatitudeValidator(tc);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public LatitudeValidator(JTextComponent tc) {
|
---|
133 | super(tc);
|
---|
134 | }
|
---|
135 |
|
---|
136 | @Override
|
---|
137 | public void validate() {
|
---|
138 | double value = 0;
|
---|
139 | try {
|
---|
140 | value = Double.parseDouble(getComponent().getText());
|
---|
141 | } catch (NumberFormatException ex) {
|
---|
142 | feedbackInvalid(tr("The string ''{0}'' is not a valid double value.", getComponent().getText()));
|
---|
143 | return;
|
---|
144 | }
|
---|
145 | if (!LatLon.isValidLat(value)) {
|
---|
146 | feedbackInvalid(tr("Value for latitude in range [-90,90] required.", getComponent().getText()));
|
---|
147 | return;
|
---|
148 | }
|
---|
149 | feedbackValid("");
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public boolean isValid() {
|
---|
154 | double value = 0;
|
---|
155 | try {
|
---|
156 | value = Double.parseDouble(getComponent().getText());
|
---|
157 | } catch (NumberFormatException ex) {
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 | if (!LatLon.isValidLat(value))
|
---|
161 | return false;
|
---|
162 | return true;
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | private static class LongitudeValidator extends AbstractTextComponentValidator{
|
---|
167 |
|
---|
168 | public static void decorate(JTextComponent tc) {
|
---|
169 | new LongitudeValidator(tc);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public LongitudeValidator(JTextComponent tc) {
|
---|
173 | super(tc);
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public void validate() {
|
---|
178 | double value = 0;
|
---|
179 | try {
|
---|
180 | value = Double.parseDouble(getComponent().getText());
|
---|
181 | } catch (NumberFormatException ex) {
|
---|
182 | feedbackInvalid(tr("The string ''{0}'' is not a valid double value.", getComponent().getText()));
|
---|
183 | return;
|
---|
184 | }
|
---|
185 | if (!LatLon.isValidLon(value)) {
|
---|
186 | feedbackInvalid(tr("Value for longitude in range [-180,180] required.", getComponent().getText()));
|
---|
187 | return;
|
---|
188 | }
|
---|
189 | feedbackValid("");
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public boolean isValid() {
|
---|
194 | double value = 0;
|
---|
195 | try {
|
---|
196 | value = Double.parseDouble(getComponent().getText());
|
---|
197 | } catch (NumberFormatException ex) {
|
---|
198 | return false;
|
---|
199 | }
|
---|
200 | if (!LatLon.isValidLon(value))
|
---|
201 | return false;
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | class OsmUrlRefresher implements DocumentListener {
|
---|
207 | @Override
|
---|
208 | public void changedUpdate(DocumentEvent e) { parseURL(); }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | public void insertUpdate(DocumentEvent e) { parseURL(); }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public void removeUpdate(DocumentEvent e) { parseURL(); }
|
---|
215 | }
|
---|
216 | }
|
---|