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