source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadPrimitiveTask.java@ 3066

Last change on this file since 3066 was 2923, checked in by mjulius, 14 years ago

New action: DownloadPrimitiveAction
This allows to download a specific primitive from the API, optional including referrers

File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.CheckParameterUtil.ensureParameterNotNull;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.io.IOException;
8import java.lang.reflect.InvocationTargetException;
9
10import javax.swing.SwingUtilities;
11
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.PrimitiveId;
14import org.openstreetmap.josm.gui.ExceptionDialogUtil;
15import org.openstreetmap.josm.gui.PleaseWaitRunnable;
16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.io.OsmServerObjectReader;
19import org.openstreetmap.josm.io.OsmTransferException;
20import org.xml.sax.SAXException;
21
22/**
23 * The asynchronous task for updating a collection of objects using multi fetch.
24 *
25 */
26public class DownloadPrimitiveTask extends PleaseWaitRunnable {
27 private DataSet ds;
28 private boolean canceled;
29 private Exception lastException;
30 private PrimitiveId primitiveId;
31 private OsmDataLayer layer;
32 private OsmServerObjectReader reader;
33
34 /**
35 * Creates the task
36 *
37 * @param layer the layer in which primitives are updated. Must not be null.
38 * @param toUpdate a collection of primitives to update from the server. Set to
39 * the empty collection if null.
40 * @throws IllegalArgumentException thrown if layer is null.
41 */
42 public DownloadPrimitiveTask(PrimitiveId id, OsmDataLayer layer) {
43 super(tr("Download object"), false /* don't ignore exception */);
44 ensureParameterNotNull(layer, "layer");
45 this.layer = layer;
46 this.primitiveId = id;
47 }
48
49 @Override
50 protected void cancel() {
51 canceled = true;
52 synchronized(this) {
53 if (reader != null) {
54 reader.cancel();
55 }
56 }
57 }
58
59 @Override
60 protected void finish() {
61 if (canceled)
62 return;
63 if (lastException != null) {
64 ExceptionDialogUtil.explainException(lastException);
65 return;
66 }
67 Runnable r = new Runnable() {
68 public void run() {
69 layer.mergeFrom(ds);
70 layer.onPostDownloadFromServer();
71 }
72 };
73
74 if (SwingUtilities.isEventDispatchThread()) {
75 r.run();
76 } else {
77 try {
78 SwingUtilities.invokeAndWait(r);
79 } catch(InterruptedException e) {
80 e.printStackTrace();
81 } catch(InvocationTargetException e) {
82 e.printStackTrace();
83 }
84 }
85 }
86
87 @Override
88 protected void realRun() throws SAXException, IOException, OsmTransferException {
89 this.ds = new DataSet();
90 try {
91 synchronized(this) {
92 if (canceled) return;
93 reader = new OsmServerObjectReader(primitiveId, true);
94 }
95 ds = reader.parseOsm(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
96 synchronized(this) {
97 reader = null;
98 }
99 } catch(Exception e) {
100 if (canceled)
101 return;
102 lastException = e;
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.