source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java@ 1800

Last change on this file since 1800 was 1800, checked in by Gubaer, 15 years ago

#2962: applied patch by plaicy - Download always increment number for new layer

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