Ticket #3294: josm_patch_3294-fix1.diff

File josm_patch_3294-fix1.diff, 6.5 KB (added by alexm, 17 years ago)

Updated Patch

  • WMSLayer.java

     
    3434import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    3535import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
    3636import org.openstreetmap.josm.gui.layer.Layer;
     37import org.openstreetmap.josm.io.CacheFiles;
    3738import org.openstreetmap.josm.tools.ImageProvider;
    3839
    3940/**
    4041 * This is a layer that grabs the current screen from an WMS server. The data
    41  * fetched this way is tiled and managerd to the disc to reduce server load.
     42 * fetched this way is tiled and managed to the disc to reduce server load.
    4243 */
     44@SuppressWarnings("serial")
    4345public class WMSLayer extends Layer {
    4446        protected static final Icon icon =
    4547                new ImageIcon(Toolkit.getDefaultToolkit().createImage(WMSPlugin.class.getResource("/images/wms_small.png")));
     
    7072                mv = Main.map.mapView;
    7173        }
    7274
    73         public WMSLayer(String name, String baseURL, String cookies) {
    74                 super(name);
    75                 alphaChannel.setSelected(Main.pref.getBoolean("wmsplugin.alpha_channel"));
    76                 background = true; /* set global background variable */
    77                 initializeImages();
    78                 this.baseURL = baseURL;
    79                 this.cookies = cookies;
    80                 WMSGrabber.getProjection(baseURL, true);
    81                 mv = Main.map.mapView;
    82                 resolution = mv.getDist100PixelText();
    83                 pixelPerDegree = getPPD();
     75    public WMSLayer(String name, String baseURL, String cookies) {
     76        super(name);
     77        alphaChannel.setSelected(Main.pref.getBoolean("wmsplugin.alpha_channel"));
     78        background = true; /* set global background variable */
     79        initializeImages();
     80        this.baseURL = baseURL;
     81        this.cookies = cookies;
     82        WMSGrabber.getProjection(baseURL, true);
     83        mv = Main.map.mapView;
     84       
     85        // quick hack to predefine the PixelDensity to reuse the cache
     86        int codeIndex = getName().indexOf("#PPD=");
     87        if (codeIndex != -1) {
     88                pixelPerDegree = Double.valueOf(getName().substring(codeIndex+5));
     89        } else {
     90                pixelPerDegree = getPPD();
     91        }
    8492
     93        resolution = "PixelPerDegree: " + Double.toString(pixelPerDegree) + " (add #PPD= to layer name to restore)";
    8594                executor = Executors.newFixedThreadPool(3);
    8695        }
    8796
    88         @Override
    89         public void destroy() {
    90                 try {
    91                         executor.shutdown();
    92                         // Might not be initalized, so catch NullPointer as well
    93                 } catch(Exception x) {}
    94         }
     97    @Override
     98    public void destroy() {
     99        try {
     100            executor.shutdown();
     101            // Might not be initialized, so catch NullPointer as well
     102        } catch(Exception x) {}
     103    }
    95104
    96         public double getPPD(){
    97                 ProjectionBounds bounds = mv.getProjectionBounds();
    98                 return mv.getWidth() / (bounds.max.east() - bounds.min.east());
    99         }
     105    public double getPPD(){
     106        ProjectionBounds bounds = mv.getProjectionBounds();
     107        return mv.getWidth() / (bounds.max.east() - bounds.min.east());
     108    }
    100109
    101110        public void initializeImages() {
    102111                images = new GeorefImage[dax][day];
     
    200209                return getToolTipText();
    201210        }
    202211
    203         @Override public Component[] getMenuEntries() {
    204                 return new Component[]{
    205                                 new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)),
    206                                 new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)),
    207                                 new JSeparator(),
    208                                 new JMenuItem(new LoadWmsAction()),
    209                                 new JMenuItem(new SaveWmsAction()),
    210                                 new JSeparator(),
    211                                 startstop,
    212                                 alphaChannel,
    213                                 new JMenuItem(new changeResolutionAction()),
    214                                 new JMenuItem(new reloadErrorTilesAction()),
    215                                 new JMenuItem(new downloadAction()),
    216                                 new JSeparator(),
    217                                 new JMenuItem(new LayerListPopup.InfoAction(this))
    218                 };
    219         }
     212    @Override public Component[] getMenuEntries() {
     213        return new Component[]{
     214                new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)),
     215                new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)),
     216                new JSeparator(),
     217                new JMenuItem(new LoadWmsAction()),
     218                new JMenuItem(new SaveWmsAction()),
     219                new JMenuItem(new BookmarkWmsAction()),
     220                new JSeparator(),
     221                startstop,
     222                alphaChannel,
     223                new JMenuItem(new changeResolutionAction()),
     224                new JMenuItem(new reloadErrorTilesAction()),
     225                new JMenuItem(new downloadAction()),
     226                new JSeparator(),
     227                new JMenuItem(new LayerListPopup.InfoAction(this))
     228        };
     229    }
    220230
    221231        public GeorefImage findImage(EastNorth eastNorth) {
    222232                for(int x = 0; x<dax; ++x) {
     
    250260        }
    251261
    252262        public class reloadErrorTilesAction extends AbstractAction {
    253                 public reloadErrorTilesAction() {
    254                         super(tr("Reload erroneous tiles"));
    255                 }
    256                 public void actionPerformed(ActionEvent ev) {
    257                         // Delete small files, because they're probably blank tiles.
    258                         // See https://josm.openstreetmap.de/ticket/2307
    259                         WMSPlugin.cache.customCleanUp(WMSPlugin.cache.CLEAN_SMALL_FILES, 2048);
     263        public reloadErrorTilesAction() {
     264            super(tr("Reload erroneous tiles"));
     265        }
     266        public void actionPerformed(ActionEvent ev) {
     267            // Delete small files, because they're probably blank tiles.
     268            // See https://josm.openstreetmap.de/ticket/2307
     269            WMSPlugin.cache.customCleanUp(CacheFiles.CLEAN_SMALL_FILES, 4096);
    260270
    261271                        for (int x = 0; x < dax; ++x) {
    262272                                for (int y = 0; y < day; ++y) {
     
    364374                        }
    365375                }
    366376        }
     377   
     378    /**
     379     * This action will add a WMS layer menu entry with the current WMS layer URL and name extended by the current resolution.
     380     * When using the menu entry again, the WMS cache will be used properly.
     381     *
     382     * @author <alex@addismap.com>
     383     */
     384        public class BookmarkWmsAction extends AbstractAction {
     385        public BookmarkWmsAction() {
     386            super(tr("Set WMS Bookmark"));
     387        }
     388        public void actionPerformed(ActionEvent ev) {
     389                int i = 0;
     390                while (Main.pref.hasKey("wmsplugin.url."+i+".url")) {
     391                        i++;
     392                }
     393                String baseName;
     394                // cut old parameter
     395                int parameterIndex = getName().indexOf("#PPD=");
     396                if (parameterIndex != -1) {
     397                        baseName = getName().substring(0,parameterIndex);
     398                }
     399                else {
     400                        baseName = getName();
     401                }
     402                Main.pref.put("wmsplugin.url."+ i +".url",baseURL );
     403                Main.pref.put("wmsplugin.url."+String.valueOf(i)+".name", baseName + "#" + getPPD() );
     404                WMSPlugin.refreshMenu();
     405        }
     406    }
     407
    367408}