Ignore:
Timestamp:
2012-08-20T01:11:45+02:00 (12 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/gui/layer/WMSLayer.java

    r5457 r5459  
    3333import javax.swing.Action;
    3434import javax.swing.JCheckBoxMenuItem;
    35 import javax.swing.JFileChooser;
    3635import javax.swing.JMenuItem;
    3736import javax.swing.JOptionPane;
     
    3938import org.openstreetmap.gui.jmapviewer.AttributionSupport;
    4039import org.openstreetmap.josm.Main;
    41 import org.openstreetmap.josm.actions.DiskAccessAction;
    4240import org.openstreetmap.josm.actions.SaveActionBase;
    4341import org.openstreetmap.josm.data.Bounds;
     
    6260import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    6361import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
    64 import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    6562import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    66 import org.openstreetmap.josm.io.WMSLayerExporter;
    6763import org.openstreetmap.josm.io.WMSLayerImporter;
    6864import org.openstreetmap.josm.io.imagery.Grabber;
     
    7066import org.openstreetmap.josm.io.imagery.WMSGrabber;
    7167import org.openstreetmap.josm.io.imagery.WMSRequest;
    72 import org.openstreetmap.josm.tools.ImageProvider;
    7368
    7469
     
    124119    protected boolean settingsChanged;
    125120    protected ImageryInfo info;
    126     protected MapView mv;
    127121    public WmsCache cache;
    128122    private AttributionSupport attribution = new AttributionSupport();
     
    178172    @Override
    179173    public void hookUpMapView() {
    180         mv = Main.map.mapView;
    181174        if (info.getUrl() != null) {
    182175            for (WMSLayer layer: Main.map.mapView.getLayersOfType(WMSLayer.class)) {
     
    194187            this.info.setPixelPerDegree(getPPD());
    195188        }
    196         resolution = mv.getDist100PixelText();
     189        resolution = Main.map.mapView.getDist100PixelText();
    197190
    198191        final MouseAdapter adapter = new MouseAdapter() {
     
    484477                SeparatorLayerAction.INSTANCE,
    485478                new OffsetAction(),
    486                 new LoadWmsAction(),
    487                 new SaveWmsAction(),
     479                new LayerSaveAction(this),
     480                new LayerSaveAsAction(this),
    488481                new BookmarkWmsAction(),
    489482                SeparatorLayerAction.INSTANCE,
     
    523516            return -1;
    524517
    525         EastNorth cursorEastNorth = mv.getEastNorth(mv.lastMEvent.getX(), mv.lastMEvent.getY());
     518        MouseEvent lastMEvent = Main.map.mapView.lastMEvent;
     519        EastNorth cursorEastNorth = Main.map.mapView.getEastNorth(lastMEvent.getX(), lastMEvent.getY());
    526520        int mouseX = getImageXIndex(cursorEastNorth.east());
    527521        int mouseY = getImageYIndex(cursorEastNorth.north());
     
    605599            if (request.getState() != null && !request.isPrecacheOnly()) {
    606600                finishedRequests.add(request);
    607                 mv.repaint();
     601                Main.map.mapView.repaint();
    608602            }
    609603        } finally {
     
    669663                        );
    670664            } else {
    671                 downloadAndPaintVisible(mv.getGraphics(), mv, true);
     665                downloadAndPaintVisible(Main.map.mapView.getGraphics(), Main.map.mapView, true);
    672666            }
    673667        }
     
    680674
    681675        private void changeResolution(WMSLayer layer) {
    682             layer.resolution = layer.mv.getDist100PixelText();
     676            layer.resolution = Main.map.mapView.getDist100PixelText();
    683677            layer.info.setPixelPerDegree(layer.getPPD());
    684678            layer.settingsChanged = true;
     
    761755                }
    762756            }
    763             mv.repaint();
     757            Main.map.mapView.repaint();
    764758        }
    765759        @Override
     
    775769    }
    776770
    777     public class SaveWmsAction extends AbstractAction {
    778         public SaveWmsAction() {
    779             super(tr("Save WMS layer to file"), ImageProvider.get("save"));
    780         }
    781         @Override
    782         public void actionPerformed(ActionEvent ev) {
    783             File f = SaveActionBase.createAndOpenSaveFileChooser(
    784                     tr("Save WMS layer"), WMSLayerImporter.FILE_FILTER);
    785             try {
    786                 new WMSLayerExporter().exportData(f, WMSLayer.this);
    787             } catch (Exception ex) {
    788                 ex.printStackTrace(System.out);
    789             }
    790         }
    791     }
    792 
    793     public class LoadWmsAction extends AbstractAction {
    794         public LoadWmsAction() {
    795             super(tr("Load WMS layer from file"), ImageProvider.get("open"));
    796         }
    797         @Override
    798         public void actionPerformed(ActionEvent ev) {
    799             JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(true,
    800                     false, tr("Load WMS layer"), WMSLayerImporter.FILE_FILTER, JFileChooser.FILES_ONLY, null);
    801             if (fc == null) return;
    802             File f = fc.getSelectedFile();
    803             if (f == null) return;
    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);
    808                 return;
    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    
    817771    /**
    818772     * This action will add a WMS layer menu entry with the current WMS layer
     
    860814                    }
    861815                }
    862                 mv.repaint();
     816                Main.map.mapView.repaint();
    863817            }
    864818        }
     
    952906    }
    953907
    954     protected Grabber getGrabber(boolean localOnly){
    955         if(getInfo().getImageryType() == ImageryType.HTML)
    956             return new HTMLGrabber(mv, this, localOnly);
    957         else if(getInfo().getImageryType() == ImageryType.WMS)
    958             return new WMSGrabber(mv, this, localOnly);
     908    protected Grabber getGrabber(boolean localOnly) {
     909        if (getInfo().getImageryType() == ImageryType.HTML)
     910            return new HTMLGrabber(Main.map.mapView, this, localOnly);
     911        else if (getInfo().getImageryType() == ImageryType.WMS)
     912            return new WMSGrabber(Main.map.mapView, this, localOnly);
    959913        else throw new IllegalStateException("getGrabber() called for non-WMS layer type");
    960914    }
     
    1042996       
    1043997        settingsChanged = true;
    1044         mv.repaint();
     998        if (Main.isDisplayingMapView()) {
     999            Main.map.mapView.repaint();
     1000        }
    10451001        if (cache != null) {
    10461002            cache.saveIndex();
    10471003            cache = null;
    10481004        }
     1005    }
     1006
     1007    @Override
     1008    public void onPostLoadFromFile() {
    10491009        if (info.getUrl() != null) {
    10501010            cache = new WmsCache(info.getUrl(), imageSize);
     
    10521012        }
    10531013    }
     1014
     1015    @Override
     1016    public boolean isSavable() {
     1017        return true; // With WMSLayerExporter
     1018    }
     1019
     1020    @Override
     1021    public File createAndOpenSaveFileChooser() {
     1022        return SaveActionBase.createAndOpenSaveFileChooser(tr("Save WMS file"), WMSLayerImporter.FILE_FILTER);
     1023    }
    10541024}
Note: See TracChangeset for help on using the changeset viewer.