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

Last change on this file since 809 was 776, checked in by alloc, 12 years ago

AEI2.00:

  • Added "-usewd" param to use working directory instead of jar path
File size: 5.4 KB
RevLine 
[726]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
11import com.thoughtworks.xstream.XStream;
12import com.thoughtworks.xstream.io.xml.StaxDriver;
13
14/**
15 * Manages and stores programm settings
16 *
17 * @author Christian Illy
18 */
19public class SettingsManager implements Serializable {
20
21 private static final long serialVersionUID = 8067725289301601179L;
22
23 private static SettingsManager instance = new SettingsManager();
24
25 private static boolean debugRun = false;
[776]26 private static boolean useWorkingDir = false;
[726]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 /**
[776]60 * @param useWd
61 * Use working directory instead of jar path
62 */
63 public static void setUseWorkingDir(boolean useWd) {
64 useWorkingDir = useWd;
65 }
66
67 /**
68 * @return Do we want to use working directory
69 */
70 public static boolean getUseWorkingDir() {
71 return useWorkingDir;
72 }
73
74 /**
[726]75 * @return Is offline?
76 */
77 public boolean isOfflineMode() {
78 return offlineMode;
79 }
80
81 /**
82 * @param offline
83 * Is offline?
84 */
85 public void setOfflineMode(boolean offline) {
86 this.offlineMode = offline;
87 }
88
89 /**
90 * @return Is in noCacheUpdate mode?
91 */
92 public boolean isNoCacheUpdateMode() {
93 return noCacheUpdate;
94 }
95
96 /**
97 * @param noCacheUpdate
98 * Is in noCacheUpdate mode?
99 */
100 public void setNoCacheUpdateMode(boolean noCacheUpdate) {
101 this.noCacheUpdate = noCacheUpdate;
102 }
103
104 private static XStream getXStream() {
105 XStream xs = new XStream(new StaxDriver());
106 xs.alias("Settings", SettingsManager.class);
107 return xs;
108 }
109
110 /**
111 * Serializes the settings to disk
112 *
113 * @param settingsFile
114 * File to write to
115 */
116 public void serializeToFile(File settingsFile) {
117 try {
118 FileOutputStream fos = new FileOutputStream(settingsFile);
119 XStream xs = getXStream();
120 xs.toXML(this, fos);
121 fos.close();
122 } catch (FileNotFoundException e) {
123 e.printStackTrace();
124 } catch (IOException e) {
125 e.printStackTrace();
126 }
127 }
128
129 /**
130 * Deserializes the settings from disk
131 *
132 * @param settingsFile
133 * File to read from
134 */
135 public static void deserializeFromFile(File settingsFile) {
136 try {
137 FileInputStream fis = new FileInputStream(settingsFile);
138 XStream xs = getXStream();
139 Object obj = xs.fromXML(fis);
140 if (obj instanceof SettingsManager)
141 instance = (SettingsManager) obj;
142 fis.close();
143 } catch (FileNotFoundException e) {
144 } catch (IOException e) {
145 }
146 }
147
148 /**
149 * Put a string value
150 *
151 * @param key
152 * Key for value
153 * @param value
154 * Value
155 */
156 public void put(String key, String value) {
157 prefs.put(key, value);
158 }
159
160 /**
161 * Put a boolean value
162 *
163 * @param key
164 * Key for value
165 * @param value
166 * Value
167 */
168 public void put(String key, boolean value) {
169 prefs.put(key, value);
170 }
171
172 /**
173 * Put a int value
174 *
175 * @param key
176 * Key for value
177 * @param value
178 * Value
179 */
180 public void put(String key, int value) {
181 prefs.put(key, value);
182 }
183
184 /**
185 * Get a string value
186 *
187 * @param key
188 * Key for value
189 * @param def
190 * Default return value if key does not exist
191 * @return Value
192 */
193 public String get(String key, String def) {
194 if (prefs.containsKey(key)) {
195 if (prefs.get(key) instanceof String)
196 return (String) (prefs.get(key));
197 }
198 if (printNamesNotInMap)
199 System.out.println("Settings: Key \"" + key
200 + "\" not in Map, defaulting to \"" + def + "\".");
201 return def;
202 }
203
204 /**
205 * Get a boolean value
206 *
207 * @param key
208 * Key for value
209 * @param def
210 * Default return value if key does not exist
211 * @return Value
212 */
213 public Boolean get(String key, Boolean def) {
214 if (prefs.containsKey(key)) {
215 if (prefs.get(key) instanceof Boolean)
216 return (Boolean) (prefs.get(key));
217 }
218 if (printNamesNotInMap)
219 System.out.println("Settings: Key \"" + key
220 + "\" not in Map, defaulting to \"" + def + "\".");
221 return def;
222 }
223
224 /**
225 * Get a int value
226 *
227 * @param key
228 * Key for value
229 * @param def
230 * Default return value if key does not exist
231 * @return Value
232 */
233 public int get(String key, int def) {
234 if (prefs.containsKey(key)) {
235 if (prefs.get(key) instanceof Integer)
236 return (Integer) (prefs.get(key));
237 }
238 if (printNamesNotInMap)
239 System.out.println("Settings: Key \"" + key
240 + "\" not in Map, defaulting to \"" + def + "\".");
241 return def;
242 }
243
244 /**
245 * Remove a value
246 *
247 * @param key
248 * Key to value to remove
249 */
250 public void removeValue(String key) {
251 prefs.remove(key);
252 }
253
254}
Note: See TracBrowser for help on using the repository browser.