Changeset 81 in josm for src/org/openstreetmap/josm/actions/DownloadAction.java
- Timestamp:
- 2006-04-05T00:31:57+02:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/actions/DownloadAction.java
r79 r81 10 10 import java.awt.event.KeyEvent; 11 11 import java.awt.event.KeyListener; 12 import java.io.FileNotFoundException;13 12 import java.io.IOException; 13 import java.util.Collection; 14 14 import java.util.HashMap; 15 15 import java.util.Map; … … 41 41 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 42 42 import org.openstreetmap.josm.gui.layer.RawGpsDataLayer; 43 import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint; 43 44 import org.openstreetmap.josm.io.OsmServerReader; 44 45 import org.openstreetmap.josm.tools.GBC; … … 55 56 public class DownloadAction extends JosmAction { 56 57 57 /** 58 /** 59 * Open the download dialog and download the data. 60 * Run in the worker thread. 61 */ 62 private final class DownloadOsmTask extends PleaseWaitRunnable { 63 private final OsmServerReader reader; 64 private DataSet dataSet; 65 66 private DownloadOsmTask(OsmServerReader reader) { 67 super("Downloading data"); 68 this.reader = reader; 69 } 70 71 @Override 72 public void realRun() throws IOException, SAXException { 73 dataSet = reader.parseOsm(); 74 if (dataSet.nodes.isEmpty()) 75 JOptionPane.showMessageDialog(Main.main, "No data imported."); 76 } 77 78 @Override 79 protected void finish() { 80 if (dataSet == null) 81 return; // user cancelled download or error occoured 82 Layer layer = new OsmDataLayer(dataSet, "Data Layer", false); 83 if (Main.main.getMapFrame() == null) 84 Main.main.setMapFrame(new MapFrame(layer)); 85 else 86 Main.main.getMapFrame().mapView.addLayer(layer); 87 } 88 89 } 90 91 92 private final class DownloadGpsTask extends PleaseWaitRunnable { 93 private final OsmServerReader reader; 94 private Collection<Collection<GpsPoint>> rawData; 95 96 private DownloadGpsTask(OsmServerReader reader) { 97 super("Downloading GPS data"); 98 this.reader = reader; 99 } 100 101 @Override 102 public void realRun() throws IOException, JDOMException { 103 rawData = reader.parseRawGps(); 104 } 105 106 @Override 107 protected void finish() { 108 if (rawData == null) 109 return; 110 String name = latlon[0].getText() + " " + latlon[1].getText() + " x " + latlon[2].getText() + " " + latlon[3].getText(); 111 Layer layer = new RawGpsDataLayer(rawData, name); 112 if (Main.main.getMapFrame() == null) 113 Main.main.setMapFrame(new MapFrame(layer)); 114 else 115 Main.main.getMapFrame().mapView.addLayer(layer); 116 } 117 118 } 119 120 121 /** 58 122 * minlat, minlon, maxlat, maxlon 59 123 */ … … 231 295 232 296 // Finally: the dialog 233 while(true) { 297 Bookmark b; 298 do { 234 299 int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area", 235 300 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 236 301 if (r != JOptionPane.OK_OPTION) 237 302 return; 238 if (startDownload()) 239 break; 240 } 241 } 242 243 /** 244 * Read the values from the edit fields and from the download. 245 * @return <code>true</code> for a success, <code>false</code> redisplay 246 */ 247 private boolean startDownload() { 248 Bookmark b = readBookmark(); 249 if (b == null) { 250 JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark."); 251 return false; 252 } 303 b = readBookmark(); 304 if (b == null) 305 JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark."); 306 } while (b == null); 253 307 254 308 double minlon = b.latlon[0]; … … 257 311 double maxlat = b.latlon[3]; 258 312 download(rawGps.isSelected(), minlon, minlat, maxlon, maxlat); 259 return true; 260 } 261 313 } 314 262 315 /** 263 316 * Read a bookmark from the current set edit fields. If one of the fields is … … 325 378 * Do the download for the given area. 326 379 */ 327 public void download(final boolean rawGps, double minlat, double minlon, double maxlat, double maxlon) { 328 final OsmServerReader osmReader = new OsmServerReader(minlat, minlon, maxlat, maxlon); 329 new Thread(new PleaseWaitRunnable("Downloading data"){ 330 @Override 331 public void realRun() { 332 try { 333 String name = latlon[0].getText() + " " + latlon[1].getText() + " x " + latlon[2].getText() + " " + latlon[3].getText(); 334 335 Layer layer = null; 336 if (rawGps) { 337 layer = new RawGpsDataLayer(osmReader.parseRawGps(), name); 338 } else { 339 DataSet dataSet = osmReader.parseOsm(); 340 if (dataSet == null) 341 return; // user cancelled download 342 if (dataSet.nodes.isEmpty()) { 343 closeDialog(); 344 JOptionPane.showMessageDialog(Main.main, 345 "No data imported."); 346 } 347 348 layer = new OsmDataLayer(dataSet, "Data Layer", false); 349 } 350 351 if (Main.main.getMapFrame() == null) 352 Main.main.setMapFrame(new MapFrame(layer)); 353 else 354 Main.main.getMapFrame().mapView.addLayer(layer); 355 } catch (SAXException x) { 356 closeDialog(); 357 x.printStackTrace(); 358 JOptionPane.showMessageDialog(Main.main, "Error while parsing: "+x.getMessage()); 359 } catch (JDOMException x) { 360 closeDialog(); 361 x.printStackTrace(); 362 JOptionPane.showMessageDialog(Main.main, "Error while parsing: "+x.getMessage()); 363 } catch (FileNotFoundException x) { 364 closeDialog(); 365 x.printStackTrace(); 366 JOptionPane.showMessageDialog(Main.main, "URL not found: " + x.getMessage()); 367 } catch (IOException x) { 368 closeDialog(); 369 x.printStackTrace(); 370 JOptionPane.showMessageDialog(Main.main, x.getMessage()); 371 } 372 } 373 }).start(); 380 public void download(boolean rawGps, double minlat, double minlon, double maxlat, double maxlon) { 381 OsmServerReader reader = new OsmServerReader(minlat, minlon, maxlat, maxlon); 382 PleaseWaitRunnable task = rawGps ? new DownloadGpsTask(reader) : new DownloadOsmTask(reader); 383 Main.worker.execute(task); 384 task.pleaseWaitDlg.setVisible(true); 374 385 } 375 386 }
Note:
See TracChangeset
for help on using the changeset viewer.
