Changeset 24989 in osm


Ignore:
Timestamp:
2011-01-07T15:29:33+01:00 (14 years ago)
Author:
upliner
Message:

jmapviewer: add function to get default cache directory and throw IOException if temp directory is not set

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java

    r18772 r24989  
    88import java.awt.event.ItemEvent;
    99import java.awt.event.ItemListener;
     10import java.io.IOException;
    1011
    1112import javax.swing.JButton;
     
    6162            }
    6263        });
    63         JComboBox tileLoaderSelector = new JComboBox(new TileLoader[] { new OsmFileCacheTileLoader(map),
    64                 new OsmTileLoader(map) });
     64        JComboBox tileLoaderSelector;
     65        try {
     66            tileLoaderSelector = new JComboBox(new TileLoader[] { new OsmFileCacheTileLoader(map),
     67                    new OsmTileLoader(map) });
     68        } catch (IOException e) {
     69            tileLoaderSelector = new JComboBox(new TileLoader[] { new OsmTileLoader(map) });
     70        }
    6571        tileLoaderSelector.addItemListener(new ItemListener() {
    6672            public void itemStateChanged(ItemEvent e) {
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java

    r24985 r24989  
    5454    protected long recheckAfter = FILE_AGE_ONE_DAY;
    5555
    56     /**
    57      * Create a OSMFileCacheTileLoader with given cache directory.
    58      * If cacheDir is <code>null</code> the system property temp dir
    59      * is used. If not set an IOException will be thrown.
    60      * @param map
    61      * @param cacheDir
    62      */
    63     public OsmFileCacheTileLoader(TileLoaderListener map, File cacheDir) throws SecurityException {
    64         super(map);
     56    public static File getDefaultCacheDir() throws SecurityException {
    6557        String tempDir = null;
    6658        String userName = System.getProperty("user.name");
     
    7466        }
    7567        try {
    76             if (cacheDir == null) {
    77                 if (tempDir == null)
    78                     throw new IOException("No temp directory set");
    79                 String subDirName = "JMapViewerTiles";
    80                 // On Linux/Unix systems we do not have a per user tmp directory.
    81                 // Therefore we add the user name for getting a unique dir name.
    82                 if (userName != null && userName.length() > 0) {
    83                     subDirName += "_" + userName;
    84                 }
    85                 cacheDir = new File(tempDir, subDirName);
    86             }
    87             log.finest("Tile cache directory: " + cacheDir);
    88             if (!cacheDir.exists() && !cacheDir.mkdirs())
    89                 throw new IOException();
    90             cacheDirBase = cacheDir.getAbsolutePath();
     68            if (tempDir == null)
     69                throw new IOException("No temp directory set");
     70            String subDirName = "JMapViewerTiles";
     71            // On Linux/Unix systems we do not have a per user tmp directory.
     72            // Therefore we add the user name for getting a unique dir name.
     73            if (userName != null && userName.length() > 0) {
     74                subDirName += "_" + userName;
     75            }
     76            File cacheDir = new File(tempDir, subDirName);
     77            return cacheDir;
    9178        } catch (Exception e) {
    92             cacheDirBase = "tiles";
    93         }
     79        }
     80        return null;
     81    }
     82
     83    /**
     84     * Create a OSMFileCacheTileLoader with given cache directory.
     85     * If cacheDir is not set or invalid, IOException will be thrown.
     86     * @param map
     87     * @param cacheDir
     88     */
     89    public OsmFileCacheTileLoader(TileLoaderListener map, File cacheDir) throws IOException  {
     90        super(map);
     91        if (cacheDir == null || (!cacheDir.exists() && !cacheDir.mkdirs()))
     92            throw new IOException("Cannot access cache directory");
     93
     94        log.finest("Tile cache directory: " + cacheDir);
     95        cacheDirBase = cacheDir.getAbsolutePath();
    9496    }
    9597
     
    99101     * @param map
    100102     */
    101     public OsmFileCacheTileLoader(TileLoaderListener map) throws SecurityException {
    102         this(map, null);
     103    public OsmFileCacheTileLoader(TileLoaderListener map) throws SecurityException, IOException {
     104        this(map, getDefaultCacheDir());
    103105    }
    104106
Note: See TracChangeset for help on using the changeset viewer.