Changeset 14632 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2019-01-03T20:46:14+01:00 (5 years ago)
Author:
simon04
Message:

fix #15817 - Imagery Preferences: collect all cache settings in the same tab

Location:
trunk/src/org/openstreetmap/josm/gui/preferences/imagery
Files:
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CacheSettingsPanel.java

    r14631 r14632  
    66import java.awt.GridBagLayout;
    77import java.awt.event.ActionEvent;
     8import java.io.File;
    89import java.util.ArrayList;
    910import java.util.Comparator;
     
    1920import javax.swing.JPanel;
    2021import javax.swing.JScrollPane;
     22import javax.swing.JSpinner;
    2123import javax.swing.JTable;
     24import javax.swing.SpinnerNumberModel;
    2225import javax.swing.table.DefaultTableModel;
    2326import javax.swing.table.TableColumn;
     
    2932import org.apache.commons.jcs.engine.stats.behavior.IStats;
    3033import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
     34import org.openstreetmap.josm.data.cache.JCSCacheManager;
     35import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
    3136import org.openstreetmap.josm.gui.MainApplication;
     37import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
    3238import org.openstreetmap.josm.gui.layer.TMSLayer;
    3339import org.openstreetmap.josm.gui.layer.WMSLayer;
     
    3541import org.openstreetmap.josm.gui.util.GuiHelper;
    3642import org.openstreetmap.josm.gui.widgets.ButtonColumn;
     43import org.openstreetmap.josm.gui.widgets.JosmTextField;
    3744import org.openstreetmap.josm.tools.GBC;
    3845import org.openstreetmap.josm.tools.Logging;
     
    4148
    4249/**
    43  * Panel for cache content management.
     50 * Panel for cache size, location and content management.
    4451 *
    4552 * @author Wiktor Niesiobędzki
    4653 *
    4754 */
    48 public class CacheContentsPanel extends JPanel {
     55public class CacheSettingsPanel extends JPanel {
     56
     57    private final JosmTextField cacheDir = new JosmTextField(11);
     58    private final JSpinner maxElementsOnDisk = new JSpinner(new SpinnerNumberModel(
     59            AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().intValue(), 0, Integer.MAX_VALUE, 1));
    4960
    5061    /**
    5162     * Creates cache content panel
    5263     */
    53     public CacheContentsPanel() {
     64    public CacheSettingsPanel() {
    5465        super(new GridBagLayout());
     66
     67        add(new JLabel(tr("Tile cache directory: ")), GBC.std());
     68        add(GBC.glue(5, 0), GBC.std());
     69        add(cacheDir, GBC.eol().fill(GBC.HORIZONTAL));
     70
     71        add(new JLabel(tr("Maximum size of disk cache (per imagery) in MB: ")), GBC.std());
     72        add(GBC.glue(5, 0), GBC.std());
     73        add(maxElementsOnDisk, GBC.eop());
     74
    5575        MainApplication.worker.submit(() -> {
    5676            addToPanel(TMSLayer.getCache(), "TMS");
     
    152172        };
    153173    }
     174
     175    /**
     176     * Loads the common settings.
     177     */
     178    void loadSettings() {
     179        this.cacheDir.setText(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
     180        this.maxElementsOnDisk.setValue(AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get());
     181    }
     182
     183    /**
     184     * Saves the common settings.
     185     * @return true when restart is required
     186     */
     187    boolean saveSettings() {
     188        boolean restartRequired = removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(),
     189                1024L * 1024L * ((Integer) this.maxElementsOnDisk.getValue()));
     190
     191        if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {
     192            AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());
     193            restartRequired = true;
     194        }
     195
     196
     197        if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.cacheDir.getText())) {
     198            restartRequired = true;
     199            removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 0); // clear old cache directory
     200            CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.cacheDir.getText());
     201        }
     202
     203        return restartRequired;
     204    }
     205
     206    private static boolean removeCacheFiles(String path, long maxSize) {
     207        File directory = new File(path);
     208        File[] cacheFiles = directory.listFiles((dir, name) -> name.endsWith(".data") || name.endsWith(".key"));
     209        boolean restartRequired = false;
     210        if (cacheFiles != null) {
     211            for (File cacheFile: cacheFiles) {
     212                if (cacheFile.length() > maxSize) {
     213                    if (!restartRequired) {
     214                        JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
     215                        restartRequired = true;
     216                    }
     217                    Utils.deleteFile(cacheFile);
     218                    File otherFile = null;
     219                    if (cacheFile.getName().endsWith(".data")) {
     220                        otherFile = new File(cacheFile.getPath().replaceAll("\\.data$", ".key"));
     221                    } else if (cacheFile.getName().endsWith(".key")) {
     222                        otherFile = new File(cacheFile.getPath().replaceAll("\\.key$", ".data"));
     223                    }
     224                    if (otherFile != null) {
     225                        Utils.deleteFileIfExists(otherFile);
     226                    }
     227                }
     228            }
     229        }
     230        return restartRequired;
     231    }
    154232}
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CommonSettingsPanel.java

    r14327 r14632  
    55
    66import java.awt.GridBagLayout;
    7 import java.io.File;
    8 import java.io.FilenameFilter;
    97
    108import javax.swing.JLabel;
    119import javax.swing.JPanel;
    1210import javax.swing.JSlider;
    13 import javax.swing.JSpinner;
    14 import javax.swing.SpinnerNumberModel;
    1511
    16 import org.openstreetmap.josm.data.cache.JCSCacheManager;
    17 import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
    18 import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
    1912import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    2013import org.openstreetmap.josm.gui.layer.ImageryLayer;
    2114import org.openstreetmap.josm.gui.widgets.JosmComboBox;
    22 import org.openstreetmap.josm.gui.widgets.JosmTextField;
    2315import org.openstreetmap.josm.tools.GBC;
    2416import org.openstreetmap.josm.tools.Utils;
     
    3224    // Common Settings
    3325    private final JosmComboBox<String> sharpen;
    34     private final JosmTextField tilecacheDir = new JosmTextField(11);
    35     private final JSpinner maxElementsOnDisk;
    3626    private final JSlider tilesZoom = new JSlider(-2, 2, 0);
    3727
     
    4333        super(new GridBagLayout());
    4434
    45         this.maxElementsOnDisk = new JSpinner(new SpinnerNumberModel(
    46                 AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().intValue(), 0, Integer.MAX_VALUE, 1));
    47 
    4835        this.sharpen = new JosmComboBox<>(new String[] {
    4936                tr("None"),
     
    5340        add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
    5441        add(this.sharpen, GBC.eol().fill(GBC.HORIZONTAL));
    55 
    56         add(new JLabel(tr("Tile cache directory: ")), GBC.std());
    57         add(GBC.glue(5, 0), GBC.std());
    58         add(tilecacheDir, GBC.eol().fill(GBC.HORIZONTAL));
    59 
    60         add(new JLabel(tr("Maximum size of disk cache (per imagery) in MB: ")), GBC.std());
    61         add(GBC.glue(5, 0), GBC.std());
    62         add(this.maxElementsOnDisk, GBC.eol());
    6342
    6443        this.tilesZoom.setPaintLabels(true);
     
    7655    public void loadSettings() {
    7756        this.sharpen.setSelectedIndex(Utils.clamp(ImageryLayer.PROP_SHARPEN_LEVEL.get(), 0, 2));
    78         this.tilecacheDir.setText(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
    79         this.maxElementsOnDisk.setValue(AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get());
    8057        this.tilesZoom.setValue(AbstractTileSourceLayer.ZOOM_OFFSET.get());
    8158    }
     
    8966
    9067        boolean restartRequired = false;
    91         restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(),
    92                 1024L * 1024L * ((Integer) this.maxElementsOnDisk.getValue()));
    93 
    94         if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {
    95             AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());
    96             restartRequired = true;
    97         }
    98 
    99 
    100         if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {
    101             restartRequired = true;
    102             restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 0); // clear old cache directory
    103             CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
    104         }
    105 
    10668        if (!AbstractTileSourceLayer.ZOOM_OFFSET.get().equals(this.tilesZoom.getValue())) {
    10769            // TODO: make warning about too small MEMORY_CACHE_SIZE?
     
    11173        return restartRequired;
    11274    }
    113 
    114     private static boolean removeCacheFiles(String path, long maxSize) {
    115 
    116         File directory = new File(path);
    117         File[] cacheFiles = directory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".data") || name.endsWith(".key"));
    118         boolean restartRequired = false;
    119         if (cacheFiles != null) {
    120             for (File cacheFile: cacheFiles) {
    121                 if (cacheFile.length() > maxSize) {
    122                     if (!restartRequired) {
    123                         JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
    124                         restartRequired = true;
    125                     }
    126                     Utils.deleteFile(cacheFile);
    127                     File otherFile = null;
    128                     if (cacheFile.getName().endsWith(".data")) {
    129                         otherFile = new File(cacheFile.getPath().replaceAll("\\.data$", ".key"));
    130                     } else if (cacheFile.getName().endsWith(".key")) {
    131                         otherFile = new File(cacheFile.getPath().replaceAll("\\.key$", ".data"));
    132                     }
    133                     if (otherFile != null) {
    134                         Utils.deleteFileIfExists(otherFile);
    135                     }
    136                 }
    137             }
    138         }
    139         return restartRequired;
    140     }
    14175}
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java

    r14629 r14632  
    9090    private final WMSSettingsPanel wmsSettings = new WMSSettingsPanel();
    9191    private final TMSSettingsPanel tmsSettings = new TMSSettingsPanel();
     92    private final CacheSettingsPanel cacheSettingsPanel = new CacheSettingsPanel();
    9293
    9394    /**
     
    143144        pane.addTab(tr("Settings"), buildSettingsPanel());
    144145        pane.addTab(tr("Offset bookmarks"), new OffsetBookmarksPanel(gui));
    145         pane.addTab(tr("Cache contents"), new CacheContentsPanel());
     146        pane.addTab(tr("Cache"), cacheSettingsPanel);
    146147        loadSettings();
    147148        p.add(pane, GBC.std().fill(GBC.BOTH));
     
    160161        wmsSettings.loadSettings();
    161162        tmsSettings.loadSettings();
     163        cacheSettingsPanel.loadSettings();
    162164    }
    163165
     
    177179        boolean wmsRestartRequired = wmsSettings.saveSettings();
    178180        boolean tmsRestartRequired = tmsSettings.saveSettings();
    179 
    180         return commonRestartRequired || wmsRestartRequired || tmsRestartRequired;
     181        boolean cacheRestartRequired = cacheSettingsPanel.saveSettings();
     182
     183        return commonRestartRequired || wmsRestartRequired || tmsRestartRequired || cacheRestartRequired;
    181184    }
    182185
Note: See TracChangeset for help on using the changeset viewer.