Changeset 16971 in josm
- Timestamp:
- 2020-08-29T20:24:08+02:00 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/LatLonDialog.java
r16643 r16971 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Color;7 6 import java.awt.Component; 8 7 import java.awt.GridBagLayout; … … 19 18 import javax.swing.JSeparator; 20 19 import javax.swing.JTabbedPane; 21 import javax.swing.UIManager; 22 import javax.swing.event.DocumentEvent; 23 import javax.swing.event.DocumentListener; 20 import javax.swing.text.JTextComponent; 24 21 25 22 import org.openstreetmap.josm.data.coor.EastNorth; … … 30 27 import org.openstreetmap.josm.gui.ExtendedDialog; 31 28 import org.openstreetmap.josm.gui.util.WindowGeometry; 29 import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator; 32 30 import org.openstreetmap.josm.gui.widgets.HtmlPanel; 33 31 import org.openstreetmap.josm.gui.widgets.JosmTextField; … … 40 38 */ 41 39 public class LatLonDialog extends ExtendedDialog { 42 private static final Color BG_COLOR_ERROR = new Color(255, 224, 224);43 40 44 41 /** … … 48 45 private JosmTextField tfLatLon, tfEastNorth; 49 46 private LatLon latLonCoordinates; 47 private LatLonValidator latLonValidator; 50 48 private EastNorth eastNorthCoordinates; 49 private EastNorthValidator eastNorthValidator; 51 50 52 51 protected JPanel buildLatLon() { … … 108 107 109 108 // parse and verify input on the fly 110 // 111 LatLonInputVerifier inputVerifier = new LatLonInputVerifier(); 112 tfLatLon.getDocument().addDocumentListener(inputVerifier); 109 latLonValidator = new LatLonValidator(tfLatLon); 113 110 114 111 // select the text in the field on focus … … 136 133 pnl.add(GBC.glue(1, 1), GBC.eol().fill().weight(1.0, 1.0)); 137 134 138 EastNorthInputVerifier inputVerifier = new EastNorthInputVerifier(); 139 tfEastNorth.getDocument().addDocumentListener(inputVerifier); 135 eastNorthValidator = new EastNorthValidator(tfEastNorth); 140 136 141 137 TextFieldFocusHandler focusHandler = new TextFieldFocusHandler(); … … 151 147 tabs.getModel().addChangeListener(e -> { 152 148 switch (tabs.getModel().getSelectedIndex()) { 153 case 0: parseLatLonUserInput(); break;154 case 1: parseEastNorthUserInput(); break;149 case 0: latLonValidator.validate(); break; 150 case 1: eastNorthValidator.validate(); break; 155 151 default: throw new AssertionError(); 156 152 } … … 233 229 } 234 230 235 protected void setErrorFeedback(JosmTextField tf, String message) { 236 tf.setBorder(BorderFactory.createLineBorder(Color.RED, 1)); 237 tf.setToolTipText(message); 238 tf.setBackground(BG_COLOR_ERROR); 239 } 240 241 protected void clearErrorFeedback(JosmTextField tf, String message) { 242 tf.setBorder(UIManager.getBorder("TextField.border")); 243 tf.setToolTipText(message); 244 tf.setBackground(UIManager.getColor("TextField.background")); 245 } 246 247 protected void parseLatLonUserInput() { 248 LatLon latLon; 249 try { 250 latLon = LatLonParser.parse(tfLatLon.getText()); 251 if (!LatLon.isValidLat(latLon.lat()) || !LatLon.isValidLon(latLon.lon())) { 231 private class LatLonValidator extends AbstractTextComponentValidator { 232 LatLonValidator(JTextComponent tc) { 233 super(tc); 234 } 235 236 @Override 237 public void validate() { 238 LatLon latLon; 239 try { 240 latLon = LatLonParser.parse(tfLatLon.getText()); 241 if (!LatLon.isValidLat(latLon.lat()) || !LatLon.isValidLon(latLon.lon())) { 242 latLon = null; 243 } 244 } catch (IllegalArgumentException e) { 245 Logging.trace(e); 252 246 latLon = null; 253 247 } 254 } catch (IllegalArgumentException e) { 255 Logging.trace(e); 256 latLon = null; 257 } 258 if (latLon == null) { 259 setErrorFeedback(tfLatLon, tr("Please enter a GPS coordinates")); 260 latLonCoordinates = null; 261 setOkEnabled(false); 262 } else { 263 clearErrorFeedback(tfLatLon, tr("Please enter a GPS coordinates")); 264 latLonCoordinates = latLon; 265 setOkEnabled(true); 266 } 267 } 268 269 protected void parseEastNorthUserInput() { 270 EastNorth en; 271 try { 272 en = parseEastNorth(tfEastNorth.getText()); 273 } catch (IllegalArgumentException e) { 274 Logging.trace(e); 275 en = null; 276 } 277 if (en == null) { 278 setErrorFeedback(tfEastNorth, tr("Please enter a Easting and Northing")); 279 latLonCoordinates = null; 280 setOkEnabled(false); 281 } else { 282 clearErrorFeedback(tfEastNorth, tr("Please enter a Easting and Northing")); 283 eastNorthCoordinates = en; 284 setOkEnabled(true); 248 if (latLon == null) { 249 feedbackInvalid(tr("Please enter a GPS coordinates")); 250 latLonCoordinates = null; 251 setOkEnabled(false); 252 } else { 253 feedbackValid(null); 254 latLonCoordinates = latLon; 255 setOkEnabled(true); 256 } 257 } 258 259 @Override 260 public boolean isValid() { 261 throw new UnsupportedOperationException(); 262 } 263 } 264 265 private class EastNorthValidator extends AbstractTextComponentValidator { 266 EastNorthValidator(JTextComponent tc) { 267 super(tc); 268 } 269 270 @Override 271 public void validate() { 272 EastNorth en; 273 try { 274 en = parseEastNorth(tfEastNorth.getText()); 275 } catch (IllegalArgumentException e) { 276 Logging.trace(e); 277 en = null; 278 } 279 if (en == null) { 280 feedbackInvalid(tr("Please enter a Easting and Northing")); 281 latLonCoordinates = null; 282 setOkEnabled(false); 283 } else { 284 feedbackValid(null); 285 eastNorthCoordinates = en; 286 setOkEnabled(true); 287 } 288 } 289 290 @Override 291 public boolean isValid() { 292 throw new UnsupportedOperationException(); 285 293 } 286 294 } … … 306 314 } 307 315 308 class LatLonInputVerifier implements DocumentListener {309 @Override310 public void changedUpdate(DocumentEvent e) {311 parseLatLonUserInput();312 }313 314 @Override315 public void insertUpdate(DocumentEvent e) {316 parseLatLonUserInput();317 }318 319 @Override320 public void removeUpdate(DocumentEvent e) {321 parseLatLonUserInput();322 }323 }324 325 class EastNorthInputVerifier implements DocumentListener {326 @Override327 public void changedUpdate(DocumentEvent e) {328 parseEastNorthUserInput();329 }330 331 @Override332 public void insertUpdate(DocumentEvent e) {333 parseEastNorthUserInput();334 }335 336 @Override337 public void removeUpdate(DocumentEvent e) {338 parseEastNorthUserInput();339 }340 }341 342 316 static class TextFieldFocusHandler implements FocusListener { 343 317 @Override
Note:
See TracChangeset
for help on using the changeset viewer.