~josejuan-sanchez/+junk/original-jhv-experimental-version

« back to all changes in this revision

Viewing changes to src/jhv/src/org/helioviewer/jhv/Settings.java

  • Committer: José Juan Sánchez Hernández
  • Date: 2013-02-05 13:32:08 UTC
  • Revision ID: josejuan.sanchez@gmail.com-20130205133208-dfz1sh1uge5pjkny
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.helioviewer.jhv;
 
2
 
 
3
import java.io.File;
 
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;
 
9
 
 
10
import javax.swing.SwingUtilities;
 
11
import javax.swing.UIManager;
 
12
 
 
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;
 
18
 
 
19
/**
 
20
 * A class that stores and reads default values in a settings file.
 
21
 * 
 
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.
 
24
 * 
 
25
 * @author Benjamin Wamsler
 
26
 * @author Juan Pablo
 
27
 * @author Markus Langenberg
 
28
 * @author Andre Dau
 
29
 */
 
30
public class Settings {
 
31
    /** The sole instance of this class. */
 
32
    private static final Settings singletonInstance = new Settings();
 
33
 
 
34
    /** The properties object */
 
35
    private Properties defaultProperties = new Properties();
 
36
    private Properties userProperties = new Properties();
 
37
 
 
38
    /** The properties file */
 
39
    private File propFile = new File(JHVDirectory.SETTINGS.getPath() + "user.properties");
 
40
 
 
41
    /**
 
42
     * The private constructor of this class.
 
43
     * */
 
44
    private Settings() {
 
45
    }
 
46
 
 
47
    public void load() {
 
48
        load(true);
 
49
    }
 
50
 
 
51
    /**
 
52
     * Method loads the settings from a user file or the default settings file
 
53
     * */
 
54
    public void load(boolean verbose) {
 
55
        try {
 
56
            defaultProperties.clear();
 
57
            userProperties.clear();
 
58
 
 
59
            InputStream defaultPropStream = FileUtils.getResourceInputStream("/settings/defaults.properties");
 
60
            defaultProperties.load(defaultPropStream);
 
61
            defaultPropStream.close();
 
62
            if (verbose) {
 
63
                Log.debug(">> Settings.load() > Load default system settings: " + defaultProperties.toString());
 
64
            }
 
65
            if (propFile.exists()) {
 
66
                FileInputStream fileInput = new FileInputStream(propFile);
 
67
                userProperties.load(fileInput);
 
68
                fileInput.close();
 
69
            }
 
70
 
 
71
            if (getProperty("default.save.path") == null) {
 
72
                setProperty("default.save.path", JHVDirectory.EXPORTS.getPath());
 
73
            }
 
74
            if (getProperty("default.local.path") == null) {
 
75
                setProperty("default.local.path", JHVDirectory.HOME.getPath());
 
76
            }
 
77
        } catch (Exception ex) {
 
78
            if (verbose) {
 
79
                Log.error(">> Settings.load(boolean) > Could not load settings", ex);
 
80
            } else {
 
81
                ex.printStackTrace();
 
82
            }
 
83
        }
 
84
    }
 
85
 
 
86
    /**
 
87
     * This method saves all the values in the user properties file.
 
88
     */
 
89
    public void save() {
 
90
        try {
 
91
            propFile.createNewFile();
 
92
            FileOutputStream fileOutput = new FileOutputStream(propFile);
 
93
            userProperties.store(fileOutput, null);
 
94
            fileOutput.close();
 
95
 
 
96
        } catch (IOException ex) {
 
97
            ex.printStackTrace();
 
98
        }
 
99
    }
 
100
 
 
101
    /**
 
102
     * The new property values are updated.
 
103
     */
 
104
    public void update() {
 
105
        try {
 
106
            String val;
 
107
 
 
108
            val = getProperty("opengl.enabled");
 
109
            GLInfo.setGlEnabled(Boolean.parseBoolean(val));
 
110
 
 
111
            val = getProperty("display.laf");
 
112
 
 
113
            setLookAndFeelEverywhere(val);
 
114
 
 
115
            double size = 0;
 
116
            val = getProperty("jpip.cache.size");
 
117
            if (val != null) {
 
118
                try {
 
119
                    size = Double.valueOf(val);
 
120
                } catch (NumberFormatException ex) {
 
121
                    Log.error(">> Settings.update(boolean) > Invalid jpip cache size: " + val);
 
122
                }
 
123
                setProperty("jpip.cache.size", Double.toString(size));
 
124
            }
 
125
 
 
126
            JHV_Kdu_cache.updateCacheDirectory(JHVDirectory.CACHE.getFile(), size);
 
127
        } catch (Exception ex) {
 
128
            ex.printStackTrace();
 
129
        }
 
130
    }
 
131
 
 
132
    /**
 
133
     * Method sets the value of a specified property and saves it as a user
 
134
     * setting
 
135
     * 
 
136
     * @param key
 
137
     *            Default field to be written to
 
138
     * @param val
 
139
     *            Value to be set to
 
140
     */
 
141
    public void setProperty(String key, String val) {
 
142
        if (!val.equals(getProperty(key))) {
 
143
            userProperties.setProperty(key, val);
 
144
        }
 
145
    }
 
146
 
 
147
    /**
 
148
     * Method that returns the value of the specified property. User defined
 
149
     * properties are always preferred over the default settings.
 
150
     * 
 
151
     * @param key
 
152
     *            Default field to read
 
153
     */
 
154
    public String getProperty(String key) {
 
155
        String val = userProperties.getProperty(key);
 
156
        if (val == null) {
 
157
            val = defaultProperties.getProperty(key);
 
158
        }
 
159
        return val;
 
160
    }
 
161
 
 
162
    /**
 
163
     * Method returns the sole instance of this class.
 
164
     * 
 
165
     * @return the only instance of this class.
 
166
     * */
 
167
    public static Settings getSingletonInstance() {
 
168
        return singletonInstance;
 
169
    }
 
170
 
 
171
    /**
 
172
     * Sets the look and feel to all windows of the application.
 
173
     * 
 
174
     * @param lookAndFeel
 
175
     *            name of the lookandfeel.
 
176
     */
 
177
    public void setLookAndFeelEverywhere(String lookAndFeel) {
 
178
 
 
179
        if (!UIManager.getLookAndFeel().getClass().getName().equals(lookAndFeel)) {
 
180
            try {
 
181
                UIManager.setLookAndFeel(lookAndFeel);
 
182
 
 
183
                ImageViewerGui.getSingletonInstance();
 
184
                SwingUtilities.updateComponentTreeUI(ImageViewerGui.getMainFrame());
 
185
 
 
186
            } catch (Exception ex) {
 
187
                ex.printStackTrace();
 
188
            }
 
189
        }
 
190
 
 
191
    }
 
192
 
 
193
    /**
 
194
     * The values are saved to disk only if there have been a modification.
 
195
     */
 
196
    protected void finalize() {
 
197
        save();
 
198
    }
 
199
}