source: josm/trunk/src/org/openstreetmap/josm/data/preferences/JosmBaseDirectories.java@ 19050

Last change on this file since 19050 was 19050, checked in by taylor.smock, 4 weeks ago

Revert most var changes from r19048, fix most new compile warnings and checkstyle issues

Also, document why various ErrorProne checks were originally disabled and fix
generic SonarLint issues.

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5import static org.openstreetmap.josm.tools.Utils.getSystemProperty;
6
7import java.awt.GraphicsEnvironment;
8import java.io.File;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.gui.MainApplication;
13import org.openstreetmap.josm.spi.preferences.Config;
14import org.openstreetmap.josm.spi.preferences.IBaseDirectories;
15import org.openstreetmap.josm.tools.Logging;
16import org.openstreetmap.josm.tools.PlatformManager;
17
18/**
19 * Class provides base directory locations for JOSM.
20 * @since 13021
21 */
22public final class JosmBaseDirectories implements IBaseDirectories {
23
24 private JosmBaseDirectories() {
25 // hide constructor
26 }
27
28 private static final class InstanceHolder {
29 static final JosmBaseDirectories INSTANCE = new JosmBaseDirectories();
30 }
31
32 /**
33 * Returns the unique instance.
34 * @return the unique instance
35 */
36 public static JosmBaseDirectories getInstance() {
37 return InstanceHolder.INSTANCE;
38 }
39
40 /**
41 * Internal storage for the preference directory.
42 */
43 private File preferencesDir;
44
45 /**
46 * Internal storage for the cache directory.
47 */
48 private File cacheDir;
49
50 /**
51 * Internal storage for the user data directory.
52 */
53 private File userdataDir;
54
55 @Override
56 public File getPreferencesDirectory(boolean createIfMissing) {
57 if (preferencesDir == null) {
58 String path = getSystemProperty("josm.pref");
59 if (path != null) {
60 preferencesDir = new File(path).getAbsoluteFile();
61 } else {
62 path = getSystemProperty("josm.home");
63 if (path != null) {
64 preferencesDir = new File(path).getAbsoluteFile();
65 } else {
66 preferencesDir = PlatformManager.getPlatform().getDefaultPrefDirectory();
67 }
68 }
69 }
70 try {
71 if (createIfMissing && !preferencesDir.exists() && !preferencesDir.mkdirs()) {
72 Logging.warn(tr("Failed to create missing preferences directory: {0}", preferencesDir.getAbsoluteFile()));
73 if (!GraphicsEnvironment.isHeadless()) {
74 JOptionPane.showMessageDialog(
75 MainApplication.getMainFrame(),
76 tr("<html>Failed to create missing preferences directory: {0}</html>", preferencesDir.getAbsoluteFile()),
77 tr("Error"),
78 JOptionPane.ERROR_MESSAGE
79 );
80 }
81 }
82 } catch (SecurityException e) {
83 Logging.log(Logging.LEVEL_ERROR, "Unable to check if preferences dir must be created", e);
84 }
85 return preferencesDir;
86 }
87
88 @Override
89 public File getUserDataDirectory(boolean createIfMissing) {
90 if (userdataDir == null) {
91 String path = getSystemProperty("josm.userdata");
92 if (path != null) {
93 userdataDir = new File(path).getAbsoluteFile();
94 } else {
95 path = getSystemProperty("josm.home");
96 if (path != null) {
97 userdataDir = new File(path).getAbsoluteFile();
98 } else {
99 userdataDir = PlatformManager.getPlatform().getDefaultUserDataDirectory();
100 }
101 }
102 }
103 try {
104 if (createIfMissing && !userdataDir.exists() && !userdataDir.mkdirs()) {
105 Logging.warn(tr("Failed to create missing user data directory: {0}", userdataDir.getAbsoluteFile()));
106 if (!GraphicsEnvironment.isHeadless()) {
107 JOptionPane.showMessageDialog(
108 MainApplication.getMainFrame(),
109 tr("<html>Failed to create missing user data directory: {0}</html>", userdataDir.getAbsoluteFile()),
110 tr("Error"),
111 JOptionPane.ERROR_MESSAGE
112 );
113 }
114 }
115 } catch (SecurityException e) {
116 Logging.log(Logging.LEVEL_ERROR, "Unable to check if user data dir must be created", e);
117 }
118 return userdataDir;
119 }
120
121 @Override
122 public File getCacheDirectory(boolean createIfMissing) {
123 if (cacheDir == null) {
124 String path = getSystemProperty("josm.cache");
125 if (path != null) {
126 cacheDir = new File(path).getAbsoluteFile();
127 } else {
128 path = getSystemProperty("josm.home");
129 if (path != null) {
130 cacheDir = new File(path, "cache");
131 } else {
132 path = Config.getPref().get("cache.folder", null);
133 if (path != null) {
134 cacheDir = new File(path).getAbsoluteFile();
135 } else {
136 cacheDir = PlatformManager.getPlatform().getDefaultCacheDirectory();
137 }
138 }
139 }
140 }
141 try {
142 if (createIfMissing && !cacheDir.exists() && !cacheDir.mkdirs()) {
143 Logging.warn(tr("Failed to create missing cache directory: {0}", cacheDir.getAbsoluteFile()));
144 if (!GraphicsEnvironment.isHeadless()) {
145 JOptionPane.showMessageDialog(
146 MainApplication.getMainFrame(),
147 tr("<html>Failed to create missing cache directory: {0}</html>", cacheDir.getAbsoluteFile()),
148 tr("Error"),
149 JOptionPane.ERROR_MESSAGE
150 );
151 }
152 }
153 } catch (SecurityException e) {
154 Logging.log(Logging.LEVEL_ERROR, "Unable to check if cache dir must be created", e);
155 }
156 return cacheDir;
157 }
158
159 /**
160 * Clears any previously calculated values used for {@link #getPreferencesDirectory(boolean)},
161 * {@link #getCacheDirectory(boolean)} or {@link #getUserDataDirectory(boolean)}. Useful for tests.
162 * @since 14052
163 */
164 public void clearMemos() {
165 this.preferencesDir = null;
166 this.cacheDir = null;
167 this.userdataDir = null;
168 }
169}
Note: See TracBrowser for help on using the repository browser.