Index: trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java	(revision 5456)
+++ trunk/src/org/openstreetmap/josm/actions/SaveActionBase.java	(revision 5457)
@@ -224,7 +224,7 @@
             }
             file = new File(fn);
-        }
-        if (!confirmOverwrite(file))
-            return null;
+            if (!confirmOverwrite(file))
+                return null;
+        }
         return file;
     }
Index: trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java	(revision 5456)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java	(revision 5457)
@@ -529,11 +529,5 @@
             if (fc == null)
                 return;
-
-            File file = fc.getSelectedFile();
-
-            if (!SaveActionBase.confirmOverwrite(file))
-                return;
-
-            Main.worker.submit(new SaveToFileTask(s, file));
+            Main.worker.submit(new SaveToFileTask(s, fc.getSelectedFile()));
         }
 
Index: trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java	(revision 5456)
+++ trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java	(revision 5457)
@@ -14,9 +14,10 @@
 import java.awt.image.BufferedImage;
 import java.awt.image.ImageObserver;
+import java.io.Externalizable;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
+import java.io.IOException;
+import java.io.InvalidClassException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -35,5 +36,4 @@
 import javax.swing.JMenuItem;
 import javax.swing.JOptionPane;
-import javax.swing.SwingUtilities;
 
 import org.openstreetmap.gui.jmapviewer.AttributionSupport;
@@ -62,5 +62,8 @@
 import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
 import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
+import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.io.WMSLayerExporter;
+import org.openstreetmap.josm.io.WMSLayerImporter;
 import org.openstreetmap.josm.io.imagery.Grabber;
 import org.openstreetmap.josm.io.imagery.HTMLGrabber;
@@ -74,5 +77,5 @@
  * fetched this way is tiled and managed to the disc to reduce server load.
  */
-public class WMSLayer extends ImageryLayer implements ImageObserver, PreferenceChangedListener {
+public class WMSLayer extends ImageryLayer implements ImageObserver, PreferenceChangedListener, Externalizable {
 
     public static class PrecacheTask {
@@ -459,5 +462,7 @@
             }
         }
-        cache.setAreaToCache(areaToCache);
+        if (cache != null) {
+            cache.setAreaToCache(areaToCache);
+        }
     }
 
@@ -611,9 +616,11 @@
         try {
 
-            ProjectionBounds b = getBounds(request);
-            // Checking for exact match is fast enough, no need to do it in separated thread
-            request.setHasExactMatch(cache.hasExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth));
-            if (request.isPrecacheOnly() && request.hasExactMatch())
-                return; // We already have this tile cached
+            if (cache != null) {
+                ProjectionBounds b = getBounds(request);
+                // Checking for exact match is fast enough, no need to do it in separated thread
+                request.setHasExactMatch(cache.hasExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth));
+                if (request.isPrecacheOnly() && request.hasExactMatch())
+                    return; // We already have this tile cached
+            }
 
             if (!requestQueue.contains(request) && !finishedRequests.contains(request) && !processingRequests.contains(request)) {
@@ -649,5 +656,4 @@
 
     public class DownloadAction extends AbstractAction {
-        private static final long serialVersionUID = -7183852461015284020L;
         public DownloadAction() {
             super(tr("Download visible tiles"));
@@ -776,20 +782,7 @@
         public void actionPerformed(ActionEvent ev) {
             File f = SaveActionBase.createAndOpenSaveFileChooser(
-                    tr("Save WMS layer"), ".wms");
+                    tr("Save WMS layer"), WMSLayerImporter.FILE_FILTER);
             try {
-                if (f != null) {
-                    ObjectOutputStream oos = new ObjectOutputStream(
-                            new FileOutputStream(f)
-                            );
-                    oos.writeInt(serializeFormatVersion);
-                    oos.writeInt(dax);
-                    oos.writeInt(day);
-                    oos.writeInt(imageSize);
-                    oos.writeDouble(info.getPixelPerDegree());
-                    oos.writeObject(info.getName());
-                    oos.writeObject(info.getExtendedUrl());
-                    oos.writeObject(images);
-                    oos.close();
-                }
+                new WMSLayerExporter().exportData(f, WMSLayer.this);
             } catch (Exception ex) {
                 ex.printStackTrace(System.out);
@@ -805,60 +798,21 @@
         public void actionPerformed(ActionEvent ev) {
             JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true,
-                    false, tr("Load WMS layer"), "wms");
-            if(fc == null) return;
+                    false, tr("Load WMS layer"), WMSLayerImporter.FILE_FILTER, JFileChooser.FILES_ONLY, null);
+            if (fc == null) return;
             File f = fc.getSelectedFile();
             if (f == null) return;
-            try
-            {
-                FileInputStream fis = new FileInputStream(f);
-                ObjectInputStream ois = new ObjectInputStream(fis);
-                int sfv = ois.readInt();
-                if (sfv != serializeFormatVersion) {
-                    JOptionPane.showMessageDialog(Main.parent,
-                            tr("Unsupported WMS file version; found {0}, expected {1}", sfv, serializeFormatVersion),
-                            tr("File Format Error"),
-                            JOptionPane.ERROR_MESSAGE);
-                    return;
-                }
-                autoDownloadEnabled = false;
-                dax = ois.readInt();
-                day = ois.readInt();
-                imageSize = ois.readInt();
-                info.setPixelPerDegree(ois.readDouble());
-                doSetName((String)ois.readObject());
-                info.setExtendedUrl((String) ois.readObject());
-                images = (GeorefImage[][])ois.readObject();
-                ois.close();
-                fis.close();
-                for (GeorefImage[] imgs : images) {
-                    for (GeorefImage img : imgs) {
-                        if (img != null) {
-                            img.setLayer(WMSLayer.this);
-                        }
-                    }
-                }
-                settingsChanged = true;
-                mv.repaint();
-                if (cache != null) {
-                    cache.saveIndex();
-                    cache = null;
-                }
-                if(info.getUrl() != null)
-                {
-                    cache = new WmsCache(info.getUrl(), imageSize);
-                    startGrabberThreads();
-                }
-            }
-            catch (Exception ex) {
-                // FIXME be more specific
-                ex.printStackTrace(System.out);
-                JOptionPane.showMessageDialog(Main.parent,
-                        tr("Error loading file"),
-                        tr("Error"),
-                        JOptionPane.ERROR_MESSAGE);
+            try {
+                new WMSLayerImporter(WMSLayer.this).importData(f, NullProgressMonitor.INSTANCE);
+            } catch (InvalidClassException ex) {
+                JOptionPane.showMessageDialog(Main.parent, ex.getMessage(), tr("File Format Error"), JOptionPane.ERROR_MESSAGE);
                 return;
-            }
-        }
-    }
+            } catch (Exception ex) {
+                ex.printStackTrace();
+                JOptionPane.showMessageDialog(Main.parent, ex.getMessage(), tr("Error loading file"), JOptionPane.ERROR_MESSAGE);
+                return;
+            }
+        }
+    }
+    
     /**
      * This action will add a WMS layer menu entry with the current WMS layer
@@ -1052,3 +1006,49 @@
     }
 
+    @Override
+    public void writeExternal(ObjectOutput out) throws IOException {
+        out.writeInt(serializeFormatVersion);
+        out.writeInt(dax);
+        out.writeInt(day);
+        out.writeInt(imageSize);
+        out.writeDouble(info.getPixelPerDegree());
+        out.writeObject(info.getName());
+        out.writeObject(info.getExtendedUrl());
+        out.writeObject(images);
+    }
+
+    @Override
+    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+        int sfv = in.readInt();
+        if (sfv != serializeFormatVersion) {
+            throw new InvalidClassException(tr("Unsupported WMS file version; found {0}, expected {1}", sfv, serializeFormatVersion));
+        }
+        autoDownloadEnabled = false;
+        dax = in.readInt();
+        day = in.readInt();
+        imageSize = in.readInt();
+        info.setPixelPerDegree(in.readDouble());
+        doSetName((String)in.readObject());
+        info.setExtendedUrl((String)in.readObject());
+        images = (GeorefImage[][])in.readObject();
+        
+        for (GeorefImage[] imgs : images) {
+            for (GeorefImage img : imgs) {
+                if (img != null) {
+                    img.setLayer(WMSLayer.this);
+                }
+            }
+        }
+        
+        settingsChanged = true;
+        mv.repaint();
+        if (cache != null) {
+            cache.saveIndex();
+            cache = null;
+        }
+        if (info.getUrl() != null) {
+            cache = new WmsCache(info.getUrl(), imageSize);
+            startGrabberThreads();
+        }
+    }
 }
Index: trunk/src/org/openstreetmap/josm/gui/widgets/JFileChooserManager.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/widgets/JFileChooserManager.java	(revision 5456)
+++ trunk/src/org/openstreetmap/josm/gui/widgets/JFileChooserManager.java	(revision 5457)
@@ -1,5 +1,3 @@
 package org.openstreetmap.josm.gui.widgets;
-
-import static org.openstreetmap.josm.tools.I18n.tr;
 
 import java.awt.Component;
@@ -14,5 +12,5 @@
 import org.openstreetmap.josm.actions.DiskAccessAction;
 import org.openstreetmap.josm.actions.ExtensionFileFilter;
-import org.openstreetmap.josm.gui.ExtendedDialog;
+import org.openstreetmap.josm.actions.SaveActionBase;
 
 /**
@@ -206,16 +204,6 @@
             if (!open) {
                 File file = fc.getSelectedFile();
-                if (file != null && file.exists()) {
-                    ExtendedDialog dialog = new ExtendedDialog(
-                            Main.parent,
-                            tr("Overwrite"),
-                            new String[] {tr("Overwrite"), tr("Cancel")}
-                    );
-                    dialog.setContent(tr("File exists. Overwrite?"));
-                    dialog.setButtonIcons(new String[] {"save_as.png", "cancel.png"});
-                    dialog.showDialog();
-                    if (dialog.getValue() != 1) {
-                        return null;
-                    }
+                if (!SaveActionBase.confirmOverwrite(file)) {
+                    return null;
                 }
             }
Index: trunk/src/org/openstreetmap/josm/io/WMSLayerExporter.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/WMSLayerExporter.java	(revision 5456)
+++ trunk/src/org/openstreetmap/josm/io/WMSLayerExporter.java	(revision 5457)
@@ -2,8 +2,39 @@
 package org.openstreetmap.josm.io;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.ObjectOutputStream;
+
+import org.openstreetmap.josm.gui.layer.Layer;
+import org.openstreetmap.josm.gui.layer.WMSLayer;
+import org.openstreetmap.josm.tools.CheckParameterUtil;
+
+/**
+ * Export a WMS layer to a serialized binary file that can be imported later via {@link WMSLayerImporter}. 
+ * 
+ * @since 5457
+ */
 public class WMSLayerExporter extends FileExporter {
 
+    /**
+     * Constructs a new {@code WMSLayerExporter}
+     */
     public WMSLayerExporter() {
         super(WMSLayerImporter.FILE_FILTER);
     }
+
+    @Override
+    public void exportData(File file, Layer layer) throws IOException {
+        CheckParameterUtil.ensureParameterNotNull(file, "file");
+        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
+        if (layer instanceof WMSLayer) {
+            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
+            try {
+                ((WMSLayer)layer).writeExternal(oos);
+            } finally {
+                oos.close();
+            }
+        }
+    }
 }
Index: trunk/src/org/openstreetmap/josm/io/WMSLayerImporter.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/WMSLayerImporter.java	(revision 5456)
+++ trunk/src/org/openstreetmap/josm/io/WMSLayerImporter.java	(revision 5457)
@@ -4,13 +4,64 @@
 import static org.openstreetmap.josm.tools.I18n.tr;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+
 import org.openstreetmap.josm.actions.ExtensionFileFilter;
+import org.openstreetmap.josm.gui.layer.WMSLayer;
+import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.tools.CheckParameterUtil;
 
+/**
+ * Import a WMS layer from a serialized binary file previously exported via {@link WMSLayerExporter}.
+ * @since 5457
+ */
 public class WMSLayerImporter extends FileImporter {
 
+    /**
+     * The file filter used in "open" and "save" dialogs for WMS layers.
+     */
     public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
             "wms", "wms", tr("WMS Files (*.wms)"));
     
+    private final WMSLayer wmsLayer;
+
+    /**
+     * Constructs a new {@code WMSLayerImporter}.
+     */
     public WMSLayerImporter() {
+        this(new WMSLayer());
+    }
+
+    /**
+     * Constructs a new {@code WMSLayerImporter} that will import data to the specified WMS layer.
+     * @param wmsLayer The WMS layer.
+     */
+    public WMSLayerImporter(WMSLayer wmsLayer) {
         super(FILE_FILTER);
+        this.wmsLayer = wmsLayer;
+    }
+
+    @Override
+    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
+        CheckParameterUtil.ensureParameterNotNull(file, "file");
+        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
+        try {
+            wmsLayer.readExternal(ois);
+        } catch (ClassNotFoundException e) {
+            throw new IllegalDataException(e);
+        } finally {
+            ois.close();
+        }
+    }
+
+    /**
+     * Replies the imported WMS layer.
+     * @return The imported WMS layer.
+     * @see #importData(File, ProgressMonitor)
+     */
+    public final WMSLayer getWmsLayer() {
+        return wmsLayer;
     }
 }
