Changeset 13133 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2017-11-19T22:52:24+01:00 (6 years ago)
Author:
bastiK
Message:

applied #8509 - Background uploading (patch by udit, minor changes)

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/UploadAction.java

    r13123 r13133  
    1010import java.util.List;
    1111import java.util.Map;
     12import java.util.Optional;
    1213
    1314import javax.swing.JOptionPane;
     
    2526import org.openstreetmap.josm.gui.HelpAwareOptionPane;
    2627import org.openstreetmap.josm.gui.MainApplication;
     28import org.openstreetmap.josm.gui.io.AsynchronousUploadPrimitivesTask;
    2729import org.openstreetmap.josm.gui.io.UploadDialog;
    2830import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
     
    3032import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3133import org.openstreetmap.josm.gui.util.GuiHelper;
     34import org.openstreetmap.josm.spi.preferences.Config;
    3235import org.openstreetmap.josm.tools.ImageProvider;
    3336import org.openstreetmap.josm.tools.Shortcut;
     
    5861    private static final List<UploadHook> LATE_UPLOAD_HOOKS = new LinkedList<>();
    5962
     63    private static final String IS_ASYNC_UPLOAD_ENABLED = "asynchronous.upload";
     64
    6065    static {
    6166        /**
     
    260265        }
    261266
    262         MainApplication.worker.execute(
    263                 new UploadPrimitivesTask(
    264                         UploadDialog.getUploadDialog().getUploadStrategySpecification(),
    265                         layer,
    266                         apiData,
    267                         cs
    268                 )
    269         );
     267        if (Config.getPref().getBoolean(IS_ASYNC_UPLOAD_ENABLED, true)) {
     268            Optional<AsynchronousUploadPrimitivesTask> asyncUploadTask = AsynchronousUploadPrimitivesTask.createAsynchronousUploadTask(
     269                    UploadDialog.getUploadDialog().getUploadStrategySpecification(),
     270                    layer,
     271                    apiData,
     272                    cs);
     273
     274            if (asyncUploadTask.isPresent()) {
     275                MainApplication.worker.execute(asyncUploadTask.get());
     276            }
     277        } else {
     278            MainApplication.worker.execute(
     279                    new UploadPrimitivesTask(
     280                            UploadDialog.getUploadDialog().getUploadStrategySpecification(),
     281                            layer,
     282                            apiData,
     283                            cs));
     284        }
    270285    }
    271286
  • trunk/src/org/openstreetmap/josm/gui/layer/MainLayerManager.java

    r11774 r13133  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.gui.layer;
     3
     4import static org.openstreetmap.josm.tools.I18n.tr;
    35
    46import java.util.ArrayList;
     
    810import java.util.concurrent.CopyOnWriteArrayList;
    911
     12import javax.swing.JOptionPane;
     13
    1014import org.openstreetmap.josm.data.osm.DataSet;
     15import org.openstreetmap.josm.gui.MainApplication;
    1116import org.openstreetmap.josm.gui.util.GuiHelper;
    1217
     
    206211
    207212    /**
    208      * Set the active layer. If the layer is an OsmDataLayer, the edit layer is also changed.
     213     * Set the active layer, unless the layer is read-only.
     214     * If the layer is an OsmDataLayer, the edit layer is also changed.
    209215     * @param layer The active layer.
    210216     */
     
    212218        // we force this on to the EDT Thread to make events fire from there.
    213219        // The synchronization lock needs to be held by the EDT.
    214         GuiHelper.runInEDTAndWaitWithException(() -> realSetActiveLayer(layer));
     220        if (layer instanceof OsmDataLayer && ((OsmDataLayer) layer).isReadOnly()) {
     221            GuiHelper.runInEDT(() ->
     222                    JOptionPane.showMessageDialog(
     223                            MainApplication.parent,
     224                            tr("Trying to set a read only data layer as edit layer"),
     225                            tr("Warning"),
     226                            JOptionPane.WARNING_MESSAGE));
     227        } else {
     228            GuiHelper.runInEDTAndWaitWithException(() -> realSetActiveLayer(layer));
     229        }
    215230    }
    216231
     
    310325     */
    311326    public synchronized Layer getActiveLayer() {
    312         return activeLayer;
    313     }
    314 
    315     /**
    316      * Replies the current edit layer, if any
     327        if (activeLayer instanceof OsmDataLayer) {
     328            if (!((OsmDataLayer) activeLayer).isReadOnly()) {
     329                return activeLayer;
     330            } else {
     331                return null;
     332            }
     333        } else {
     334            return activeLayer;
     335        }
     336    }
     337
     338    /**
     339     * Replies the current edit layer, if present and not readOnly
    317340     *
    318341     * @return the current edit layer. May be null.
    319342     */
    320343    public synchronized OsmDataLayer getEditLayer() {
    321         return editLayer;
     344        if (editLayer != null && !editLayer.isReadOnly())
     345            return editLayer;
     346        else
     347            return null;
    322348    }
    323349
     
    379405        layerAvailabilityListeners.clear();
    380406    }
     407
     408    /**
     409     * Prepares an OsmDataLayer for upload. The layer to be uploaded is locked and
     410     * if the layer to be uploaded is the current editLayer then editLayer is reset
     411     * to null for disallowing any changes to the layer. An ActiveLayerChangeEvent
     412     * is fired to notify the listeners
     413     *
     414     * @param layer The OsmDataLayer to be uploaded
     415     */
     416    public void prepareLayerForUpload(OsmDataLayer layer) {
     417
     418        GuiHelper.assertCallFromEdt();
     419        layer.setReadOnly();
     420
     421        // Reset only the edit layer as empty
     422        if (editLayer == layer) {
     423            ActiveLayerChangeEvent activeLayerChangeEvent = new ActiveLayerChangeEvent(this, editLayer, activeLayer);
     424            editLayer = null;
     425            fireActiveLayerChange(activeLayerChangeEvent);
     426        }
     427    }
     428
     429    /**
     430     * Post upload processing of the OsmDataLayer.
     431     * If the current edit layer is empty this function sets the layer uploaded as the
     432     * current editLayer. An ActiveLayerChangeEvent is fired to notify the listeners
     433     *
     434     * @param layer The OsmDataLayer uploaded
     435     */
     436    public void processLayerAfterUpload(OsmDataLayer layer) {
     437        GuiHelper.assertCallFromEdt();
     438        layer.unsetReadOnly();
     439
     440        // Set the layer as edit layer if the edit layer is empty.
     441        if (editLayer == null) {
     442            ActiveLayerChangeEvent layerChangeEvent = new ActiveLayerChangeEvent(this, editLayer, activeLayer);
     443            editLayer = layer;
     444            fireActiveLayerChange(layerChangeEvent);
     445        }
     446    }
    381447}
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r13130 r13133  
    3232import java.util.Set;
    3333import java.util.concurrent.CopyOnWriteArrayList;
     34import java.util.concurrent.atomic.AtomicBoolean;
    3435import java.util.concurrent.atomic.AtomicInteger;
    3536import java.util.regex.Pattern;
     
    130131    private boolean requiresSaveToFile;
    131132    private boolean requiresUploadToServer;
     133    /** Flag used to know if the layer should not be editable */
     134    private final AtomicBoolean isReadOnly = new AtomicBoolean(false);
    132135
    133136    /**
     
    421424            base.addOverlay(new ImageOverlay(new ImageProvider("warning-small"), 0.5, 0.5, 1.0, 1.0));
    422425        }
     426
     427        if (isReadOnly()) {
     428            // If the layer is read only then change the default icon to a clock
     429            base = new ImageProvider("clock").setMaxSize(ImageSizes.LAYER);
     430        }
    423431        return base.get();
    424432    }
     
    11441152        super.setName(name);
    11451153    }
     1154
     1155    /**
     1156     * Sets the isReadOnly flag for the OsmDataLayer as true
     1157     */
     1158    public void setReadOnly() {
     1159        if (!isReadOnly.compareAndSet(false, true)) {
     1160            Logging.warn("Trying to set readOnly flag on a readOnly layer ", this.getName());
     1161        }
     1162    }
     1163
     1164    /**
     1165     * Sets the isReadOnly flag for the OsmDataLayer as false
     1166     */
     1167    public void unsetReadOnly() {
     1168        if (!isReadOnly.compareAndSet(true, false)) {
     1169            Logging.warn("Trying to unset readOnly flag on a non-readOnly layer ", this.getName());
     1170        }
     1171    }
     1172
     1173    /**
     1174     * Returns the value of the isReadOnly flag for the OsmDataLayer
     1175     * @return isReadOnly
     1176     */
     1177    public boolean isReadOnly() {
     1178        return isReadOnly.get();
     1179    }
    11461180}
Note: See TracChangeset for help on using the changeset viewer.