1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.CheckParameterUtil.ensureParameterNotNull;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.lang.reflect.InvocationTargetException;
|
---|
9 |
|
---|
10 | import javax.swing.SwingUtilities;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
13 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
14 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
15 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
16 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
17 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.io.OsmServerObjectReader;
|
---|
19 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
20 | import org.xml.sax.SAXException;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * The asynchronous task for updating a collection of objects using multi fetch.
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public 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 | }
|
---|