Changeset 5457 in josm


Ignore:
Timestamp:
2012-08-19T02:23:40+02:00 (12 years ago)
Author:
Don-vip
Message:

Restrict WMS "save as" and "load" dialogs to WMS FileFilter + refactor WMS layer import/export to make WMSLayerImporter/Exporter real classes

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

Legend:

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

    r5456 r5457  
    224224            }
    225225            file = new File(fn);
    226         }
    227         if (!confirmOverwrite(file))
    228             return null;
     226            if (!confirmOverwrite(file))
     227                return null;
     228        }
    229229        return file;
    230230    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/MapPaintDialog.java

    r5438 r5457  
    529529            if (fc == null)
    530530                return;
    531 
    532             File file = fc.getSelectedFile();
    533 
    534             if (!SaveActionBase.confirmOverwrite(file))
    535                 return;
    536 
    537             Main.worker.submit(new SaveToFileTask(s, file));
     531            Main.worker.submit(new SaveToFileTask(s, fc.getSelectedFile()));
    538532        }
    539533
  • trunk/src/org/openstreetmap/josm/gui/layer/WMSLayer.java

    r5391 r5457  
    1414import java.awt.image.BufferedImage;
    1515import java.awt.image.ImageObserver;
     16import java.io.Externalizable;
    1617import java.io.File;
    17 import java.io.FileInputStream;
    18 import java.io.FileOutputStream;
    19 import java.io.ObjectInputStream;
    20 import java.io.ObjectOutputStream;
     18import java.io.IOException;
     19import java.io.InvalidClassException;
     20import java.io.ObjectInput;
     21import java.io.ObjectOutput;
    2122import java.util.ArrayList;
    2223import java.util.Collections;
     
    3536import javax.swing.JMenuItem;
    3637import javax.swing.JOptionPane;
    37 import javax.swing.SwingUtilities;
    3838
    3939import org.openstreetmap.gui.jmapviewer.AttributionSupport;
     
    6262import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    6363import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
     64import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    6465import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     66import org.openstreetmap.josm.io.WMSLayerExporter;
     67import org.openstreetmap.josm.io.WMSLayerImporter;
    6568import org.openstreetmap.josm.io.imagery.Grabber;
    6669import org.openstreetmap.josm.io.imagery.HTMLGrabber;
     
    7477 * fetched this way is tiled and managed to the disc to reduce server load.
    7578 */
    76 public class WMSLayer extends ImageryLayer implements ImageObserver, PreferenceChangedListener {
     79public class WMSLayer extends ImageryLayer implements ImageObserver, PreferenceChangedListener, Externalizable {
    7780
    7881    public static class PrecacheTask {
     
    459462            }
    460463        }
    461         cache.setAreaToCache(areaToCache);
     464        if (cache != null) {
     465            cache.setAreaToCache(areaToCache);
     466        }
    462467    }
    463468
     
    611616        try {
    612617
    613             ProjectionBounds b = getBounds(request);
    614             // Checking for exact match is fast enough, no need to do it in separated thread
    615             request.setHasExactMatch(cache.hasExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth));
    616             if (request.isPrecacheOnly() && request.hasExactMatch())
    617                 return; // We already have this tile cached
     618            if (cache != null) {
     619                ProjectionBounds b = getBounds(request);
     620                // Checking for exact match is fast enough, no need to do it in separated thread
     621                request.setHasExactMatch(cache.hasExactMatch(Main.getProjection(), request.getPixelPerDegree(), b.minEast, b.minNorth));
     622                if (request.isPrecacheOnly() && request.hasExactMatch())
     623                    return; // We already have this tile cached
     624            }
    618625
    619626            if (!requestQueue.contains(request) && !finishedRequests.contains(request) && !processingRequests.contains(request)) {
     
    649656
    650657    public class DownloadAction extends AbstractAction {
    651         private static final long serialVersionUID = -7183852461015284020L;
    652658        public DownloadAction() {
    653659            super(tr("Download visible tiles"));
     
    776782        public void actionPerformed(ActionEvent ev) {
    777783            File f = SaveActionBase.createAndOpenSaveFileChooser(
    778                     tr("Save WMS layer"), ".wms");
     784                    tr("Save WMS layer"), WMSLayerImporter.FILE_FILTER);
    779785            try {
    780                 if (f != null) {
    781                     ObjectOutputStream oos = new ObjectOutputStream(
    782                             new FileOutputStream(f)
    783                             );
    784                     oos.writeInt(serializeFormatVersion);
    785                     oos.writeInt(dax);
    786                     oos.writeInt(day);
    787                     oos.writeInt(imageSize);
    788                     oos.writeDouble(info.getPixelPerDegree());
    789                     oos.writeObject(info.getName());
    790                     oos.writeObject(info.getExtendedUrl());
    791                     oos.writeObject(images);
    792                     oos.close();
    793                 }
     786                new WMSLayerExporter().exportData(f, WMSLayer.this);
    794787            } catch (Exception ex) {
    795788                ex.printStackTrace(System.out);
     
    805798        public void actionPerformed(ActionEvent ev) {
    806799            JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true,
    807                     false, tr("Load WMS layer"), "wms");
    808             if(fc == null) return;
     800                    false, tr("Load WMS layer"), WMSLayerImporter.FILE_FILTER, JFileChooser.FILES_ONLY, null);
     801            if (fc == null) return;
    809802            File f = fc.getSelectedFile();
    810803            if (f == null) return;
    811             try
    812             {
    813                 FileInputStream fis = new FileInputStream(f);
    814                 ObjectInputStream ois = new ObjectInputStream(fis);
    815                 int sfv = ois.readInt();
    816                 if (sfv != serializeFormatVersion) {
    817                     JOptionPane.showMessageDialog(Main.parent,
    818                             tr("Unsupported WMS file version; found {0}, expected {1}", sfv, serializeFormatVersion),
    819                             tr("File Format Error"),
    820                             JOptionPane.ERROR_MESSAGE);
    821                     return;
    822                 }
    823                 autoDownloadEnabled = false;
    824                 dax = ois.readInt();
    825                 day = ois.readInt();
    826                 imageSize = ois.readInt();
    827                 info.setPixelPerDegree(ois.readDouble());
    828                 doSetName((String)ois.readObject());
    829                 info.setExtendedUrl((String) ois.readObject());
    830                 images = (GeorefImage[][])ois.readObject();
    831                 ois.close();
    832                 fis.close();
    833                 for (GeorefImage[] imgs : images) {
    834                     for (GeorefImage img : imgs) {
    835                         if (img != null) {
    836                             img.setLayer(WMSLayer.this);
    837                         }
    838                     }
    839                 }
    840                 settingsChanged = true;
    841                 mv.repaint();
    842                 if (cache != null) {
    843                     cache.saveIndex();
    844                     cache = null;
    845                 }
    846                 if(info.getUrl() != null)
    847                 {
    848                     cache = new WmsCache(info.getUrl(), imageSize);
    849                     startGrabberThreads();
    850                 }
    851             }
    852             catch (Exception ex) {
    853                 // FIXME be more specific
    854                 ex.printStackTrace(System.out);
    855                 JOptionPane.showMessageDialog(Main.parent,
    856                         tr("Error loading file"),
    857                         tr("Error"),
    858                         JOptionPane.ERROR_MESSAGE);
     804            try {
     805                new WMSLayerImporter(WMSLayer.this).importData(f, NullProgressMonitor.INSTANCE);
     806            } catch (InvalidClassException ex) {
     807                JOptionPane.showMessageDialog(Main.parent, ex.getMessage(), tr("File Format Error"), JOptionPane.ERROR_MESSAGE);
    859808                return;
    860             }
    861         }
    862     }
     809            } catch (Exception ex) {
     810                ex.printStackTrace();
     811                JOptionPane.showMessageDialog(Main.parent, ex.getMessage(), tr("Error loading file"), JOptionPane.ERROR_MESSAGE);
     812                return;
     813            }
     814        }
     815    }
     816   
    863817    /**
    864818     * This action will add a WMS layer menu entry with the current WMS layer
     
    10521006    }
    10531007
     1008    @Override
     1009    public void writeExternal(ObjectOutput out) throws IOException {
     1010        out.writeInt(serializeFormatVersion);
     1011        out.writeInt(dax);
     1012        out.writeInt(day);
     1013        out.writeInt(imageSize);
     1014        out.writeDouble(info.getPixelPerDegree());
     1015        out.writeObject(info.getName());
     1016        out.writeObject(info.getExtendedUrl());
     1017        out.writeObject(images);
     1018    }
     1019
     1020    @Override
     1021    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
     1022        int sfv = in.readInt();
     1023        if (sfv != serializeFormatVersion) {
     1024            throw new InvalidClassException(tr("Unsupported WMS file version; found {0}, expected {1}", sfv, serializeFormatVersion));
     1025        }
     1026        autoDownloadEnabled = false;
     1027        dax = in.readInt();
     1028        day = in.readInt();
     1029        imageSize = in.readInt();
     1030        info.setPixelPerDegree(in.readDouble());
     1031        doSetName((String)in.readObject());
     1032        info.setExtendedUrl((String)in.readObject());
     1033        images = (GeorefImage[][])in.readObject();
     1034       
     1035        for (GeorefImage[] imgs : images) {
     1036            for (GeorefImage img : imgs) {
     1037                if (img != null) {
     1038                    img.setLayer(WMSLayer.this);
     1039                }
     1040            }
     1041        }
     1042       
     1043        settingsChanged = true;
     1044        mv.repaint();
     1045        if (cache != null) {
     1046            cache.saveIndex();
     1047            cache = null;
     1048        }
     1049        if (info.getUrl() != null) {
     1050            cache = new WmsCache(info.getUrl(), imageSize);
     1051            startGrabberThreads();
     1052        }
     1053    }
    10541054}
  • trunk/src/org/openstreetmap/josm/gui/widgets/JFileChooserManager.java

    r5438 r5457  
    11package org.openstreetmap.josm.gui.widgets;
    2 
    3 import static org.openstreetmap.josm.tools.I18n.tr;
    42
    53import java.awt.Component;
     
    1412import org.openstreetmap.josm.actions.DiskAccessAction;
    1513import org.openstreetmap.josm.actions.ExtensionFileFilter;
    16 import org.openstreetmap.josm.gui.ExtendedDialog;
     14import org.openstreetmap.josm.actions.SaveActionBase;
    1715
    1816/**
     
    206204            if (!open) {
    207205                File file = fc.getSelectedFile();
    208                 if (file != null && file.exists()) {
    209                     ExtendedDialog dialog = new ExtendedDialog(
    210                             Main.parent,
    211                             tr("Overwrite"),
    212                             new String[] {tr("Overwrite"), tr("Cancel")}
    213                     );
    214                     dialog.setContent(tr("File exists. Overwrite?"));
    215                     dialog.setButtonIcons(new String[] {"save_as.png", "cancel.png"});
    216                     dialog.showDialog();
    217                     if (dialog.getValue() != 1) {
    218                         return null;
    219                     }
     206                if (!SaveActionBase.confirmOverwrite(file)) {
     207                    return null;
    220208                }
    221209            }
  • trunk/src/org/openstreetmap/josm/io/WMSLayerExporter.java

    r5361 r5457  
    22package org.openstreetmap.josm.io;
    33
     4import java.io.File;
     5import java.io.FileOutputStream;
     6import java.io.IOException;
     7import java.io.ObjectOutputStream;
     8
     9import org.openstreetmap.josm.gui.layer.Layer;
     10import org.openstreetmap.josm.gui.layer.WMSLayer;
     11import org.openstreetmap.josm.tools.CheckParameterUtil;
     12
     13/**
     14 * Export a WMS layer to a serialized binary file that can be imported later via {@link WMSLayerImporter}.
     15 *
     16 * @since 5457
     17 */
    418public class WMSLayerExporter extends FileExporter {
    519
     20    /**
     21     * Constructs a new {@code WMSLayerExporter}
     22     */
    623    public WMSLayerExporter() {
    724        super(WMSLayerImporter.FILE_FILTER);
    825    }
     26
     27    @Override
     28    public void exportData(File file, Layer layer) throws IOException {
     29        CheckParameterUtil.ensureParameterNotNull(file, "file");
     30        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
     31        if (layer instanceof WMSLayer) {
     32            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
     33            try {
     34                ((WMSLayer)layer).writeExternal(oos);
     35            } finally {
     36                oos.close();
     37            }
     38        }
     39    }
    940}
  • trunk/src/org/openstreetmap/josm/io/WMSLayerImporter.java

    r5361 r5457  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.File;
     7import java.io.FileInputStream;
     8import java.io.IOException;
     9import java.io.ObjectInputStream;
     10
    611import org.openstreetmap.josm.actions.ExtensionFileFilter;
     12import org.openstreetmap.josm.gui.layer.WMSLayer;
     13import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     14import org.openstreetmap.josm.tools.CheckParameterUtil;
    715
     16/**
     17 * Import a WMS layer from a serialized binary file previously exported via {@link WMSLayerExporter}.
     18 * @since 5457
     19 */
    820public class WMSLayerImporter extends FileImporter {
    921
     22    /**
     23     * The file filter used in "open" and "save" dialogs for WMS layers.
     24     */
    1025    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
    1126            "wms", "wms", tr("WMS Files (*.wms)"));
    1227   
     28    private final WMSLayer wmsLayer;
     29
     30    /**
     31     * Constructs a new {@code WMSLayerImporter}.
     32     */
    1333    public WMSLayerImporter() {
     34        this(new WMSLayer());
     35    }
     36
     37    /**
     38     * Constructs a new {@code WMSLayerImporter} that will import data to the specified WMS layer.
     39     * @param wmsLayer The WMS layer.
     40     */
     41    public WMSLayerImporter(WMSLayer wmsLayer) {
    1442        super(FILE_FILTER);
     43        this.wmsLayer = wmsLayer;
     44    }
     45
     46    @Override
     47    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
     48        CheckParameterUtil.ensureParameterNotNull(file, "file");
     49        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
     50        try {
     51            wmsLayer.readExternal(ois);
     52        } catch (ClassNotFoundException e) {
     53            throw new IllegalDataException(e);
     54        } finally {
     55            ois.close();
     56        }
     57    }
     58
     59    /**
     60     * Replies the imported WMS layer.
     61     * @return The imported WMS layer.
     62     * @see #importData(File, ProgressMonitor)
     63     */
     64    public final WMSLayer getWmsLayer() {
     65        return wmsLayer;
    1566    }
    1667}
Note: See TracChangeset for help on using the changeset viewer.