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 | import java.util.concurrent.Future;
|
---|
8 |
|
---|
9 | import javax.swing.JCheckBox;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.DownloadAction;
|
---|
13 | import org.openstreetmap.josm.data.Bounds;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
17 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
18 | import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
|
---|
19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
20 | import org.openstreetmap.josm.io.BoundingBoxDownloader;
|
---|
21 | import org.openstreetmap.josm.io.OsmServerLocationReader;
|
---|
22 | import org.openstreetmap.josm.io.OsmServerReader;
|
---|
23 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Open the download dialog and download the data.
|
---|
29 | * Run in the worker thread.
|
---|
30 | */
|
---|
31 | public class DownloadOsmTask implements DownloadTask {
|
---|
32 | private static Bounds currentBounds;
|
---|
33 | private Future<Task> task = null;
|
---|
34 | private DataSet downloadedData;
|
---|
35 |
|
---|
36 | private class Task extends PleaseWaitRunnable {
|
---|
37 | private OsmServerReader reader;
|
---|
38 | private DataSet dataSet;
|
---|
39 | private boolean newLayer;
|
---|
40 | private String msg = "";
|
---|
41 |
|
---|
42 | public Task(boolean newLayer, OsmServerReader reader, boolean silent, String msg) {
|
---|
43 | super(tr("Downloading data"));
|
---|
44 | this.msg = msg;
|
---|
45 | this.reader = reader;
|
---|
46 | this.newLayer = newLayer;
|
---|
47 | this.silent = silent;
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override public void realRun() throws IOException, SAXException, OsmTransferException {
|
---|
51 | Main.pleaseWaitDlg.setCustomText(msg);
|
---|
52 | dataSet = reader.parseOsm();
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected OsmDataLayer getEditLayer() {
|
---|
56 | if (Main.map == null) return null;
|
---|
57 | if (Main.map.mapView == null) return null;
|
---|
58 | return Main.map.mapView.getEditLayer();
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override protected void finish() {
|
---|
62 | if (dataSet == null)
|
---|
63 | return; // user canceled download or error occurred
|
---|
64 | if (dataSet.allPrimitives().isEmpty()) {
|
---|
65 | // If silent is set to true, we don't want to see information messages
|
---|
66 | if(!silent) {
|
---|
67 | errorMessage = tr("No data imported.");
|
---|
68 | }
|
---|
69 | // need to synthesize a download bounds lest the visual indication of downloaded
|
---|
70 | // area doesn't work
|
---|
71 | dataSet.dataSources.add(new DataSource(currentBounds, "OpenStreetMap server"));
|
---|
72 | }
|
---|
73 | rememberDownloadedData(dataSet);
|
---|
74 | if (newLayer || getEditLayer() == null) {
|
---|
75 | OsmDataLayer layer = new OsmDataLayer(dataSet, OsmDataLayer.createNewName(), null);
|
---|
76 | Main.main.addLayer(layer);
|
---|
77 | } else {
|
---|
78 | getEditLayer().mergeFrom(dataSet);
|
---|
79 | }
|
---|
80 |
|
---|
81 | Main.pleaseWaitDlg.setCustomText("");
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override protected void cancel() {
|
---|
85 | if (reader != null) {
|
---|
86 | reader.cancel();
|
---|
87 | }
|
---|
88 | Main.pleaseWaitDlg.cancel.setEnabled(false);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | private JCheckBox checkBox = new JCheckBox(tr("OpenStreetMap data"), true);
|
---|
92 |
|
---|
93 | private void rememberDownloadedData(DataSet ds) {
|
---|
94 | this.downloadedData = ds;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public DataSet getDownloadedData() {
|
---|
98 | return downloadedData;
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void download(DownloadAction action, double minlat, double minlon,
|
---|
102 | double maxlat, double maxlon, boolean silent, String message) {
|
---|
103 | // Swap min and max if user has specified them the wrong way round
|
---|
104 | // (easy to do if you are crossing 0, for example)
|
---|
105 | // FIXME should perhaps be done in download dialog?
|
---|
106 | if (minlat > maxlat) {
|
---|
107 | double t = minlat; minlat = maxlat; maxlat = t;
|
---|
108 | }
|
---|
109 | if (minlon > maxlon) {
|
---|
110 | double t = minlon; minlon = maxlon; maxlon = t;
|
---|
111 | }
|
---|
112 |
|
---|
113 | boolean newLayer = action != null
|
---|
114 | && (action.dialog == null || action.dialog.newLayer.isSelected());
|
---|
115 |
|
---|
116 | Task t = new Task(newLayer,
|
---|
117 | new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon),
|
---|
118 | silent,
|
---|
119 | message);
|
---|
120 | currentBounds = new Bounds(new LatLon(minlat, minlon), new LatLon(maxlat, maxlon));
|
---|
121 | // We need submit instead of execute so we can wait for it to finish and get the error
|
---|
122 | // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
|
---|
123 | task = Main.worker.submit(t, t);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void download(DownloadAction action, double minlat, double minlon,
|
---|
127 | double maxlat, double maxlon) {
|
---|
128 | download(action, minlat, minlon, maxlat, maxlon, false, "");
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Loads a given URL from the OSM Server
|
---|
133 | * @param True if the data should be saved to a new layer
|
---|
134 | * @param The URL as String
|
---|
135 | */
|
---|
136 | public void loadUrl(boolean new_layer, String url) {
|
---|
137 | Task t = new Task(new_layer,
|
---|
138 | new OsmServerLocationReader(url),
|
---|
139 | false,
|
---|
140 | "");
|
---|
141 | task = Main.worker.submit(t, t);
|
---|
142 | }
|
---|
143 |
|
---|
144 | public JCheckBox getCheckBox() {
|
---|
145 | return checkBox;
|
---|
146 | }
|
---|
147 |
|
---|
148 | public String getPreferencesSuffix() {
|
---|
149 | return "osm";
|
---|
150 | }
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * (non-Javadoc)
|
---|
154 | * @see org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask#getErrorMessage()
|
---|
155 | */
|
---|
156 | public String getErrorMessage() {
|
---|
157 | if(task == null)
|
---|
158 | return "";
|
---|
159 |
|
---|
160 | try {
|
---|
161 | Task t = task.get();
|
---|
162 | return t.errorMessage == null
|
---|
163 | ? ""
|
---|
164 | : t.errorMessage;
|
---|
165 | } catch (Exception e) {
|
---|
166 | return "";
|
---|
167 | }
|
---|
168 | }
|
---|
169 | }
|
---|