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

Last change on this file since 1009 was 852, checked in by alloc, 12 years ago

AEI2.08, AEIUpdater:

File size: 4.3 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
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 HashMap<String, Object> prefs = new HashMap<String, Object>();
26
27 private boolean printNamesNotInMap = false;
28
29 @SuppressWarnings("unused")
30 private transient boolean offlineMode = false;
31 @SuppressWarnings("unused")
32 private transient boolean noCacheUpdate = false;
33
34 /**
35 * Get the singleton instance
36 *
37 * @return Singleton instance
38 */
39 public static SettingsManager getInstance() {
40 return instance;
41 }
42
43 private static XStream getXStream() {
44 XStream xs = new XStream(new StaxDriver());
45 xs.alias("Settings", SettingsManager.class);
46 return xs;
47 }
48
49 /**
50 * Serializes the settings to disk
51 *
52 * @param settingsFile
53 * File to write to
54 */
55 public void serializeToFile(File settingsFile) {
56 try {
57 FileOutputStream fos = new FileOutputStream(settingsFile);
58 XStream xs = getXStream();
59 xs.toXML(this, fos);
60 fos.close();
61 } catch (FileNotFoundException e) {
62 e.printStackTrace();
63 } catch (IOException e) {
64 e.printStackTrace();
65 }
66 }
67
68 /**
69 * Deserializes the settings from disk
70 *
71 * @param settingsFile
72 * File to read from
73 */
74 public static void deserializeFromFile(File settingsFile) {
75 try {
76 FileInputStream fis = new FileInputStream(settingsFile);
77 XStream xs = getXStream();
78 Object obj = xs.fromXML(fis);
79 if (obj instanceof SettingsManager)
80 instance = (SettingsManager) obj;
81 fis.close();
82 } catch (FileNotFoundException e) {
83 } catch (IOException e) {
84 }
85 }
86
87 /**
88 * Put a string value
89 *
90 * @param key
91 * Key for value
92 * @param value
93 * Value
94 */
95 public void put(String key, String value) {
96 prefs.put(key, value);
97 }
98
99 /**
100 * Put a boolean value
101 *
102 * @param key
103 * Key for value
104 * @param value
105 * Value
106 */
107 public void put(String key, boolean value) {
108 prefs.put(key, value);
109 }
110
111 /**
112 * Put a int value
113 *
114 * @param key
115 * Key for value
116 * @param value
117 * Value
118 */
119 public void put(String key, int value) {
120 prefs.put(key, value);
121 }
122
123 /**
124 * Get a string value
125 *
126 * @param key
127 * Key for value
128 * @param def
129 * Default return value if key does not exist
130 * @return Value
131 */
132 public String get(String key, String def) {
133 if (prefs.containsKey(key)) {
134 if (prefs.get(key) instanceof String)
135 return (String) (prefs.get(key));
136 }
137 if (printNamesNotInMap)
138 System.out.println("Settings: Key \"" + key
139 + "\" not in Map, defaulting to \"" + def + "\".");
140 return def;
141 }
142
143 /**
144 * Get a boolean value
145 *
146 * @param key
147 * Key for value
148 * @param def
149 * Default return value if key does not exist
150 * @return Value
151 */
152 public Boolean get(String key, Boolean def) {
153 if (prefs.containsKey(key)) {
154 if (prefs.get(key) instanceof Boolean)
155 return (Boolean) (prefs.get(key));
156 }
157 if (printNamesNotInMap)
158 System.out.println("Settings: Key \"" + key
159 + "\" not in Map, defaulting to \"" + def + "\".");
160 return def;
161 }
162
163 /**
164 * Get a int value
165 *
166 * @param key
167 * Key for value
168 * @param def
169 * Default return value if key does not exist
170 * @return Value
171 */
172 public int get(String key, int def) {
173 if (prefs.containsKey(key)) {
174 if (prefs.get(key) instanceof Integer)
175 return (Integer) (prefs.get(key));
176 }
177 if (printNamesNotInMap)
178 System.out.println("Settings: Key \"" + key
179 + "\" not in Map, defaulting to \"" + def + "\".");
180 return def;
181 }
182
183 /**
184 * Remove a value
185 *
186 * @param key
187 * Key to value to remove
188 */
189 public void removeValue(String key) {
190 prefs.remove(key);
191 }
192
193}
Note: See TracBrowser for help on using the repository browser.