source: java/ProgramSettings/src/net/oni2/SettingsManager.java@ 773

Last change on this file since 773 was 726, checked in by alloc, 12 years ago

ProgramSettings library

File size: 5.0 KB
Line 
1package net.oni2;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileNotFoundException;
6import java.io.FileOutputStream;
7import java.io.IOException;
8import java.io.Serializable;
9import java.util.HashMap;
10
11
12import com.thoughtworks.xstream.XStream;
13import com.thoughtworks.xstream.io.xml.StaxDriver;
14
15/**
16 * Manages and stores programm settings
17 *
18 * @author Christian Illy
19 */
20public class SettingsManager implements Serializable {
21
22 private static final long serialVersionUID = 8067725289301601179L;
23
24 private static SettingsManager instance = new SettingsManager();
25
26 private static boolean debugRun = false;
27
28 private HashMap<String, Object> prefs = new HashMap<String, Object>();
29
30 private boolean printNamesNotInMap = false;
31
32 private boolean offlineMode = false;
33 private boolean noCacheUpdate = false;
34
35 /**
36 * Get the singleton instance
37 *
38 * @return Singleton instance
39 */
40 public static SettingsManager getInstance() {
41 return instance;
42 }
43
44 /**
45 * @param debug
46 * Debug mode
47 */
48 public static void setDebug(boolean debug) {
49 debugRun = debug;
50 }
51
52 /**
53 * @return Is debug run
54 */
55 public static boolean isDebug() {
56 return debugRun;
57 }
58
59 /**
60 * @return Is offline?
61 */
62 public boolean isOfflineMode() {
63 return offlineMode;
64 }
65
66 /**
67 * @param offline
68 * Is offline?
69 */
70 public void setOfflineMode(boolean offline) {
71 this.offlineMode = offline;
72 }
73
74 /**
75 * @return Is in noCacheUpdate mode?
76 */
77 public boolean isNoCacheUpdateMode() {
78 return noCacheUpdate;
79 }
80
81 /**
82 * @param noCacheUpdate
83 * Is in noCacheUpdate mode?
84 */
85 public void setNoCacheUpdateMode(boolean noCacheUpdate) {
86 this.noCacheUpdate = noCacheUpdate;
87 }
88
89 private static XStream getXStream() {
90 XStream xs = new XStream(new StaxDriver());
91 xs.alias("Settings", SettingsManager.class);
92 return xs;
93 }
94
95 /**
96 * Serializes the settings to disk
97 *
98 * @param settingsFile
99 * File to write to
100 */
101 public void serializeToFile(File settingsFile) {
102 try {
103 FileOutputStream fos = new FileOutputStream(settingsFile);
104 XStream xs = getXStream();
105 xs.toXML(this, fos);
106 fos.close();
107 } catch (FileNotFoundException e) {
108 e.printStackTrace();
109 } catch (IOException e) {
110 e.printStackTrace();
111 }
112 }
113
114 /**
115 * Deserializes the settings from disk
116 *
117 * @param settingsFile
118 * File to read from
119 */
120 public static void deserializeFromFile(File settingsFile) {
121 try {
122 FileInputStream fis = new FileInputStream(settingsFile);
123 XStream xs = getXStream();
124 Object obj = xs.fromXML(fis);
125 if (obj instanceof SettingsManager)
126 instance = (SettingsManager) obj;
127 fis.close();
128 } catch (FileNotFoundException e) {
129 } catch (IOException e) {
130 }
131 }
132
133 /**
134 * Put a string value
135 *
136 * @param key
137 * Key for value
138 * @param value
139 * Value
140 */
141 public void put(String key, String value) {
142 prefs.put(key, value);
143 }
144
145 /**
146 * Put a boolean value
147 *
148 * @param key
149 * Key for value
150 * @param value
151 * Value
152 */
153 public void put(String key, boolean value) {
154 prefs.put(key, value);
155 }
156
157 /**
158 * Put a int value
159 *
160 * @param key
161 * Key for value
162 * @param value
163 * Value
164 */
165 public void put(String key, int value) {
166 prefs.put(key, value);
167 }
168
169 /**
170 * Get a string value
171 *
172 * @param key
173 * Key for value
174 * @param def
175 * Default return value if key does not exist
176 * @return Value
177 */
178 public String get(String key, String def) {
179 if (prefs.containsKey(key)) {
180 if (prefs.get(key) instanceof String)
181 return (String) (prefs.get(key));
182 }
183 if (printNamesNotInMap)
184 System.out.println("Settings: Key \"" + key
185 + "\" not in Map, defaulting to \"" + def + "\".");
186 return def;
187 }
188
189 /**
190 * Get a boolean value
191 *
192 * @param key
193 * Key for value
194 * @param def
195 * Default return value if key does not exist
196 * @return Value
197 */
198 public Boolean get(String key, Boolean def) {
199 if (prefs.containsKey(key)) {
200 if (prefs.get(key) instanceof Boolean)
201 return (Boolean) (prefs.get(key));
202 }
203 if (printNamesNotInMap)
204 System.out.println("Settings: Key \"" + key
205 + "\" not in Map, defaulting to \"" + def + "\".");
206 return def;
207 }
208
209 /**
210 * Get a int value
211 *
212 * @param key
213 * Key for value
214 * @param def
215 * Default return value if key does not exist
216 * @return Value
217 */
218 public int get(String key, int def) {
219 if (prefs.containsKey(key)) {
220 if (prefs.get(key) instanceof Integer)
221 return (Integer) (prefs.get(key));
222 }
223 if (printNamesNotInMap)
224 System.out.println("Settings: Key \"" + key
225 + "\" not in Map, defaulting to \"" + def + "\".");
226 return def;
227 }
228
229 /**
230 * Remove a value
231 *
232 * @param key
233 * Key to value to remove
234 */
235 public void removeValue(String key) {
236 prefs.remove(key);
237 }
238
239}
Note: See TracBrowser for help on using the repository browser.