1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 |
|
---|
8 | import javax.swing.JCheckBox;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
13 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
14 | import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
|
---|
15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
16 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
17 | import org.xml.sax.SAXException;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Open the download dialog and download the data.
|
---|
21 | * Run in the worker thread.
|
---|
22 | */
|
---|
23 | public class DownloadOsmTask implements DownloadTask {
|
---|
24 |
|
---|
25 | private static class Task extends PleaseWaitRunnable {
|
---|
26 | private BoundingBoxDownloader reader;
|
---|
27 | private DataSet dataSet;
|
---|
28 | private boolean newLayer;
|
---|
29 |
|
---|
30 | public Task(boolean newLayer, BoundingBoxDownloader reader) {
|
---|
31 | super(tr("Downloading data"));
|
---|
32 | this.reader = reader;
|
---|
33 | this.newLayer = newLayer;
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override public void realRun() throws IOException, SAXException {
|
---|
37 | dataSet = reader.parseOsm();
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override protected void finish() {
|
---|
41 | if (dataSet == null)
|
---|
42 | return; // user cancelled download or error occoured
|
---|
43 | if (dataSet.allPrimitives().isEmpty())
|
---|
44 | errorMessage = tr("No data imported.");
|
---|
45 | OsmDataLayer layer = new OsmDataLayer(dataSet, tr("Data Layer"), null);
|
---|
46 | if (newLayer)
|
---|
47 | Main.main.addLayer(layer);
|
---|
48 | else
|
---|
49 | Main.main.editLayer().mergeFrom(layer);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override protected void cancel() {
|
---|
53 | if (reader != null)
|
---|
54 | reader.cancel();
|
---|
55 | }
|
---|
56 | }
|
---|
57 | private JCheckBox checkBox = new JCheckBox(tr("OpenStreetMap data"), true);
|
---|
58 |
|
---|
59 | public void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon) {
|
---|
60 | Task task = new Task(action.dialog.newLayer.isSelected(), new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon));
|
---|
61 | Main.worker.execute(task);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public JCheckBox getCheckBox() {
|
---|
65 | return checkBox;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public String getPreferencesSuffix() {
|
---|
69 | return "osm";
|
---|
70 | }
|
---|
71 | }
|
---|