1
package org.helioviewer.jhv;
4
import java.io.FileInputStream;
5
import java.io.FileOutputStream;
6
import java.io.IOException;
7
import java.io.InputStream;
8
import java.util.Properties;
10
import javax.swing.SwingUtilities;
11
import javax.swing.UIManager;
13
import org.helioviewer.base.FileUtils;
14
import org.helioviewer.base.logging.Log;
15
import org.helioviewer.jhv.gui.ImageViewerGui;
16
import org.helioviewer.jhv.opengl.GLInfo;
17
import org.helioviewer.viewmodel.view.jp2view.kakadu.JHV_Kdu_cache;
20
* A class that stores and reads default values in a settings file.
22
* To check, whether the default settings have changed, a version string is
23
* used. This string always should contain the date of the last change.
25
* @author Benjamin Wamsler
27
* @author Markus Langenberg
30
public class Settings {
31
/** The sole instance of this class. */
32
private static final Settings singletonInstance = new Settings();
34
/** The properties object */
35
private Properties defaultProperties = new Properties();
36
private Properties userProperties = new Properties();
38
/** The properties file */
39
private File propFile = new File(JHVDirectory.SETTINGS.getPath() + "user.properties");
42
* The private constructor of this class.
52
* Method loads the settings from a user file or the default settings file
54
public void load(boolean verbose) {
56
defaultProperties.clear();
57
userProperties.clear();
59
InputStream defaultPropStream = FileUtils.getResourceInputStream("/settings/defaults.properties");
60
defaultProperties.load(defaultPropStream);
61
defaultPropStream.close();
63
Log.debug(">> Settings.load() > Load default system settings: " + defaultProperties.toString());
65
if (propFile.exists()) {
66
FileInputStream fileInput = new FileInputStream(propFile);
67
userProperties.load(fileInput);
71
if (getProperty("default.save.path") == null) {
72
setProperty("default.save.path", JHVDirectory.EXPORTS.getPath());
74
if (getProperty("default.local.path") == null) {
75
setProperty("default.local.path", JHVDirectory.HOME.getPath());
77
} catch (Exception ex) {
79
Log.error(">> Settings.load(boolean) > Could not load settings", ex);
87
* This method saves all the values in the user properties file.
91
propFile.createNewFile();
92
FileOutputStream fileOutput = new FileOutputStream(propFile);
93
userProperties.store(fileOutput, null);
96
} catch (IOException ex) {
102
* The new property values are updated.
104
public void update() {
108
val = getProperty("opengl.enabled");
109
GLInfo.setGlEnabled(Boolean.parseBoolean(val));
111
val = getProperty("display.laf");
113
setLookAndFeelEverywhere(val);
116
val = getProperty("jpip.cache.size");
119
size = Double.valueOf(val);
120
} catch (NumberFormatException ex) {
121
Log.error(">> Settings.update(boolean) > Invalid jpip cache size: " + val);
123
setProperty("jpip.cache.size", Double.toString(size));
126
JHV_Kdu_cache.updateCacheDirectory(JHVDirectory.CACHE.getFile(), size);
127
} catch (Exception ex) {
128
ex.printStackTrace();
133
* Method sets the value of a specified property and saves it as a user
137
* Default field to be written to
141
public void setProperty(String key, String val) {
142
if (!val.equals(getProperty(key))) {
143
userProperties.setProperty(key, val);
148
* Method that returns the value of the specified property. User defined
149
* properties are always preferred over the default settings.
152
* Default field to read
154
public String getProperty(String key) {
155
String val = userProperties.getProperty(key);
157
val = defaultProperties.getProperty(key);
163
* Method returns the sole instance of this class.
165
* @return the only instance of this class.
167
public static Settings getSingletonInstance() {
168
return singletonInstance;
172
* Sets the look and feel to all windows of the application.
175
* name of the lookandfeel.
177
public void setLookAndFeelEverywhere(String lookAndFeel) {
179
if (!UIManager.getLookAndFeel().getClass().getName().equals(lookAndFeel)) {
181
UIManager.setLookAndFeel(lookAndFeel);
183
ImageViewerGui.getSingletonInstance();
184
SwingUtilities.updateComponentTreeUI(ImageViewerGui.getMainFrame());
186
} catch (Exception ex) {
187
ex.printStackTrace();
194
* The values are saved to disk only if there have been a modification.
196
protected void finalize() {