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

Last change on this file since 6084 was 6084, checked in by bastiK, 11 years ago

see #8902 - add missing @Override annotations (patch by shinigami)

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