1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
9 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
12 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
13 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
14 | import org.openstreetmap.josm.io.OsmServerWriter;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * UploadLayerTask uploads the data managed by an {@see OsmDataLayer} asynchronously.
|
---|
18 | *
|
---|
19 | * <pre>
|
---|
20 | * ExecutorService executorService = ...
|
---|
21 | * UploadLayerTask task = new UploadLayerTask(layer, monitor);
|
---|
22 | * Future<?> taskFuture = executorServce.submit(task)
|
---|
23 | * try {
|
---|
24 | * // wait for the task to complete
|
---|
25 | * taskFuture.get();
|
---|
26 | * } catch(Exception e) {
|
---|
27 | * e.printStackTracek();
|
---|
28 | * }
|
---|
29 | * </pre>
|
---|
30 | */
|
---|
31 | class UploadLayerTask extends AbstractIOTask implements Runnable {
|
---|
32 | private OsmServerWriter writer;
|
---|
33 | private OsmDataLayer layer;
|
---|
34 | private ProgressMonitor monitor;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | *
|
---|
38 | * @param layer the layer. Must not be null.
|
---|
39 | * @param monitor a progress monitor. If monitor is null, uses {@see NullProgressMonitor#INSTANCE}
|
---|
40 | * @throws IllegalArgumentException thrown, if layer is null
|
---|
41 | */
|
---|
42 | public UploadLayerTask(OsmDataLayer layer, ProgressMonitor monitor) {
|
---|
43 | if (layer == null)
|
---|
44 | throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", layer));
|
---|
45 | if (monitor == null) {
|
---|
46 | monitor = NullProgressMonitor.INSTANCE;
|
---|
47 | }
|
---|
48 | this.layer = layer;
|
---|
49 | this.monitor = monitor;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void run() {
|
---|
54 | monitor.subTask(tr("Preparing primitives to upload ..."));
|
---|
55 | Collection<OsmPrimitive> toUpload = new APIDataSet(layer.data).getPrimitives();
|
---|
56 | if (toUpload.isEmpty())
|
---|
57 | return;
|
---|
58 | writer = new OsmServerWriter();
|
---|
59 | try {
|
---|
60 | ProgressMonitor m = monitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
|
---|
61 | if (isCancelled()) return;
|
---|
62 | writer.uploadOsm(layer.data.version, toUpload, m);
|
---|
63 | } catch (Exception sxe) {
|
---|
64 | if (isCancelled()) {
|
---|
65 | System.out.println("Ignoring exception caught because upload is cancelled. Exception is: " + sxe.toString());
|
---|
66 | return;
|
---|
67 | }
|
---|
68 | setLastException(sxe);
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (isCancelled())
|
---|
72 | return;
|
---|
73 | layer.cleanupAfterUpload(writer.getProcessedPrimitives());
|
---|
74 | DataSet.fireSelectionChanged(layer.data.getSelected());
|
---|
75 | layer.fireDataChange();
|
---|
76 | layer.onPostUploadToServer();
|
---|
77 | }
|
---|
78 |
|
---|
79 | @Override
|
---|
80 | public void cancel() {
|
---|
81 | // make sure the the softCancel operation is serialized with
|
---|
82 | // blocks which can be interrupted.
|
---|
83 | setCancelled(true);
|
---|
84 | if (writer != null) {
|
---|
85 | writer.disconnectActiveConnection();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|