Changeset 2483 in josm for trunk


Ignore:
Timestamp:
2009-11-19T17:28:41+01:00 (14 years ago)
Author:
Gubaer
Message:

Fixed bounding box selection using coordinates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java

    r2403 r2483  
    5656
    5757
     58    protected void registerBoundingBoxBuilder() {
     59        BoundingBoxBuilder bboxbuilder = new BoundingBoxBuilder();
     60        for (int i = 0;i < latlon.length; i++) {
     61            latlon[i].addFocusListener(bboxbuilder);
     62            latlon[i].addActionListener(bboxbuilder);
     63        }
     64    }
     65
    5866    protected void buildDownloadAreaInputFields() {
    5967        latlon = new JTextField[4];
     
    7886        latlon[3].addFocusListener(lonChecker);
    7987        latlon[3].addActionListener(lonChecker);
     88
     89        registerBoundingBoxBuilder();
    8090    }
    8191
     
    175185        latlon[2].setText(Double.toString(area.getMax().lat()));
    176186        latlon[3].setText(Double.toString(area.getMax().lon()));
    177         for (JTextField f : latlon) {
    178             f.setCaretPosition(0);
     187        for (JTextField tf: latlon) {
     188            resetErrorMessage(tf);
    179189        }
    180190    }
     
    185195    }
    186196
     197    private Border errorBorder = BorderFactory.createLineBorder(Color.RED, 1);
     198
     199    protected void setErrorMessage(JTextField tf, String msg) {
     200        tf.setBorder(errorBorder);
     201        tf.setToolTipText(msg);
     202    }
     203
     204    protected void resetErrorMessage(JTextField tf) {
     205        tf.setBorder(UIManager.getBorder("TextField.border"));
     206        tf.setToolTipText("");
     207    }
     208
    187209
    188210    class LatValueChecker extends FocusAdapter implements ActionListener{
    189211        private JTextField tfLatValue;
    190212
    191         private Border errorBorder = BorderFactory.createLineBorder(Color.RED, 1);
    192         protected void setErrorMessage(String msg) {
    193             if (msg != null) {
    194                 tfLatValue.setBorder(errorBorder);
    195                 tfLatValue.setToolTipText(msg);
    196             } else {
    197                 tfLatValue.setBorder(UIManager.getBorder("TextField.border"));
    198                 tfLatValue.setToolTipText("");
    199             }
    200         }
    201 
    202213        public LatValueChecker(JTextField tfLatValue) {
    203214            this.tfLatValue = tfLatValue;
     
    209220                value = Double.parseDouble(tfLatValue.getText());
    210221            } catch(NumberFormatException ex) {
    211                 setErrorMessage(tr("The string ''{0}'' isn''t a valid double value.", tfLatValue.getText()));
     222                setErrorMessage(tfLatValue,tr("The string ''{0}'' isn''t a valid double value.", tfLatValue.getText()));
    212223                return;
    213224            }
    214225            if (!LatLon.isValidLat(value)) {
    215                 setErrorMessage(tr("Value for latitude in range [-90,90] required.", tfLatValue.getText()));
     226                setErrorMessage(tfLatValue,tr("Value for latitude in range [-90,90] required.", tfLatValue.getText()));
    216227                return;
    217228            }
    218             setErrorMessage(null);
     229            resetErrorMessage(tfLatValue);
    219230        }
    220231
     
    231242    class LonValueChecker extends FocusAdapter implements ActionListener {
    232243        private JTextField tfLonValue;
    233         private Border errorBorder = BorderFactory.createLineBorder(Color.RED, 1);
    234         protected void setErrorMessage(String msg) {
    235             if (msg != null) {
    236                 tfLonValue.setBorder(errorBorder);
    237                 tfLonValue.setToolTipText(msg);
    238             } else {
    239                 tfLonValue.setBorder(UIManager.getBorder("TextField.border"));
    240                 tfLonValue.setToolTipText("");
    241             }
    242         }
    243244
    244245        public LonValueChecker(JTextField tfLonValue) {
     
    251252                value = Double.parseDouble(tfLonValue.getText());
    252253            } catch(NumberFormatException ex) {
    253                 setErrorMessage(tr("The string ''{0}'' isn''t a valid double value.", tfLonValue.getText()));
     254                setErrorMessage(tfLonValue,tr("The string ''{0}'' isn''t a valid double value.", tfLonValue.getText()));
    254255                return;
    255256            }
    256257            if (!LatLon.isValidLon(value)) {
    257                 setErrorMessage(tr("Value for longitude in range [-180,180] required.", tfLonValue.getText()));
     258                setErrorMessage(tfLonValue,tr("Value for longitude in range [-180,180] required.", tfLonValue.getText()));
    258259                return;
    259260            }
    260             setErrorMessage(null);
     261            resetErrorMessage(tfLonValue);
    261262        }
    262263
     
    336337        }
    337338    }
     339
     340    class BoundingBoxBuilder extends FocusAdapter implements ActionListener {
     341        protected Bounds build() {
     342            double minlon, minlat, maxlon,maxlat;
     343            try {
     344                minlon = Double.parseDouble(latlon[0].getText().trim());
     345                minlat = Double.parseDouble(latlon[1].getText().trim());
     346                maxlon = Double.parseDouble(latlon[2].getText().trim());
     347                maxlat = Double.parseDouble(latlon[3].getText().trim());
     348            } catch(NumberFormatException e) {
     349                return null;
     350            }
     351            if (!LatLon.isValidLon(minlon) || !LatLon.isValidLon(maxlon)
     352                    || !LatLon.isValidLat(minlat) || ! LatLon.isValidLat(maxlat))
     353                return null;
     354            if (minlon > maxlon)
     355                return null;
     356            if (minlat > maxlat)
     357                return null;
     358            return new Bounds(minlon,minlat,maxlon,maxlat);
     359        }
     360
     361        protected void refreshBounds() {
     362            Bounds  b = build();
     363            parent.boundingBoxChanged(b, BoundingBoxSelection.this);
     364        }
     365
     366        @Override
     367        public void focusLost(FocusEvent e) {
     368            refreshBounds();
     369        }
     370
     371        public void actionPerformed(ActionEvent e) {
     372            refreshBounds();
     373        }
     374    }
    338375}
Note: See TracChangeset for help on using the changeset viewer.