001: package snow.utils.storage;
002:
003: import java.util.*;
004: import java.io.*;
005: import java.awt.Component;
006: import java.awt.Font;
007: import java.awt.Color;
008:
009: /** Customized properties (some getters/setters) should remain compatible with Properties.
010: * Is stored in XML format, in a file.
011: */
012: @SuppressWarnings("unchecked")
013: public class AppProperties extends Properties {
014:
015: public AppProperties() {
016: super ();
017: }
018:
019: public void load_XML(File f) {
020: if (!f.exists())
021: return;
022:
023: FileInputStream fis = null;
024: try {
025: fis = new FileInputStream(f);
026: this .loadFromXML(fis);
027: } catch (Exception e) {
028: e.printStackTrace();
029: } finally {
030: FileUtils.closeIgnoringExceptions(fis);
031: }
032: }
033:
034: public void save_XML(File f) {
035: FileOutputStream fos = null;
036: try {
037: fos = new FileOutputStream(f);
038: this .storeToXML(fos, "AppProperties");
039: } catch (Exception e) {
040: e.printStackTrace();
041: } finally {
042: FileUtils.closeIgnoringExceptions(fos);
043: }
044: }
045:
046: public final void loadFromStoredVectorRepresentation(File file) {
047: FileInputStream fis = null;
048: try {
049: fis = new FileInputStream(file);
050: List rep = FileUtils.loadVector(fis);
051: this .createFromVectorRepresentation(rep, true);
052: fis.close();
053: } catch (Exception ex) {
054: ex.printStackTrace();
055: } finally {
056: FileUtils.closeIgnoringExceptions(fis);
057: }
058: }
059:
060: public final void storeToFileAsVectorRepresentation(File file) {
061: try {
062: FileUtils.saveVectorToFile(file, getVectorRepresentation());
063: } catch (Exception ignored) {
064: ignored.printStackTrace();
065: }
066: }
067:
068: //
069: //
070:
071: public void setArrayProperty(String key, List<String> values) {
072: setArrayProperty(key, values.toArray(new String[values.size()]));
073: }
074:
075: public void setArrayProperty(String key, String[] values) {
076: setProperty(key + "length", "" + values.length);
077: for (int i = 0; i < values.length; i++) {
078: setProperty(key + (i), values[i]); //NOT ROBUST :-(, collides if a key ends with a number !!
079: }
080: }
081:
082: /** @return a modifiable list.
083: */
084: public List<String> getArrayProperty(String key,
085: final List<String> defaults) {
086: if (defaults == null) {
087: return getArrayProperty(key, (String[]) null);
088: }
089: return getArrayProperty(key, defaults
090: .toArray(new String[defaults.size()]));
091: }
092:
093: /** @return a modifiable list. Null if not found.
094: */
095: public List<String> getArrayProperty(String key) {
096: return getArrayProperty(key, (String[]) null);
097: }
098:
099: /** @return a modifiable list.
100: */
101: public List<String> getArrayProperty(String key, String[] defaults) {
102: List<String> ap = new ArrayList<String>();
103: String found = getProperty(key + "length", "no");
104: if (found.equals("no")) {
105: if (defaults != null) {
106: ap.addAll(Arrays.asList(defaults));
107: }
108: return ap;
109: }
110: int n = Integer.parseInt(found);
111:
112: String[] props = new String[n];
113: for (int i = 0; i < n; i++) {
114: ap.add(getProperty(key + (i), "oh la la la la"));
115: }
116: return ap;
117: }
118:
119: public void setInteger(String key, int val) {
120: this .put(key, "" + val);
121: }
122:
123: public void setLong(String key, long val) {
124: this .put(key, "" + val);
125: }
126:
127: public void setDouble(String key, double val) {
128: this .put(key, "" + val);
129: }
130:
131: public Font getFont(String key, Font def) {
132: String name = this .getProperty(key + "_name", "?");
133: if (name.equals("?"))
134: return def;
135:
136: int style = this .getInteger(key + "_style", Font.PLAIN);
137: int size = this .getInteger(key + "_size", 12);
138:
139: return new Font(name, style, size);
140: }
141:
142: public Color getColor(String key, Color def) {
143: int r = this .getInteger(key + "_r", -1);
144: if (r == -1)
145: return def;
146: int g = this .getInteger(key + "_g", 0);
147: int b = this .getInteger(key + "_b", 0);
148: int a = this .getInteger(key + "_a", 0);
149: return new Color(r, g, b, a);
150: }
151:
152: public void setColor(String key, Color val) {
153: if (val == null) {
154: this .remove(key + "_r");
155: this .remove(key + "_g");
156: this .remove(key + "_b");
157: this .remove(key + "_a");
158:
159: } else {
160: this .setInteger(key + "_r", val.getRed());
161: this .setInteger(key + "_g", val.getGreen());
162: this .setInteger(key + "_b", val.getBlue());
163: this .setInteger(key + "_a", val.getAlpha());
164: }
165: }
166:
167: public void setFont(String key, Font font) {
168: if (font == null) {
169: this .remove(key + "_name");
170: this .remove(key + "_style");
171: this .remove(key + "_size");
172: } else {
173: this .put(key + "_name", font.getName());
174: this .setInteger(key + "_style", font.getStyle());
175: this .setInteger(key + "_size", font.getSize());
176: }
177: }
178:
179: /** this set the key
180: */
181: public void setStringLCK(String key, String val) {
182: this .put(key.toLowerCase(), val);
183: }
184:
185: public int getInteger(String key, int def) {
186: String found = getProperty(key, "no");
187: if (found.equals("no"))
188: return def;
189: try {
190: int n = Integer.parseInt(found);
191: return n;
192: } catch (Exception e) {
193: return def;
194: }
195: }
196:
197: public long getLong(String key, long def) {
198: String found = getProperty(key, "no");
199: if (found.equals("no"))
200: return def;
201: try {
202: long n = Long.parseLong(found);
203: return n;
204: } catch (Exception e) {
205: return def;
206: }
207: }
208:
209: public double getDouble(String key, double def) {
210: String found = getProperty(key, "no");
211: if (found.equals("no"))
212: return def;
213: try {
214: double n = Double.parseDouble(found);
215: return n;
216: } catch (Exception e) {
217: return def;
218: }
219: }
220:
221: public void setBoolean(String key, boolean val) {
222: this .put(key, (val ? "true" : "false"));
223: }
224:
225: public boolean getBoolean(String key, boolean def) {
226: String found = getProperty(key, "no");
227: if (found.equals("no"))
228: return def;
229: return (found.equals("true"));
230: }
231:
232: /** lowercase key
233: */
234: public String getStringLCK(String key, String def) {
235: String found = getProperty(key.toLowerCase(), "no");
236: if (found.equals("no"))
237: return def;
238: return found;
239: }
240:
241: /** set the component size and location
242: */
243: public synchronized void setComponentSizeFromINIFile(
244: Component comp, String key, int defWidth, int defHeight,
245: int defPosX, int defPosY) {
246: int w = getInteger(key + ".width", defWidth);
247: int h = getInteger(key + ".height", defHeight);
248: int x = getInteger(key + ".posx", defPosX);
249: int y = getInteger(key + ".posy", defPosY);
250:
251: comp.setSize(w, h);
252: comp.setLocation(x, y);
253: }
254:
255: /** save the component size and location
256: */
257: public synchronized void saveComponentSizeInINIFile(Component comp,
258: String key) {
259: this
260: .setInteger(key + ".width", (int) comp.getSize()
261: .getWidth());
262: this .setInteger(key + ".height", (int) comp.getSize()
263: .getHeight());
264: this .setInteger(key + ".posx", (int) comp.getLocation().getX());
265: this .setInteger(key + ".posy", (int) comp.getLocation().getY());
266: }
267:
268: /** set the component location
269: */
270: public synchronized void setComponentLocationFromINIFile(
271: Component comp, String key, int defPosX, int defPosY) {
272: int x = getInteger(key + ".posx", defPosX);
273: int y = getInteger(key + ".posy", defPosY);
274:
275: comp.setLocation(x, y);
276: }
277:
278: /** save the component location
279: */
280: public synchronized void saveComponentLocationInINIFile(
281: Component comp, String key) {
282: this .setInteger(key + ".posx", (int) comp.getLocation().getX());
283: this .setInteger(key + ".posy", (int) comp.getLocation().getY());
284: }
285:
286: public StorageVector getVectorRepresentation() {
287: StorageVector v = new StorageVector();
288: v.add(1); // version
289: v.add(this .size());
290: Enumeration<Object> enum2 = this .keys();
291: while (enum2.hasMoreElements()) {
292: String obj = "" + enum2.nextElement();
293: v.add(obj);
294: v.add(getProperty(obj));
295: }
296: return v;
297: }
298:
299: /** clears and recreate from the passed vector
300: */
301: public void createFromVectorRepresentation(List<Object> v,
302: boolean clearRep) {
303: this .clear();
304:
305: int ver = (Integer) v.get(0);
306: if (ver != 1)
307: throw new RuntimeException("version =" + ver
308: + " not supported");
309: int size = (Integer) v.get(1);
310: for (int i = 0; i < size; i++) {
311: this .setProperty((String) v.get(2 * i + 2), (String) v
312: .get(2 * i + 3));
313: }
314:
315: if (clearRep) {
316: v.clear();
317: }
318: }
319:
320: }
|