Changeset 7358 in josm for trunk/src


Ignore:
Timestamp:
2014-08-01T23:03:40+02:00 (5 years ago)
Author:
Don-vip
Message:

see #9899 - refactor save/upload layer stuff to allow different layers (i.e: notes layer) to benefit from the mechanism

Location:
trunk/src/org/openstreetmap/josm
Files:
2 added
11 edited

Legend:

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

    r7335 r7358  
    7777import org.openstreetmap.josm.gui.io.SaveLayersDialog;
    7878import org.openstreetmap.josm.gui.layer.Layer;
     79import org.openstreetmap.josm.gui.layer.ModifiableLayer;
    7980import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    8081import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener;
     
    890891
    891892    /**
    892      * Asks user to perform "save layer" operations (save .osm on disk and/or upload osm data to server) for all {@link OsmDataLayer} before JOSM exits.
     893     * Asks user to perform "save layer" operations (save on disk and/or upload data to server) for all {@link ModifiableLayer} before JOSM exits.
    893894     * @return {@code true} if there was nothing to save, or if the user wants to proceed to save operations. {@code false} if the user cancels.
    894895     * @since 2025
     
    896897    public static boolean saveUnsavedModifications() {
    897898        if (!isDisplayingMapView()) return true;
    898         return saveUnsavedModifications(map.mapView.getLayersOfType(OsmDataLayer.class), true);
    899     }
    900 
    901     /**
    902      * Asks user to perform "save layer" operations (save .osm on disk and/or upload osm data to server) before osm layers deletion.
    903      *
    904      * @param selectedLayers The layers to check. Only instances of {@link OsmDataLayer} are considered.
     899        return saveUnsavedModifications(map.mapView.getLayersOfType(ModifiableLayer.class), true);
     900    }
     901
     902    /**
     903     * Asks user to perform "save layer" operations (save on disk and/or upload data to server) before data layers deletion.
     904     *
     905     * @param selectedLayers The layers to check. Only instances of {@link ModifiableLayer} are considered.
    905906     * @param exit {@code true} if JOSM is exiting, {@code false} otherwise.
    906907     * @return {@code true} if there was nothing to save, or if the user wants to proceed to save operations. {@code false} if the user cancels.
     
    909910    public static boolean saveUnsavedModifications(Iterable<? extends Layer> selectedLayers, boolean exit) {
    910911        SaveLayersDialog dialog = new SaveLayersDialog(parent);
    911         List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<>();
     912        List<ModifiableLayer> layersWithUnmodifiedChanges = new ArrayList<>();
    912913        for (Layer l: selectedLayers) {
    913             if (!(l instanceof OsmDataLayer)) {
     914            if (!(l instanceof ModifiableLayer)) {
    914915                continue;
    915916            }
    916             OsmDataLayer odl = (OsmDataLayer)l;
    917             if ((odl.requiresSaveToFile() || (odl.requiresUploadToServer() && !odl.isUploadDiscouraged())) && odl.data.isModified()) {
     917            ModifiableLayer odl = (ModifiableLayer)l;
     918            if ((odl.requiresSaveToFile() || (odl.requiresUploadToServer() && !odl.isUploadDiscouraged())) && odl.isModified()) {
    918919                layersWithUnmodifiedChanges.add(odl);
    919920            }
  • trunk/src/org/openstreetmap/josm/actions/UploadAction.java

    r7005 r7358  
    2727import org.openstreetmap.josm.gui.io.UploadDialog;
    2828import org.openstreetmap.josm.gui.io.UploadPrimitivesTask;
     29import org.openstreetmap.josm.gui.layer.ModifiableLayer;
    2930import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3031import org.openstreetmap.josm.gui.util.GuiHelper;
     
    142143    }
    143144
    144     public boolean checkPreUploadConditions(OsmDataLayer layer) {
    145         return checkPreUploadConditions(layer, new APIDataSet(layer.data));
     145    public static boolean checkPreUploadConditions(ModifiableLayer layer) {
     146        return checkPreUploadConditions(layer,
     147                layer instanceof OsmDataLayer ? new APIDataSet(((OsmDataLayer)layer).data) : null);
    146148    }
    147149
     
    162164     * want to continue
    163165     */
    164     public static boolean warnUploadDiscouraged(OsmDataLayer layer) {
     166    public static boolean warnUploadDiscouraged(ModifiableLayer layer) {
    165167        return GuiHelper.warnUser(tr("Upload discouraged"),
    166168                "<html>" +
     
    182184     * @return true, if the preconditions are met; false, otherwise
    183185     */
    184     public boolean checkPreUploadConditions(OsmDataLayer layer, APIDataSet apiData) {
     186    public static boolean checkPreUploadConditions(ModifiableLayer layer, APIDataSet apiData) {
    185187        if (layer.isUploadDiscouraged()) {
    186188            if (warnUploadDiscouraged(layer)) {
     
    188190            }
    189191        }
    190         ConflictCollection conflicts = layer.getConflicts();
    191         if (apiData.participatesInConflict(conflicts)) {
    192             alertUnresolvedConflicts(layer);
    193             return false;
     192        if (layer instanceof OsmDataLayer) {
     193            OsmDataLayer osmLayer = (OsmDataLayer) layer;
     194            ConflictCollection conflicts = osmLayer.getConflicts();
     195            if (apiData.participatesInConflict(conflicts)) {
     196                alertUnresolvedConflicts(osmLayer);
     197                return false;
     198            }
    194199        }
    195200        // Call all upload hooks in sequence.
    196201        // FIXME: this should become an asynchronous task
    197202        //
    198         for (UploadHook hook : uploadHooks) {
    199             if (!hook.checkUpload(apiData))
    200                 return false;
     203        if (apiData != null) {
     204            for (UploadHook hook : uploadHooks) {
     205                if (!hook.checkUpload(apiData))
     206                    return false;
     207            }
    201208        }
    202209
  • trunk/src/org/openstreetmap/josm/gui/io/AbstractIOTask.java

    r6084 r7358  
    1515    private Exception lastException;
    1616
     17    /**
     18     * Contructs a new {@code AbstractIOTask}.
     19     */
    1720    public AbstractIOTask() {
    1821        canceled = false;
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayerInfo.java

    r7204 r7358  
    44import java.io.File;
    55
    6 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     6import org.openstreetmap.josm.gui.layer.ModifiableLayer;
    77import org.openstreetmap.josm.tools.CheckParameterUtil;
    88
     
    1010 * SaveLayerInfo represents the information, user preferences and save/upload states of
    1111 * a layer which might be uploaded/saved.
    12  *
     12 * @since 2025
    1313 */
    1414class SaveLayerInfo implements Comparable<SaveLayerInfo> {
    1515
    16     /** the osm data layer */
    17     private OsmDataLayer layer;
     16    /** the modifiable layer */
     17    private ModifiableLayer layer;
    1818    private boolean doCheckSaveConditions;
    1919    private boolean doSaveToFile;
     
    2828     * @throws IllegalArgumentException thrown if layer is null
    2929     */
    30     public SaveLayerInfo(OsmDataLayer layer) {
     30    public SaveLayerInfo(ModifiableLayer layer) {
    3131        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    3232        this.layer = layer;
     
    4242     * @return the layer this info objects holds information for
    4343     */
    44     public OsmDataLayer getLayer() {
     44    public ModifiableLayer getLayer() {
    4545        return layer;
    4646    }
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayerTask.java

    r7204 r7358  
    1111
    1212/**
    13  * SaveLayerTask saves the data managed by an {@link org.openstreetmap.josm.gui.layer.OsmDataLayer} to the
    14  * {@link org.openstreetmap.josm.gui.layer.OsmDataLayer#getAssociatedFile()}.
     13 * SaveLayerTask saves the data managed by an {@link org.openstreetmap.josm.gui.layer.ModifiableLayer} to the
     14 * {@link org.openstreetmap.josm.gui.layer.Layer#getAssociatedFile()}.
    1515 *
    1616 * <pre>
     
    2626 * </pre>
    2727 */
    28 class SaveLayerTask extends AbstractIOTask {
     28public class SaveLayerTask extends AbstractIOTask {
    2929    private SaveLayerInfo layerInfo;
    3030    private ProgressMonitor parentMonitor;
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersDialog.java

    r7204 r7358  
    4444import org.openstreetmap.josm.Main;
    4545import org.openstreetmap.josm.actions.UploadAction;
    46 import org.openstreetmap.josm.data.APIDataSet;
    4746import org.openstreetmap.josm.gui.ExceptionDialogUtil;
    4847import org.openstreetmap.josm.gui.io.SaveLayersModel.Mode;
     48import org.openstreetmap.josm.gui.layer.ModifiableLayer;
    4949import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    5050import org.openstreetmap.josm.gui.progress.SwingRenderingProgressMonitor;
     
    449449        protected void uploadLayers(List<SaveLayerInfo> toUpload) {
    450450            for (final SaveLayerInfo layerInfo: toUpload) {
     451                ModifiableLayer layer = layerInfo.getLayer();
    451452                if (canceled) {
    452                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
     453                    model.setUploadState(layer, UploadOrSaveState.CANCELED);
    453454                    continue;
    454455                }
    455456                monitor.subTask(tr("Preparing layer ''{0}'' for upload ...", layerInfo.getName()));
    456457
    457                 if (!new UploadAction().checkPreUploadConditions(layerInfo.getLayer())) {
    458                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.FAILED);
     458                if (!UploadAction.checkPreUploadConditions(layer)) {
     459                    model.setUploadState(layer, UploadOrSaveState.FAILED);
    459460                    continue;
    460461                }
    461                 final UploadDialog dialog = UploadDialog.getUploadDialog();
    462                 dialog.setUploadedPrimitives(new APIDataSet(layerInfo.getLayer().data));
    463                 dialog.setVisible(true);
    464                 if (dialog.isCanceled()) {
    465                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
     462
     463                AbstractUploadDialog dialog = layer.getUploadDialog();
     464                if (dialog != null) {
     465                    dialog.setVisible(true);
     466                    if (dialog.isCanceled()) {
     467                        model.setUploadState(layer, UploadOrSaveState.CANCELED);
     468                        continue;
     469                    }
     470                    dialog.rememberUserInput();
     471                }
     472
     473                currentTask = layer.createUploadTask(monitor);
     474                if (currentTask == null) {
     475                    model.setUploadState(layer, UploadOrSaveState.FAILED);
    466476                    continue;
    467477                }
    468                 dialog.rememberUserInput();
    469 
    470                 currentTask = new UploadLayerTask(
    471                         UploadDialog.getUploadDialog().getUploadStrategySpecification(),
    472                         layerInfo.getLayer(),
    473                         monitor,
    474                         UploadDialog.getUploadDialog().getChangeset()
    475                 );
    476478                currentFuture = worker.submit(currentTask);
    477479                try {
     
    480482                    currentFuture.get();
    481483                } catch(CancellationException e) {
    482                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
     484                    model.setUploadState(layer, UploadOrSaveState.CANCELED);
    483485                } catch(Exception e) {
    484486                    Main.error(e);
    485                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.FAILED);
     487                    model.setUploadState(layer, UploadOrSaveState.FAILED);
    486488                    ExceptionDialogUtil.explainException(e);
    487489                }
    488490                if (currentTask.isCanceled()) {
    489                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.CANCELED);
     491                    model.setUploadState(layer, UploadOrSaveState.CANCELED);
    490492                } else if (currentTask.isFailed()) {
    491493                    Main.error(currentTask.getLastException());
    492494                    ExceptionDialogUtil.explainException(currentTask.getLastException());
    493                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.FAILED);
     495                    model.setUploadState(layer, UploadOrSaveState.FAILED);
    494496                } else {
    495                     model.setUploadState(layerInfo.getLayer(), UploadOrSaveState.OK);
     497                    model.setUploadState(layer, UploadOrSaveState.OK);
    496498                }
    497499                currentTask = null;
  • trunk/src/org/openstreetmap/josm/gui/io/SaveLayersModel.java

    r7005 r7358  
    1212import javax.swing.table.DefaultTableModel;
    1313
     14import org.openstreetmap.josm.gui.layer.ModifiableLayer;
    1415import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1516
     
    5960    }
    6061
    61     public void populate(List<OsmDataLayer> layers) {
     62    /**
     63     * Populates the model with given modifiable layers.
     64     * @param layers The layers to use to populate this model
     65     * @since 7358
     66     */
     67    public void populate(List<? extends ModifiableLayer> layers) {
    6268        layerInfo = new ArrayList<>();
    6369        if (layers == null) return;
    64         for (OsmDataLayer layer: layers) {
     70        for (ModifiableLayer layer: layers) {
    6571            layerInfo.add(new SaveLayerInfo(layer));
    6672        }
     
    135141
    136142    public List<SaveLayerInfo> getLayersWithConflictsAndUploadRequest() {
    137         List<SaveLayerInfo> ret =new ArrayList<>();
    138         for (SaveLayerInfo info: layerInfo) {
    139             if (info.isDoUploadToServer() && !info.getLayer().getConflicts().isEmpty()) {
     143        List<SaveLayerInfo> ret = new ArrayList<>();
     144        for (SaveLayerInfo info: layerInfo) {
     145            ModifiableLayer l = info.getLayer();
     146            if (info.isDoUploadToServer() && l instanceof OsmDataLayer && !((OsmDataLayer)l).getConflicts().isEmpty()) {
    140147                ret.add(info);
    141148            }
     
    145152
    146153    public List<SaveLayerInfo> getLayersToUpload() {
    147         List<SaveLayerInfo> ret =new ArrayList<>();
     154        List<SaveLayerInfo> ret = new ArrayList<>();
    148155        for (SaveLayerInfo info: layerInfo) {
    149156            if (info.isDoUploadToServer()) {
     
    155162
    156163    public List<SaveLayerInfo> getLayersToSave() {
    157         List<SaveLayerInfo> ret =new ArrayList<>();
     164        List<SaveLayerInfo> ret = new ArrayList<>();
    158165        for (SaveLayerInfo info: layerInfo) {
    159166            if (info.isDoSaveToFile()) {
     
    164171    }
    165172
    166     public void setUploadState(OsmDataLayer layer, UploadOrSaveState state) {
     173    public void setUploadState(ModifiableLayer layer, UploadOrSaveState state) {
    167174        SaveLayerInfo info = getSaveLayerInfo(layer);
    168175        if (info != null) {
     
    172179    }
    173180
    174     public void setSaveState(OsmDataLayer layer, UploadOrSaveState state) {
     181    public void setSaveState(ModifiableLayer layer, UploadOrSaveState state) {
    175182        SaveLayerInfo info = getSaveLayerInfo(layer);
    176183        if (info != null) {
     
    180187    }
    181188
    182     public SaveLayerInfo getSaveLayerInfo(OsmDataLayer layer) {
     189    public SaveLayerInfo getSaveLayerInfo(ModifiableLayer layer) {
    183190        for (SaveLayerInfo info: this.layerInfo) {
    184191            if (info.getLayer() == layer)
  • trunk/src/org/openstreetmap/josm/gui/io/UploadDialog.java

    r7190 r7358  
    3131import javax.swing.JButton;
    3232import javax.swing.JComponent;
    33 import javax.swing.JDialog;
    3433import javax.swing.JOptionPane;
    3534import javax.swing.JPanel;
     
    5958 * This is a dialog for entering upload options like the parameters for
    6059 * the upload changeset and the strategy for opening/closing a changeset.
    61  *
     60 * @since 2025
    6261 */
    63 public class UploadDialog extends JDialog implements PropertyChangeListener, PreferenceChangedListener{
     62public class UploadDialog extends AbstractUploadDialog implements PropertyChangeListener, PreferenceChangedListener {
    6463    /**  the unique instance of the upload dialog */
    6564    private static UploadDialog uploadDialog;
     
    9796    /** the upload button */
    9897    private JButton btnUpload;
    99     private boolean canceled = false;
    10098
    10199    /** the changeset comment model keeping the state of the changeset comment */
     
    267265    }
    268266
    269     /**
    270      * Remembers the user input in the preference settings
    271      */
     267    @Override
    272268    public void rememberUserInput() {
    273269        pnlBasicUploadSettings.rememberUserInput();
     
    345341    protected String getUploadSource() {
    346342        return changesetSourceModel.getComment();
    347     }
    348 
    349     /**
    350      * Returns true if the dialog was canceled
    351      *
    352      * @return true if the dialog was canceled
    353      */
    354     public boolean isCanceled() {
    355         return canceled;
    356     }
    357 
    358     /**
    359      * Sets whether the dialog was canceled
    360      *
    361      * @param canceled true if the dialog is canceled
    362      */
    363     protected void setCanceled(boolean canceled) {
    364         this.canceled = canceled;
    365343    }
    366344
  • trunk/src/org/openstreetmap/josm/gui/io/UploadLayerTask.java

    r7005 r7358  
    4040 * </pre>
    4141 */
    42 class UploadLayerTask extends AbstractIOTask implements Runnable {
     42public class UploadLayerTask extends AbstractIOTask implements Runnable {
    4343    private OsmServerWriter writer;
    4444    private OsmDataLayer layer;
  • trunk/src/org/openstreetmap/josm/gui/layer/Layer.java

    r6890 r7358  
    119119    /**
    120120     * Create the layer and fill in the necessary components.
     121     * @param name Layer name
    121122     */
    122123    public Layer(String name) {
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r7299 r7358  
    4444import org.openstreetmap.josm.actions.SaveActionBase;
    4545import org.openstreetmap.josm.actions.ToggleUploadDiscouragedLayerAction;
     46import org.openstreetmap.josm.data.APIDataSet;
    4647import org.openstreetmap.josm.data.Bounds;
    4748import org.openstreetmap.josm.data.SelectionChangedListener;
     
    7677import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    7778import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
     79import org.openstreetmap.josm.gui.io.AbstractIOTask;
     80import org.openstreetmap.josm.gui.io.AbstractUploadDialog;
     81import org.openstreetmap.josm.gui.io.UploadDialog;
     82import org.openstreetmap.josm.gui.io.UploadLayerTask;
    7883import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
    7984import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     
    9196 * @author imi
    9297 */
    93 public class OsmDataLayer extends Layer implements Listener, SelectionChangedListener {
     98public class OsmDataLayer extends ModifiableLayer implements Listener, SelectionChangedListener {
    9499    public static final String REQUIRES_SAVE_TO_DISK_PROP = OsmDataLayer.class.getName() + ".requiresSaveToDisk";
    95100    public static final String REQUIRES_UPLOAD_TO_SERVER_PROP = OsmDataLayer.class.getName() + ".requiresUploadToServer";
     
    622627    }
    623628
    624     /**
    625      * Replies true if the data managed by this layer needs to be uploaded to
    626      * the server because it contains at least one modified primitive.
    627      *
    628      * @return true if the data managed by this layer needs to be uploaded to
    629      * the server because it contains at least one modified primitive; false,
    630      * otherwise
    631      */
     629    @Override
    632630    public boolean requiresUploadToServer() {
    633631        return requiresUploadToServer;
    634632    }
    635633
    636     /**
    637      * Replies true if the data managed by this layer needs to be saved to
    638      * a file. Only replies true if a file is assigned to this layer and
    639      * if the data managed by this layer has been modified since the last
    640      * save operation to the file.
    641      *
    642      * @return true if the data managed by this layer needs to be saved to
    643      * a file
    644      */
     634    @Override
    645635    public boolean requiresSaveToFile() {
    646636        return getAssociatedFile() != null && requiresSaveToFile;
     
    650640    public void onPostLoadFromFile() {
    651641        setRequiresSaveToFile(false);
    652         setRequiresUploadToServer(data.isModified());
     642        setRequiresUploadToServer(isModified());
    653643    }
    654644
    655645    public void onPostDownloadFromServer() {
    656646        setRequiresSaveToFile(true);
    657         setRequiresUploadToServer(data.isModified());
     647        setRequiresUploadToServer(isModified());
    658648    }
    659649
     
    663653    }
    664654
    665     /**
    666      * Initializes the layer after a successful save of OSM data to a file
    667      *
    668      */
     655    @Override
    669656    public void onPostSaveToFile() {
    670657        setRequiresSaveToFile(false);
    671         setRequiresUploadToServer(data.isModified());
    672     }
    673 
    674     /**
    675      * Initializes the layer after a successful upload to the server
    676      *
    677      */
     658        setRequiresUploadToServer(isModified());
     659    }
     660
     661    @Override
    678662    public void onPostUploadToServer() {
    679         setRequiresUploadToServer(data.isModified());
     663        setRequiresUploadToServer(isModified());
    680664        // keep requiresSaveToDisk unchanged
    681665    }
     
    730714    }
    731715
     716    @Override
    732717    public final boolean isUploadDiscouraged() {
    733718        return data.isUploadDiscouraged();
     
    741726            }
    742727        }
     728    }
     729
     730    @Override
     731    public final boolean isModified() {
     732        return data.isModified();
    743733    }
    744734
     
    810800        return SaveActionBase.createAndOpenSaveFileChooser(tr("Save OSM file"), "osm");
    811801    }
     802
     803    @Override
     804    public AbstractIOTask createUploadTask(final ProgressMonitor monitor) {
     805        UploadDialog dialog = UploadDialog.getUploadDialog();
     806        return new UploadLayerTask(
     807                dialog.getUploadStrategySpecification(),
     808                this,
     809                monitor,
     810                dialog.getChangeset());
     811    }
     812
     813    @Override
     814    public AbstractUploadDialog getUploadDialog() {
     815        UploadDialog dialog = UploadDialog.getUploadDialog();
     816        dialog.setUploadedPrimitives(new APIDataSet(data));
     817        return dialog;
     818    }
    812819}
Note: See TracChangeset for help on using the changeset viewer.