1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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 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 | public BoundingBoxSelectionPanel() {
|
---|
81 | build();
|
---|
82 | }
|
---|
83 |
|
---|
84 | public void setBoundingBox(Bounds area) {
|
---|
85 | updateBboxFields(area);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public Bounds getBoundingBox() {
|
---|
89 | double minlon, minlat, maxlon,maxlat;
|
---|
90 | try {
|
---|
91 | minlat = Double.parseDouble(tfLatLon[0].getText().trim());
|
---|
92 | minlon = Double.parseDouble(tfLatLon[1].getText().trim());
|
---|
93 | maxlat = Double.parseDouble(tfLatLon[2].getText().trim());
|
---|
94 | maxlon = Double.parseDouble(tfLatLon[3].getText().trim());
|
---|
95 | } catch(NumberFormatException e) {
|
---|
96 | return null;
|
---|
97 | }
|
---|
98 | if (!LatLon.isValidLon(minlon) || !LatLon.isValidLon(maxlon)
|
---|
99 | || !LatLon.isValidLat(minlat) || ! LatLon.isValidLat(maxlat))
|
---|
100 | return null;
|
---|
101 | if (minlon > maxlon)
|
---|
102 | return null;
|
---|
103 | if (minlat > maxlat)
|
---|
104 | return null;
|
---|
105 | return new Bounds(minlon,minlat,maxlon,maxlat);
|
---|
106 | }
|
---|
107 |
|
---|
108 | private boolean parseURL() {
|
---|
109 | Bounds b = OsmUrlToBounds.parse(tfOsmUrl.getText());
|
---|
110 | if(b == null) return false;
|
---|
111 | updateBboxFields(b);
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void updateBboxFields(Bounds area) {
|
---|
116 | if (area == null) return;
|
---|
117 | tfLatLon[0].setText(area.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
118 | tfLatLon[1].setText(area.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
119 | tfLatLon[2].setText(area.getMax().latToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
120 | tfLatLon[3].setText(area.getMax().lonToString(CoordinateFormat.DECIMAL_DEGREES));
|
---|
121 | }
|
---|
122 |
|
---|
123 | static private class LatitudeValidator extends AbstractTextComponentValidator {
|
---|
124 |
|
---|
125 | public static void decorate(JTextComponent tc) {
|
---|
126 | new LatitudeValidator(tc);
|
---|
127 | }
|
---|
128 |
|
---|
129 | public LatitudeValidator(JTextComponent tc) {
|
---|
130 | super(tc);
|
---|
131 | }
|
---|
132 |
|
---|
133 | @Override
|
---|
134 | public void validate() {
|
---|
135 | double value = 0;
|
---|
136 | try {
|
---|
137 | value = Double.parseDouble(getComponent().getText());
|
---|
138 | } catch(NumberFormatException ex) {
|
---|
139 | feedbackInvalid(tr("The string ''{0}'' is not a valid double value.", getComponent().getText()));
|
---|
140 | return;
|
---|
141 | }
|
---|
142 | if (!LatLon.isValidLat(value)) {
|
---|
143 | feedbackInvalid(tr("Value for latitude in range [-90,90] required.", getComponent().getText()));
|
---|
144 | return;
|
---|
145 | }
|
---|
146 | feedbackValid("");
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public boolean isValid() {
|
---|
151 | double value = 0;
|
---|
152 | try {
|
---|
153 | value = Double.parseDouble(getComponent().getText());
|
---|
154 | } catch(NumberFormatException ex) {
|
---|
155 | return false;
|
---|
156 | }
|
---|
157 | if (!LatLon.isValidLat(value))
|
---|
158 | return false;
|
---|
159 | return true;
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | static private class LongitudeValidator extends AbstractTextComponentValidator{
|
---|
164 |
|
---|
165 | public static void decorate(JTextComponent tc) {
|
---|
166 | new LongitudeValidator(tc);
|
---|
167 | }
|
---|
168 |
|
---|
169 | public LongitudeValidator(JTextComponent tc) {
|
---|
170 | super(tc);
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public void validate() {
|
---|
175 | double value = 0;
|
---|
176 | try {
|
---|
177 | value = Double.parseDouble(getComponent().getText());
|
---|
178 | } catch(NumberFormatException ex) {
|
---|
179 | feedbackInvalid(tr("The string ''{0}'' is not a valid double value.", getComponent().getText()));
|
---|
180 | return;
|
---|
181 | }
|
---|
182 | if (!LatLon.isValidLon(value)) {
|
---|
183 | feedbackInvalid(tr("Value for longitude in range [-180,180] required.", getComponent().getText()));
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | feedbackValid("");
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public boolean isValid() {
|
---|
191 | double value = 0;
|
---|
192 | try {
|
---|
193 | value = Double.parseDouble(getComponent().getText());
|
---|
194 | } catch(NumberFormatException ex) {
|
---|
195 | return false;
|
---|
196 | }
|
---|
197 | if (!LatLon.isValidLon(value))
|
---|
198 | return false;
|
---|
199 | return true;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | class OsmUrlRefresher implements DocumentListener {
|
---|
204 | @Override
|
---|
205 | public void changedUpdate(DocumentEvent e) { parseURL(); }
|
---|
206 | @Override
|
---|
207 | public void insertUpdate(DocumentEvent e) { parseURL(); }
|
---|
208 | @Override
|
---|
209 | public void removeUpdate(DocumentEvent e) { parseURL(); }
|
---|
210 | }
|
---|
211 | }
|
---|