001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.preferences; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.File; 007 008import javax.swing.JOptionPane; 009 010import org.openstreetmap.josm.Main; 011import org.openstreetmap.josm.spi.preferences.Config; 012import org.openstreetmap.josm.spi.preferences.IBaseDirectories; 013import org.openstreetmap.josm.tools.Logging; 014 015/** 016 * Class provides base directory locations for JOSM. 017 * @since 13021 018 */ 019public final class JosmBaseDirectories implements IBaseDirectories { 020 021 private JosmBaseDirectories() { 022 // hide constructor 023 } 024 025 private static class InstanceHolder { 026 static final JosmBaseDirectories INSTANCE = new JosmBaseDirectories(); 027 } 028 029 public static JosmBaseDirectories getInstance() { 030 return InstanceHolder.INSTANCE; 031 } 032 033 /** 034 * Internal storage for the preference directory. 035 */ 036 private File preferencesDir; 037 038 /** 039 * Internal storage for the cache directory. 040 */ 041 private File cacheDir; 042 043 /** 044 * Internal storage for the user data directory. 045 */ 046 private File userdataDir; 047 048 @Override 049 public File getPreferencesDirectory(boolean createIfMissing) { 050 if (preferencesDir == null) { 051 String path; 052 path = System.getProperty("josm.pref"); 053 if (path != null) { 054 preferencesDir = new File(path).getAbsoluteFile(); 055 } else { 056 path = System.getProperty("josm.home"); 057 if (path != null) { 058 preferencesDir = new File(path).getAbsoluteFile(); 059 } else { 060 preferencesDir = Main.platform.getDefaultPrefDirectory(); 061 } 062 } 063 } 064 if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) { 065 Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile())); 066 JOptionPane.showMessageDialog( 067 Main.parent, 068 tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()), 069 tr("Error"), 070 JOptionPane.ERROR_MESSAGE 071 ); 072 } 073 return preferencesDir; 074 } 075 076 @Override 077 public File getUserDataDirectory(boolean createIfMissing) { 078 if (userdataDir == null) { 079 String path; 080 path = System.getProperty("josm.userdata"); 081 if (path != null) { 082 userdataDir = new File(path).getAbsoluteFile(); 083 } else { 084 path = System.getProperty("josm.home"); 085 if (path != null) { 086 userdataDir = new File(path).getAbsoluteFile(); 087 } else { 088 userdataDir = Main.platform.getDefaultUserDataDirectory(); 089 } 090 } 091 } 092 if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) { 093 Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile())); 094 JOptionPane.showMessageDialog( 095 Main.parent, 096 tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()), 097 tr("Error"), 098 JOptionPane.ERROR_MESSAGE 099 ); 100 } 101 return userdataDir; 102 } 103 104 @Override 105 public File getCacheDirectory(boolean createIfMissing) { 106 if (cacheDir == null) { 107 String path = System.getProperty("josm.cache"); 108 if (path != null) { 109 cacheDir = new File(path).getAbsoluteFile(); 110 } else { 111 path = System.getProperty("josm.home"); 112 if (path != null) { 113 cacheDir = new File(path, "cache"); 114 } else { 115 path = Config.getPref().get("cache.folder", null); 116 if (path != null) { 117 cacheDir = new File(path).getAbsoluteFile(); 118 } else { 119 cacheDir = Main.platform.getDefaultCacheDirectory(); 120 } 121 } 122 } 123 } 124 if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) { 125 Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile())); 126 JOptionPane.showMessageDialog( 127 Main.parent, 128 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()), 129 tr("Error"), 130 JOptionPane.ERROR_MESSAGE 131 ); 132 } 133 return cacheDir; 134 } 135}