- Timestamp:
- 2009-11-07T18:02:39+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java
r2361 r2403 7 7 import java.awt.Dimension; 8 8 import java.awt.GridBagLayout; 9 import java.awt.Toolkit; 10 import java.awt.datatransfer.DataFlavor; 11 import java.awt.datatransfer.FlavorEvent; 12 import java.awt.datatransfer.FlavorListener; 13 import java.awt.datatransfer.Transferable; 14 import java.awt.datatransfer.UnsupportedFlavorException; 9 15 import java.awt.event.ActionEvent; 10 16 import java.awt.event.ActionListener; 11 17 import java.awt.event.FocusAdapter; 12 18 import java.awt.event.FocusEvent; 13 19 import java.awt.event.MouseAdapter; 20 import java.awt.event.MouseEvent; 21 import java.io.IOException; 22 23 import javax.swing.AbstractAction; 14 24 import javax.swing.BorderFactory; 15 25 import javax.swing.JLabel; 16 26 import javax.swing.JPanel; 27 import javax.swing.JPopupMenu; 17 28 import javax.swing.JTextArea; 18 29 import javax.swing.JTextField; … … 26 37 import org.openstreetmap.josm.data.coor.LatLon; 27 38 import org.openstreetmap.josm.tools.GBC; 39 import org.openstreetmap.josm.tools.ImageProvider; 28 40 import org.openstreetmap.josm.tools.OsmUrlToBounds; 29 41 … … 39 51 40 52 private JTextField[] latlon = null; 41 private final JTextArea osmUrl = new JTextArea();53 private final JTextArea tfOsmUrl = new JTextArea(); 42 54 private final JTextArea showUrl = new JTextArea(); 43 55 private DownloadDialog parent; 44 56 45 57 46 58 protected void buildDownloadAreaInputFields() { 47 59 latlon = new JTextField[4]; … … 58 70 latlon[2].addFocusListener(latChecker); 59 71 latlon[2].addActionListener(latChecker); 60 72 61 73 LonValueChecker lonChecker = new LonValueChecker(latlon[1]); 62 74 latlon[1].addFocusListener(lonChecker); … … 66 78 latlon[3].addFocusListener(lonChecker); 67 79 latlon[3].addActionListener(lonChecker); 68 69 } 70 80 } 81 71 82 public void addGui(final DownloadDialog gui) { 72 83 buildDownloadAreaInputFields(); 73 JPanel dlg = new JPanel(new GridBagLayout());74 75 osmUrl.getDocument().addDocumentListener(new OsmUrlRefresher());84 final JPanel dlg = new JPanel(new GridBagLayout()); 85 86 tfOsmUrl.getDocument().addDocumentListener(new OsmUrlRefresher()); 76 87 77 88 // select content on receiving focus. this seems to be the default in the 78 89 // windows look+feel but not for others. needs invokeLater to avoid strange 79 90 // side effects that will cancel out the newly made selection otherwise. 80 osmUrl.addFocusListener(new SelectAllOnFocusHandler(osmUrl));81 osmUrl.setLineWrap(true);82 osmUrl.setBorder(latlon[0].getBorder());91 tfOsmUrl.addFocusListener(new SelectAllOnFocusHandler(tfOsmUrl)); 92 tfOsmUrl.setLineWrap(true); 93 tfOsmUrl.setBorder(latlon[0].getBorder()); 83 94 84 95 dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0)); … … 92 103 93 104 dlg.add(new JLabel(tr("URL from www.openstreetmap.org (you can paste an URL here to download the area)")), GBC.eol().insets(10,20,5,0)); 94 dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill()); 105 dlg.add(tfOsmUrl, GBC.eop().insets(10,0,5,0).fill()); 106 tfOsmUrl.addMouseListener( 107 new MouseAdapter() { 108 @Override 109 public void mousePressed(MouseEvent e) { 110 checkPopup(e); 111 } 112 113 @Override 114 public void mouseClicked(MouseEvent e) { 115 checkPopup(e); 116 } 117 118 @Override 119 public void mouseReleased(MouseEvent e) { 120 checkPopup(e); 121 } 122 123 private void checkPopup(MouseEvent e) { 124 if (e.isPopupTrigger()) { 125 OsmUrlPopup popup = new OsmUrlPopup(); 126 popup.show(tfOsmUrl, e.getX(), e.getY()); 127 } 128 } 129 } 130 ); 95 131 dlg.add(showUrl, GBC.eop().insets(10,0,5,5)); 96 132 showUrl.setEditable(false); … … 102 138 } 103 139 104 140 105 141 public void setDownloadArea(Bounds area) { 106 142 updateBboxFields(area); 107 143 updateUrl(area); 108 144 } 109 145 110 146 public Bounds getDownloadArea() { 111 147 double[] values = new double[4]; … … 120 156 return null; 121 157 if (!LatLon.isValidLat(values[2]) || !LatLon.isValidLon(values[3])) 122 return null; 158 return null; 123 159 return new Bounds(values); 124 160 } 125 161 126 162 private boolean parseURL(DownloadDialog gui) { 127 Bounds b = OsmUrlToBounds.parse( osmUrl.getText());128 if(b == null) return false; 163 Bounds b = OsmUrlToBounds.parse(tfOsmUrl.getText()); 164 if(b == null) return false; 129 165 gui.boundingBoxChanged(b,BoundingBoxSelection.this); 130 166 updateBboxFields(b); … … 148 184 showUrl.setText(OsmUrlToBounds.getURL(area)); 149 185 } 150 151 186 187 152 188 class LatValueChecker extends FocusAdapter implements ActionListener{ 153 189 private JTextField tfLatValue; 154 190 155 191 private Border errorBorder = BorderFactory.createLineBorder(Color.RED, 1); 156 192 protected void setErrorMessage(String msg) { … … 160 196 } else { 161 197 tfLatValue.setBorder(UIManager.getBorder("TextField.border")); 162 tfLatValue.setToolTipText(""); 163 } 164 } 165 198 tfLatValue.setToolTipText(""); 199 } 200 } 201 166 202 public LatValueChecker(JTextField tfLatValue) { 167 203 this.tfLatValue = tfLatValue; 168 204 } 169 205 170 206 protected void check() { 171 207 double value = 0; … … 178 214 if (!LatLon.isValidLat(value)) { 179 215 setErrorMessage(tr("Value for latitude in range [-90,90] required.", tfLatValue.getText())); 180 return; 216 return; 181 217 } 182 218 setErrorMessage(null); 183 219 } 184 220 185 221 @Override 186 222 public void focusLost(FocusEvent e) { … … 189 225 190 226 public void actionPerformed(ActionEvent e) { 191 check(); 192 } 193 } 194 227 check(); 228 } 229 } 230 195 231 class LonValueChecker extends FocusAdapter implements ActionListener { 196 232 private JTextField tfLonValue; … … 202 238 } else { 203 239 tfLonValue.setBorder(UIManager.getBorder("TextField.border")); 204 tfLonValue.setToolTipText(""); 205 } 206 } 207 240 tfLonValue.setToolTipText(""); 241 } 242 } 243 208 244 public LonValueChecker(JTextField tfLonValue) { 209 245 this.tfLonValue = tfLonValue; 210 246 } 211 247 212 248 protected void check() { 213 249 double value = 0; … … 215 251 value = Double.parseDouble(tfLonValue.getText()); 216 252 } catch(NumberFormatException ex) { 217 setErrorMessage(tr("The string ''{0}'' isn''t a valid double value.", tfLonValue.getText()));218 return;253 setErrorMessage(tr("The string ''{0}'' isn''t a valid double value.", tfLonValue.getText())); 254 return; 219 255 } 220 256 if (!LatLon.isValidLon(value)) { … … 224 260 setErrorMessage(null); 225 261 } 226 262 227 263 @Override 228 264 public void focusLost(FocusEvent e) { … … 232 268 public void actionPerformed(ActionEvent e) { 233 269 check(); 234 } 235 } 236 270 } 271 } 272 237 273 class SelectAllOnFocusHandler extends FocusAdapter { 238 274 private JTextComponent tfTarget; 239 275 public SelectAllOnFocusHandler(JTextComponent tfTarget) { 240 this.tfTarget = tfTarget; 241 } 242 276 this.tfTarget = tfTarget; 277 } 278 243 279 @Override 244 280 public void focusGained(FocusEvent e) { 245 281 tfTarget.selectAll(); 246 } 247 } 248 282 } 283 } 284 249 285 class OsmUrlRefresher implements DocumentListener { 250 286 public void changedUpdate(DocumentEvent e) { parseURL(parent); } … … 252 288 public void removeUpdate(DocumentEvent e) { parseURL(parent); } 253 289 } 290 291 class PasteUrlAction extends AbstractAction implements FlavorListener { 292 293 public PasteUrlAction() { 294 putValue(NAME, tr("Paste")); 295 putValue(SMALL_ICON, ImageProvider.get("paste")); 296 putValue(SHORT_DESCRIPTION, tr("Paste URL from clipboard")); 297 Toolkit.getDefaultToolkit().getSystemClipboard().addFlavorListener(this); 298 } 299 300 protected String getClipboardContent() { 301 Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 302 try { 303 if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 304 String text = (String)t.getTransferData(DataFlavor.stringFlavor); 305 return text; 306 } 307 } catch (UnsupportedFlavorException ex) { 308 ex.printStackTrace(); 309 return null; 310 } catch (IOException ex) { 311 ex.printStackTrace(); 312 return null; 313 } 314 return null; 315 } 316 317 public void actionPerformed(ActionEvent e) { 318 String content = getClipboardContent(); 319 if (content != null) { 320 tfOsmUrl.setText(content); 321 } 322 } 323 324 protected void updateEnabledState() { 325 setEnabled(getClipboardContent() != null); 326 } 327 328 public void flavorsChanged(FlavorEvent e) { 329 updateEnabledState(); 330 } 331 } 332 333 class OsmUrlPopup extends JPopupMenu { 334 public OsmUrlPopup() { 335 add(new PasteUrlAction()); 336 } 337 } 254 338 }
Note:
See TracChangeset
for help on using the changeset viewer.