Ignore:
Timestamp:
2012-08-20T01:11:45+02:00 (14 years ago)
Author:
Don-vip
Message:

fix #2961 - Improve usability of WMS Layer Saving/Loading

  • Replaced the unconventional method of creating a blank layer, then loading a .wms file to a standard File->Open approach
  • Fixed memory leaks with some actions registered as listeners but never destroyed
  • Layer interface modified to allow a generic approach of layer saving in SaveActionBase rather than the previous one restricted to OSM and GPX data
  • FileImporters and FileExporters can now be enabled/disabled at runtime, for example when the active layer changes
File:
1 edited

Legend:

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

    r5457 r5459  
    1616
    1717import org.openstreetmap.josm.Main;
    18 import org.openstreetmap.josm.data.conflict.ConflictCollection;
    19 import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2018import org.openstreetmap.josm.gui.ExtendedDialog;
    21 import org.openstreetmap.josm.gui.layer.GpxLayer;
    2219import org.openstreetmap.josm.gui.layer.Layer;
    2320import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    2421import org.openstreetmap.josm.io.FileExporter;
    25 import org.openstreetmap.josm.io.GpxImporter;
    2622import org.openstreetmap.josm.tools.Shortcut;
    2723
     
    4440
    4541    public boolean doSave() {
    46         Layer layer = null;
    47         if (Main.isDisplayingMapView() && (Main.map.mapView.getActiveLayer() instanceof OsmDataLayer
    48                 || Main.map.mapView.getActiveLayer() instanceof GpxLayer)) {
    49             layer = Main.map.mapView.getActiveLayer();
     42        if (Main.isDisplayingMapView()) {
     43            Layer layer = Main.map.mapView.getActiveLayer();
     44            if (layer != null && layer.isSavable()) {
     45                return doSave(layer);
     46            }
    5047        }
    51         if (layer == null)
    52             return false;
    53         return doSave(layer);
     48        return false;
    5449    }
    5550
    5651    public boolean doSave(Layer layer) {
    57         if(!checkSaveConditions(layer))
     52        if(!layer.checkSaveConditions())
    5853            return false;
    5954        file = getFile(layer);
     
    6257
    6358    public static boolean doSave(Layer layer, File file) {
    64         if(!checkSaveConditions(layer))
     59        if(!layer.checkSaveConditions())
    6560            return false;
    6661        return doInternalSave(layer, file);
     
    10196
    10297    /**
    103      * Checks whether it is ok to launch a save (whether we have data,
    104      * there is no conflict etc.)
    105      * @return <code>true</code>, if it is safe to save.
    106      */
    107     public static boolean checkSaveConditions(Layer layer) {
    108         if (layer instanceof GpxLayer)
    109             return ((GpxLayer)layer).data != null;
    110         else if (layer instanceof OsmDataLayer)  {
    111             if (isDataSetEmpty((OsmDataLayer)layer)) {
    112                 ExtendedDialog dialog = new ExtendedDialog(
    113                         Main.parent,
    114                         tr("Empty document"),
    115                         new String[] {tr("Save anyway"), tr("Cancel")}
    116                 );
    117                 dialog.setContent(tr("The document contains no data."));
    118                 dialog.setButtonIcons(new String[] {"save.png", "cancel.png"});
    119                 dialog.showDialog();
    120                 if (dialog.getValue() != 1) return false;
    121             }
    122 
    123             ConflictCollection conflicts = ((OsmDataLayer)layer).getConflicts();
    124             if (conflicts != null && !conflicts.isEmpty()) {
    125                 ExtendedDialog dialog = new ExtendedDialog(
    126                         Main.parent,
    127                         /* I18N: Display title of the window showing conflicts */
    128                         tr("Conflicts"),
    129                         new String[] {tr("Reject Conflicts and Save"), tr("Cancel")}
    130                 );
    131                 dialog.setContent(tr("There are unresolved conflicts. Conflicts will not be saved and handled as if you rejected all. Continue?"));
    132                 dialog.setButtonIcons(new String[] {"save.png", "cancel.png"});
    133                 dialog.showDialog();
    134                 if (dialog.getValue() != 1) return false;
    135             }
    136             return true;
    137         }
    138         return false;
    139     }
    140 
    141     public static File openFileDialog(Layer layer) {
    142         if (layer instanceof OsmDataLayer)
    143             return createAndOpenSaveFileChooser(tr("Save OSM file"), "osm");
    144         else if (layer instanceof GpxLayer)
    145             return createAndOpenSaveFileChooser(tr("Save GPX file"), GpxImporter.FILE_FILTER);
    146         return createAndOpenSaveFileChooser(tr("Save Layer"), "lay");
    147     }
    148 
    149     /**
    150      * Check the data set if it would be empty on save. It is empty, if it contains
    151      * no objects (after all objects that are created and deleted without being
    152      * transferred to the server have been removed).
    153      *
    154      * @return <code>true</code>, if a save result in an empty data set.
    155      */
    156     private static boolean isDataSetEmpty(OsmDataLayer layer) {
    157         for (OsmPrimitive osm : layer.data.allNonDeletedPrimitives())
    158             if (!osm.isDeleted() || !osm.isNewOrUndeleted())
    159                 return false;
    160         return true;
    161     }
    162 
    163     /**
    16498     * Refreshes the enabled state
    16599     *
     
    179113        }
    180114        Layer layer = Main.map.mapView.getActiveLayer();
    181         setEnabled(layer instanceof OsmDataLayer || layer instanceof GpxLayer);
     115        setEnabled(layer != null && layer.isSavable());
    182116    }
    183117
Note: See TracChangeset for help on using the changeset viewer.