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

File:
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}
Note: See TracChangeset for help on using the changeset viewer.