source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/BoundingBoxSelectionPanel.java@ 8840

Last change on this file since 8840 was 8840, checked in by Don-vip, 9 years ago

sonar - squid:S3052 - Fields should not be initialized to default values

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.Insets;
10
11import javax.swing.BorderFactory;
12import javax.swing.JLabel;
13import javax.swing.JPanel;
14import javax.swing.event.DocumentEvent;
15import javax.swing.event.DocumentListener;
16import javax.swing.text.JTextComponent;
17
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.coor.CoordinateFormat;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.tools.GBC;
22import org.openstreetmap.josm.tools.OsmUrlToBounds;
23
24/**
25 *
26 *
27 */
28public class BoundingBoxSelectionPanel extends JPanel {
29
30 private JosmTextField[] tfLatLon;
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 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 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) {
209 parseURL();
210 }
211
212 @Override
213 public void insertUpdate(DocumentEvent e) {
214 parseURL();
215 }
216
217 @Override
218 public void removeUpdate(DocumentEvent e) {
219 parseURL();
220 }
221 }
222}
Note: See TracBrowser for help on using the repository browser.