Changeset 5519 in josm


Ignore:
Timestamp:
2012-09-30T17:36:39+02:00 (7 years ago)
Author:
Don-vip
Message:

fix #8090 - Removing saved non-uploadable layer asks for confirmation

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

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

    r5514 r5519  
    628628    }
    629629
     630    /**
     631     * 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.
     632     * @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.
     633     * @since 2025
     634     */
    630635    public static boolean saveUnsavedModifications() {
    631         if (map == null) return true;
    632         SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
     636        if (map == null || map.mapView == null) return true;
     637        return saveUnsavedModifications(map.mapView.getLayersOfType(OsmDataLayer.class), true);
     638    }
     639
     640    /**
     641     * Asks user to perform "save layer" operations (save .osm on disk and/or upload osm data to server) before osm layers deletion.
     642     *
     643     * @param selectedLayers The layers to check. Only instances of {@link OsmDataLayer} are considered.
     644     * @param exit {@code true} if JOSM is exiting, {@code false} otherwise.
     645     * @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.
     646     * @since 5519
     647     */
     648    public static boolean saveUnsavedModifications(List<? extends Layer> selectedLayers, boolean exit) {
     649        SaveLayersDialog dialog = new SaveLayersDialog(parent);
    633650        List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
    634         for (OsmDataLayer l: Main.map.mapView.getLayersOfType(OsmDataLayer.class)) {
    635             if ((l.requiresSaveToFile() || l.requiresUploadToServer()) && l.data.isModified()) {
    636                 layersWithUnmodifiedChanges.add(l);
    637             }
    638         }
    639         dialog.prepareForSavingAndUpdatingLayersBeforeExit();
     651        for (Layer l: selectedLayers) {
     652            if (!(l instanceof OsmDataLayer)) {
     653                continue;
     654            }
     655            OsmDataLayer odl = (OsmDataLayer)l;
     656            if ((odl.requiresSaveToFile() || (odl.requiresUploadToServer() && !odl.isUploadDiscouraged())) && odl.data.isModified()) {
     657                layersWithUnmodifiedChanges.add(odl);
     658            }
     659        }
     660        if (exit) {
     661            dialog.prepareForSavingAndUpdatingLayersBeforeExit();
     662        } else {
     663            dialog.prepareForSavingAndUpdatingLayersBeforeDelete();
     664        }
    640665        if (!layersWithUnmodifiedChanges.isEmpty()) {
    641666            dialog.getModel().populate(layersWithUnmodifiedChanges);
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r5472 r5519  
    6969 * @author imi
    7070 */
    71 public class MapView extends NavigatableComponent implements PropertyChangeListener, PreferenceChangedListener {
     71public class MapView extends NavigatableComponent implements PropertyChangeListener, PreferenceChangedListener, OsmDataLayer.LayerStateChangeListener {
    7272
    7373    /**
     
    312312        }
    313313        fireLayerAdded(layer);
    314         boolean callSetActiveLayer = layer instanceof OsmDataLayer || activeLayer == null;
     314        boolean isOsmDataLayer = layer instanceof OsmDataLayer;
     315        if (isOsmDataLayer) {
     316            ((OsmDataLayer)layer).addLayerStateChangeListener(this);
     317        }
     318        boolean callSetActiveLayer = isOsmDataLayer || activeLayer == null;
    315319        if (callSetActiveLayer) {
    316320            // autoselect the new layer
     
    393397        if (layer == activeLayer) {
    394398            setActiveLayer(determineNextActiveLayer(layersList), false);
     399        }
     400       
     401        if (layer instanceof OsmDataLayer) {
     402            ((OsmDataLayer)layer).removeLayerPropertyChangeListener(this);
    395403        }
    396404
     
    863871
    864872    protected void refreshTitle() {
    865         boolean dirty = editLayer != null && (editLayer.requiresSaveToFile() || editLayer.requiresUploadToServer());
     873        boolean dirty = editLayer != null && (editLayer.requiresSaveToFile() || (editLayer.requiresUploadToServer() && !editLayer.isUploadDiscouraged()));
    866874        if (dirty) {
    867875            JOptionPane.getFrameForComponent(Main.parent).setTitle("* " + tr("Java OpenStreetMap Editor"));
     
    893901    }
    894902
     903    @Override
     904    public void uploadDiscouragedChanged(OsmDataLayer layer, boolean newValue) {
     905        if (layer == getEditLayer()) {
     906            refreshTitle();
     907        }
     908    }
    895909}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    r5449 r5519  
    389389        }
    390390
    391         protected boolean enforceUploadOrSaveModifiedData(List<Layer> selectedLayers) {
    392             SaveLayersDialog dialog = new SaveLayersDialog(Main.parent);
    393             List<OsmDataLayer> layersWithUnmodifiedChanges = new ArrayList<OsmDataLayer>();
    394             for (Layer l: selectedLayers) {
    395                 if (! (l instanceof OsmDataLayer)) {
    396                     continue;
    397                 }
    398                 OsmDataLayer odl = (OsmDataLayer)l;
    399                 if ((odl.requiresSaveToFile() || odl.requiresUploadToServer()) && odl.data.isModified()) {
    400                     layersWithUnmodifiedChanges.add(odl);
    401                 }
    402             }
    403             dialog.prepareForSavingAndUpdatingLayersBeforeDelete();
    404             if (!layersWithUnmodifiedChanges.isEmpty()) {
    405                 dialog.getModel().populate(layersWithUnmodifiedChanges);
    406                 dialog.setVisible(true);
    407                 switch(dialog.getUserAction()) {
    408                 case CANCEL: return false;
    409                 case PROCEED: return true;
    410                 default: return false;
    411                 }
    412             }
    413             return true;
    414         }
    415 
    416391        @Override
    417392        public void actionPerformed(ActionEvent e) {
     
    419394            if (selectedLayers.isEmpty())
    420395                return;
    421             if (! enforceUploadOrSaveModifiedData(selectedLayers))
     396            if (!Main.saveUnsavedModifications(selectedLayers, false))
    422397                return;
    423             for(Layer l: selectedLayers) {
     398            for (Layer l: selectedLayers) {
    424399                Main.main.removeLayer(l);
    425400            }
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r5459 r5519  
    2828import java.util.List;
    2929import java.util.Map;
     30import java.util.concurrent.CopyOnWriteArrayList;
    3031
    3132import javax.swing.AbstractAction;
     
    165166        void commandChanged(int queueSize, int redoSize);
    166167    }
     168   
     169    /**
     170     * Listener called when a state of this layer has changed.
     171     */
     172    public interface LayerStateChangeListener {
     173        /**
     174         * Notifies that the "upload discouraged" (upload=no) state has changed.
     175         * @param layer The layer that has been modified
     176         * @param newValue The new value of the state
     177         */
     178        void uploadDiscouragedChanged(OsmDataLayer layer, boolean newValue);
     179    }
     180   
     181    private final CopyOnWriteArrayList<LayerStateChangeListener> layerStateChangeListeners = new CopyOnWriteArrayList<LayerStateChangeListener>();
     182   
     183    /**
     184     * Adds a layer state change listener
     185     *
     186     * @param listener the listener. Ignored if null or already registered.
     187     * @since 5519
     188     */
     189    public void addLayerStateChangeListener(LayerStateChangeListener listener) {
     190        if (listener != null) {
     191            layerStateChangeListeners.addIfAbsent(listener);
     192        }
     193    }
     194   
     195    /**
     196     * Removes a layer property change listener
     197     *
     198     * @param listener the listener. Ignored if null or already registered.
     199     * @since 5519
     200     */
     201    public void removeLayerPropertyChangeListener(LayerStateChangeListener listener) {
     202        layerStateChangeListeners.remove(listener);
     203    }
    167204
    168205    /**
     
    742779
    743780    public final void setUploadDiscouraged(boolean uploadDiscouraged) {
    744         data.setUploadDiscouraged(uploadDiscouraged);
     781        if (uploadDiscouraged ^ isUploadDiscouraged()) {
     782            data.setUploadDiscouraged(uploadDiscouraged);
     783            for (LayerStateChangeListener l : layerStateChangeListeners) {
     784                l.uploadDiscouragedChanged(this, uploadDiscouraged);
     785            }
     786        }
    745787    }
    746788
Note: See TracChangeset for help on using the changeset viewer.