| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.gui.io; |
| | 3 | |
| | 4 | import org.openstreetmap.josm.data.APIDataSet; |
| | 5 | import org.openstreetmap.josm.data.osm.Changeset; |
| | 6 | import org.openstreetmap.josm.data.osm.DataSet; |
| | 7 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 8 | import org.openstreetmap.josm.gui.MainApplication; |
| | 9 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
| | 10 | import org.openstreetmap.josm.gui.progress.ProgressTaskId; |
| | 11 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| | 12 | import org.openstreetmap.josm.io.UploadStrategySpecification; |
| | 13 | import org.openstreetmap.josm.tools.I18n; |
| | 14 | |
| | 15 | import javax.swing.*; |
| | 16 | import java.awt.*; |
| | 17 | import java.util.List; |
| | 18 | import java.util.Optional; |
| | 19 | |
| | 20 | /** |
| | 21 | * Allows to upload the primitives in background |
| | 22 | */ |
| | 23 | public class AsynchronousUploadPrimitivesTask extends UploadPrimitivesTask { |
| | 24 | |
| | 25 | // Static instance |
| | 26 | private static AsynchronousUploadPrimitivesTask asynchronousUploadPrimitivesTask = null; |
| | 27 | |
| | 28 | // flag to hide primitives to be uploaded. |
| | 29 | private static final boolean hideUploadingPrimitives = false; |
| | 30 | |
| | 31 | private final ProgressTaskId taskId; |
| | 32 | private final DataSet dataSet; |
| | 33 | private final List<OsmPrimitive> osmPrimitiveList; |
| | 34 | |
| | 35 | // Restrict creating more Asynchronous upload tasks |
| | 36 | private AsynchronousUploadPrimitivesTask(UploadStrategySpecification uploadStrategySpecification, OsmDataLayer osmDataLayer, APIDataSet apiDataSet, Changeset changeset) { |
| | 37 | super(uploadStrategySpecification, |
| | 38 | osmDataLayer, |
| | 39 | apiDataSet, |
| | 40 | changeset); |
| | 41 | dataSet = osmDataLayer.data; |
| | 42 | osmPrimitiveList = apiDataSet.getPrimitives(); |
| | 43 | disablePrimitives(osmPrimitiveList); |
| | 44 | taskId = new ProgressTaskId("core", "async-upload"); |
| | 45 | } |
| | 46 | |
| | 47 | private static void disablePrimitives(List <OsmPrimitive> osmPrimitiveList) { |
| | 48 | for (OsmPrimitive p : osmPrimitiveList) { |
| | 49 | |
| | 50 | // Disable primitives to be uploaded. |
| | 51 | p.setDisabledState(hideUploadingPrimitives); |
| | 52 | |
| | 53 | // Disable the referrers of the primitives to be uploaded. |
| | 54 | for (OsmPrimitive referrer : p.getReferrers()) { |
| | 55 | referrer.setDisabledState(hideUploadingPrimitives); |
| | 56 | } |
| | 57 | } |
| | 58 | } |
| | 59 | |
| | 60 | private static void enablePrimitives(List <OsmPrimitive> osmPrimitiveList) { |
| | 61 | for (OsmPrimitive p : osmPrimitiveList) { |
| | 62 | |
| | 63 | // Disable primitives to be uploaded. |
| | 64 | p.unsetDisabledState(); |
| | 65 | |
| | 66 | // Disable the referrers of the primitives to be uploaded. |
| | 67 | for (OsmPrimitive referrer : p.getReferrers()) { |
| | 68 | referrer.unsetDisabledState(); |
| | 69 | } |
| | 70 | } |
| | 71 | } |
| | 72 | |
| | 73 | public static Optional<AsynchronousUploadPrimitivesTask> createAsynchronousUploadTask |
| | 74 | (UploadStrategySpecification uploadStrategySpecification, |
| | 75 | OsmDataLayer dataLayer, APIDataSet toUpload, Changeset changeset) { |
| | 76 | synchronized (AsynchronousUploadPrimitivesTask.class) { |
| | 77 | if (asynchronousUploadPrimitivesTask != null) { |
| | 78 | if (!GraphicsEnvironment.isHeadless()) { |
| | 79 | GuiHelper.runInEDTAndWait(() -> |
| | 80 | JOptionPane.showMessageDialog(MainApplication.parent, |
| | 81 | I18n.tr("A background upload is already in progress. Kindly wait for it to finish before uploading new changes"))); |
| | 82 | } |
| | 83 | return Optional.empty(); |
| | 84 | } else { |
| | 85 | DataSet safeCopy = new DataSet(dataLayer.data); |
| | 86 | asynchronousUploadPrimitivesTask = new AsynchronousUploadPrimitivesTask( |
| | 87 | uploadStrategySpecification, |
| | 88 | dataLayer, |
| | 89 | toUpload, |
| | 90 | changeset); |
| | 91 | return Optional.ofNullable(asynchronousUploadPrimitivesTask); |
| | 92 | } |
| | 93 | } |
| | 94 | } |
| | 95 | |
| | 96 | public static Optional<AsynchronousUploadPrimitivesTask> getCurrentAsynchronousUploadTask () { |
| | 97 | return Optional.ofNullable(asynchronousUploadPrimitivesTask); |
| | 98 | } |
| | 99 | |
| | 100 | @Override |
| | 101 | public ProgressTaskId canRunInBackground() { |
| | 102 | return taskId; |
| | 103 | } |
| | 104 | |
| | 105 | @Override |
| | 106 | protected void realRun() { |
| | 107 | super.realRun(); |
| | 108 | } |
| | 109 | |
| | 110 | @Override |
| | 111 | protected void cancel() { |
| | 112 | super.cancel(); |
| | 113 | // Free up the async uploader |
| | 114 | enablePrimitives(osmPrimitiveList); |
| | 115 | asynchronousUploadPrimitivesTask = null; |
| | 116 | } |
| | 117 | |
| | 118 | @Override |
| | 119 | protected void finish() { |
| | 120 | super.finish(); |
| | 121 | // Free up the async uploader |
| | 122 | enablePrimitives(osmPrimitiveList); |
| | 123 | asynchronousUploadPrimitivesTask = null; |
| | 124 | } |
| | 125 | } |