[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2711] | 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 |
|
---|
[8840] | 30 | private JosmTextField[] tfLatLon;
|
---|
[5886] | 31 | private final JosmTextField tfOsmUrl = new JosmTextField();
|
---|
[2711] | 32 |
|
---|
| 33 | protected void buildInputFields() {
|
---|
[5886] | 34 | tfLatLon = new JosmTextField[4];
|
---|
[8510] | 35 | for (int i = 0; i < 4; i++) {
|
---|
[5886] | 36 | tfLatLon[i] = new JosmTextField(11);
|
---|
[8510] | 37 | tfLatLon[i].setMinimumSize(new Dimension(100, new JosmTextField().getMinimumSize().height));
|
---|
[2711] | 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 |
|
---|
[6890] | 46 | protected final void build() {
|
---|
[2711] | 47 | buildInputFields();
|
---|
[8510] | 48 | setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
[2711] | 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 |
|
---|
[8510] | 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));
|
---|
[2711] | 60 | add(tfLatLon[1], GBC.eol());
|
---|
[8510] | 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));
|
---|
[2711] | 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;
|
---|
[8510] | 72 | gc.insets = new Insets(10, 0, 0, 3);
|
---|
[2711] | 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;
|
---|
[8510] | 76 | gc.insets = new Insets(3, 0, 0, 3);
|
---|
[2711] | 77 | add(tfOsmUrl, gc);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[8510] | 80 | /**
|
---|
| 81 | * Constructs a new {@code BoundingBoxSelectionPanel}.
|
---|
| 82 | */
|
---|
[2711] | 83 | public BoundingBoxSelectionPanel() {
|
---|
| 84 | build();
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | public void setBoundingBox(Bounds area) {
|
---|
| 88 | updateBboxFields(area);
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public Bounds getBoundingBox() {
|
---|
[8510] | 92 | double minlon, minlat, maxlon, maxlat;
|
---|
[2711] | 93 | try {
|
---|
[5544] | 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());
|
---|
[8510] | 98 | } catch (NumberFormatException e) {
|
---|
[2711] | 99 | return null;
|
---|
| 100 | }
|
---|
| 101 | if (!LatLon.isValidLon(minlon) || !LatLon.isValidLon(maxlon)
|
---|
[8444] | 102 | || !LatLon.isValidLat(minlat) || !LatLon.isValidLat(maxlat))
|
---|
[2711] | 103 | return null;
|
---|
| 104 | if (minlon > maxlon)
|
---|
| 105 | return null;
|
---|
| 106 | if (minlat > maxlat)
|
---|
| 107 | return null;
|
---|
[8510] | 108 | return new Bounds(minlon, minlat, maxlon, maxlat);
|
---|
[2711] | 109 | }
|
---|
| 110 |
|
---|
| 111 | private boolean parseURL() {
|
---|
| 112 | Bounds b = OsmUrlToBounds.parse(tfOsmUrl.getText());
|
---|
[8510] | 113 | if (b == null) return false;
|
---|
[2711] | 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 |
|
---|
[6883] | 126 | private static class LatitudeValidator extends AbstractTextComponentValidator {
|
---|
[2711] | 127 |
|
---|
| 128 | public static void decorate(JTextComponent tc) {
|
---|
| 129 | new LatitudeValidator(tc);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[8836] | 132 | LatitudeValidator(JTextComponent tc) {
|
---|
[2711] | 133 | super(tc);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | @Override
|
---|
| 137 | public void validate() {
|
---|
| 138 | double value = 0;
|
---|
| 139 | try {
|
---|
| 140 | value = Double.parseDouble(getComponent().getText());
|
---|
[8510] | 141 | } catch (NumberFormatException ex) {
|
---|
[2850] | 142 | feedbackInvalid(tr("The string ''{0}'' is not a valid double value.", getComponent().getText()));
|
---|
[2711] | 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());
|
---|
[8510] | 157 | } catch (NumberFormatException ex) {
|
---|
[2711] | 158 | return false;
|
---|
| 159 | }
|
---|
| 160 | if (!LatLon.isValidLat(value))
|
---|
| 161 | return false;
|
---|
| 162 | return true;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[9059] | 166 | private static class LongitudeValidator extends AbstractTextComponentValidator {
|
---|
[2711] | 167 |
|
---|
| 168 | public static void decorate(JTextComponent tc) {
|
---|
| 169 | new LongitudeValidator(tc);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[8836] | 172 | LongitudeValidator(JTextComponent tc) {
|
---|
[2711] | 173 | super(tc);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | @Override
|
---|
| 177 | public void validate() {
|
---|
| 178 | double value = 0;
|
---|
| 179 | try {
|
---|
| 180 | value = Double.parseDouble(getComponent().getText());
|
---|
[8510] | 181 | } catch (NumberFormatException ex) {
|
---|
[2850] | 182 | feedbackInvalid(tr("The string ''{0}'' is not a valid double value.", getComponent().getText()));
|
---|
[2711] | 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());
|
---|
[8510] | 197 | } catch (NumberFormatException ex) {
|
---|
[2711] | 198 | return false;
|
---|
| 199 | }
|
---|
| 200 | if (!LatLon.isValidLon(value))
|
---|
| 201 | return false;
|
---|
| 202 | return true;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | class OsmUrlRefresher implements DocumentListener {
|
---|
[6084] | 207 | @Override
|
---|
[8513] | 208 | public void changedUpdate(DocumentEvent e) {
|
---|
| 209 | parseURL();
|
---|
| 210 | }
|
---|
[8510] | 211 |
|
---|
[6084] | 212 | @Override
|
---|
[8513] | 213 | public void insertUpdate(DocumentEvent e) {
|
---|
| 214 | parseURL();
|
---|
| 215 | }
|
---|
[8510] | 216 |
|
---|
[6084] | 217 | @Override
|
---|
[8513] | 218 | public void removeUpdate(DocumentEvent e) {
|
---|
| 219 | parseURL();
|
---|
| 220 | }
|
---|
[2711] | 221 | }
|
---|
| 222 | }
|
---|