Changeset 55 in josm for src/org/openstreetmap
- Timestamp:
- 2006-02-16T20:41:05+01:00 (19 years ago)
- Location:
- src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/actions/DownloadAction.java
r51 r55 7 7 import java.awt.event.ActionListener; 8 8 import java.awt.event.InputEvent; 9 import java.awt.event.KeyAdapter; 9 10 import java.awt.event.KeyEvent; 11 import java.awt.event.KeyListener; 10 12 import java.io.FileNotFoundException; 11 13 import java.io.IOException; 14 import java.util.HashMap; 15 import java.util.Map; 12 16 13 17 import javax.swing.DefaultListModel; … … 20 24 import javax.swing.JTextField; 21 25 import javax.swing.KeyStroke; 26 import javax.swing.SwingUtilities; 22 27 import javax.swing.event.ListSelectionEvent; 23 28 import javax.swing.event.ListSelectionListener; … … 25 30 import org.jdom.JDOMException; 26 31 import org.openstreetmap.josm.Main; 32 import org.openstreetmap.josm.data.Bounds; 27 33 import org.openstreetmap.josm.data.GeoPoint; 28 34 import org.openstreetmap.josm.data.osm.DataSet; … … 62 68 public void actionPerformed(ActionEvent e) { 63 69 JPanel dlg = new JPanel(new GridBagLayout()); 64 70 71 // World image 65 72 WorldChooser wc = new WorldChooser(); 66 73 dlg.add(wc, GBC.eop()); 67 74 wc.setToolTipText("Move and zoom the image like the main map. Select an area to download by dragging."); 75 76 // Bounding box edits 68 77 dlg.add(new JLabel("Bounding box"), GBC.eol()); 69 78 dlg.add(new JLabel("min lat"), GBC.std().insets(10,0,5,0)); … … 75 84 dlg.add(new JLabel("max lon"), GBC.std().insets(10,0,5,0)); 76 85 dlg.add(latlon[3], GBC.eol()); 77 78 86 if (Main.main.getMapFrame() != null) { 79 87 MapView mv = Main.main.getMapFrame().mapView; 80 int w = mv.getWidth(); 81 int h = mv.getHeight(); 82 GeoPoint bottomLeft = mv.getPoint(0, h, true); 83 GeoPoint topRight = mv.getPoint(w, 0, true); 84 if (bottomLeft.isOutSideWorld()) 85 bottomLeft = new GeoPoint(-89.999, -179.999); // do not use the Projection constants, since this look better. 86 if (topRight.isOutSideWorld()) 87 topRight = new GeoPoint(89.999, 179.999); 88 latlon[0].setText(""+bottomLeft.lat); 89 latlon[1].setText(""+bottomLeft.lon); 90 latlon[2].setText(""+topRight.lat); 91 latlon[3].setText(""+topRight.lon); 92 for (JTextField f : latlon) 93 f.setCaretPosition(0); 88 setEditBounds(new Bounds( 89 mv.getPoint(0, mv.getHeight(), true), 90 mv.getPoint(mv.getWidth(), 0, true))); 94 91 rawGps.setSelected(mv.getActiveLayer() instanceof RawGpsDataLayer); 95 92 } 96 97 93 dlg.add(rawGps, GBC.eop()); 98 94 99 // load bookmarks 95 // OSM url edit 96 dlg.add(new JLabel("URL from www.openstreetmap.org"), GBC.eol()); 97 final JTextField osmUrl = new JTextField(); 98 dlg.add(osmUrl, GBC.eop().fill(GBC.HORIZONTAL)); 99 final KeyListener osmUrlRefresher = new KeyAdapter(){ 100 @Override 101 public void keyTyped(KeyEvent e) { 102 SwingUtilities.invokeLater(new Runnable() { 103 public void run() { 104 try { 105 double latMin = Double.parseDouble(latlon[0].getText()); 106 double lonMin = Double.parseDouble(latlon[1].getText()); 107 double latMax = Double.parseDouble(latlon[2].getText()); 108 double lonMax = Double.parseDouble(latlon[3].getText()); 109 double lat = (latMax+latMin)/2; 110 double lon = (lonMax+lonMin)/2; 111 // convert to mercator (for calculation of zoom only) 112 latMin = Math.log(Math.tan(Math.PI/4.0+latMin/180.0*Math.PI/2.0))*180.0/Math.PI; 113 latMax = Math.log(Math.tan(Math.PI/4.0+latMax/180.0*Math.PI/2.0))*180.0/Math.PI; 114 double size = Math.max(Math.abs(latMax-latMin), Math.abs(lonMax-lonMin)); 115 int zoom = 0; 116 while (zoom <= 20) { 117 if (size >= 180) 118 break; 119 size *= 2; 120 zoom++; 121 } 122 osmUrl.setText("http://www.openstreetmap.org/index.html?lat="+lat+"&lon="+lon+"&zoom="+zoom); 123 } catch (NumberFormatException x) { 124 osmUrl.setText(""); 125 } 126 osmUrl.setCaretPosition(0); 127 } 128 }); 129 } 130 }; 131 for (JTextField f : latlon) 132 f.addKeyListener(osmUrlRefresher); 133 SwingUtilities.invokeLater(new Runnable() {public void run() {osmUrlRefresher.keyTyped(null);}}); 134 osmUrl.addKeyListener(new KeyAdapter(){ 135 @Override 136 public void keyTyped(KeyEvent e) { 137 SwingUtilities.invokeLater(new Runnable() { 138 public void run() { 139 Map<String, Double> map = readArgs(osmUrl.getText()); 140 try { 141 double size = 180.0 / Math.pow(2, map.get("zoom")); 142 Bounds b = new Bounds( 143 new GeoPoint(map.get("lat") - size/2, map.get("lon") - size), 144 new GeoPoint(map.get("lat") + size/2, map.get("lon") + size)); 145 setEditBounds(b); 146 } catch (Exception x) { // NPE or IAE 147 for (JTextField f : latlon) 148 f.setText(""); 149 } 150 } 151 }); 152 } 153 }); 154 155 // Bookmarks 100 156 dlg.add(new JLabel("Bookmarks"), GBC.eol()); 101 157 final BookmarkList bookmarks = new BookmarkList(); … … 108 164 } 109 165 rawGps.setSelected(b == null ? false : b.rawgps); 166 osmUrlRefresher.keyTyped(null); 110 167 } 111 168 }); 112 169 wc.addListMarker(bookmarks); 113 wc.addLatLonInputField(latlon);114 170 dlg.add(new JScrollPane(bookmarks), GBC.eol().fill()); 115 171 … … 145 201 buttons.add(remove); 146 202 dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL)); 147 203 148 204 Dimension d = dlg.getPreferredSize(); 149 205 wc.setPreferredSize(new Dimension(d.width, d.width/2)); 206 wc.addInputFields(latlon, osmUrl, osmUrlRefresher); 150 207 208 // Finally: the dialog 151 209 int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area", 152 210 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); … … 154 212 return; 155 213 214 startDownload(); 215 } 216 217 /** 218 * Read the values from the edit fields and start the download. 219 */ 220 private void startDownload() { 156 221 Bookmark b = readBookmark(); 157 222 if (b == null) { … … 161 226 162 227 final OsmServerReader osmReader = new OsmServerReader(b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]); 163 164 228 new Thread(new PleaseWaitRunnable("Downloading data"){ 165 229 @Override … … 229 293 } 230 294 } 295 296 297 /** 298 * Extrakt URL arguments. 299 */ 300 private Map<String, Double> readArgs(String s) { 301 int i = s.indexOf('?'); 302 if (i == -1) 303 return new HashMap<String, Double>(); 304 String[] args = s.substring(i+1).split("&"); 305 HashMap<String, Double> map = new HashMap<String, Double>(); 306 for (String arg : args) { 307 int eq = arg.indexOf('='); 308 if (eq != -1) { 309 try { 310 map.put(arg.substring(0, eq), Double.parseDouble(arg.substring(eq + 1))); 311 } catch (NumberFormatException e) { 312 } 313 } 314 } 315 return map; 316 } 317 318 /** 319 * Set the four edit fields to the given bounds coordinates. 320 */ 321 private void setEditBounds(Bounds b) { 322 GeoPoint bottomLeft = b.min; 323 GeoPoint topRight = b.max; 324 if (bottomLeft.isOutSideWorld()) 325 bottomLeft = new GeoPoint(-89.999, -179.999); // do not use the Projection constants, since this looks better. 326 if (topRight.isOutSideWorld()) 327 topRight = new GeoPoint(89.999, 179.999); 328 latlon[0].setText(""+bottomLeft.lat); 329 latlon[1].setText(""+bottomLeft.lon); 330 latlon[2].setText(""+topRight.lat); 331 latlon[3].setText(""+topRight.lon); 332 for (JTextField f : latlon) 333 f.setCaretPosition(0); 334 } 231 335 } -
src/org/openstreetmap/josm/gui/WorldChooser.java
r43 r55 144 144 * @param field Must have exactly 4 entries (min lat to max lon) 145 145 */ 146 public void addLatLonInputField(final JTextField[] field) { 147 if (field.length != 4) 148 throw new IllegalArgumentException(); 149 146 public void addInputFields(final JTextField[] field, JTextField osmUrl, final KeyListener osmUrlRefresher) { 150 147 // listener that invokes updateMarkerFromTextField after all 151 148 // messages are dispatched and so text fields are updated. … … 163 160 for (JTextField f : field) 164 161 f.addKeyListener(listener); 162 osmUrl.addKeyListener(listener); 165 163 166 164 SelectionEnded selListener = new SelectionEnded(){ … … 175 173 for (JTextField f : field) 176 174 f.setCaretPosition(0); 175 osmUrlRefresher.keyTyped(null); 177 176 repaint(); 178 177 }
Note:
See TracChangeset
for help on using the changeset viewer.