Initial revision
[old-projects.git] / ekit / com / swabunga / spell / engine / PropertyConfiguration.java
1 package com.swabunga.spell.engine;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.URL;
9 import java.util.Properties;
10
11
12 /**
13 * @author aim4min
14 *
15 * To change this generated comment edit the template variable "typecomment":
16 * Window>Preferences>Java>Templates.
17 * To enable and disable the creation of type comments go to
18 * Window>Preferences>Java>Code Generation.
19 */
20 public class PropertyConfiguration extends Configuration {
21
22 public Properties prop;
23 public URL filename;
24
25 public PropertyConfiguration() {
26 prop = new Properties();
27 try {
28 /*
29 //original initalization code
30 filename = getClass().getClassLoader().getResource("com/swabunga/spell/engine/configuration.properties");
31 InputStream in = filename.openStream();
32 prop.load(in);
33 */
34 // new constructor code so that this works within JAR files (Howard Kistler)
35 InputStream is = this.getClass().getResourceAsStream("configuration.properties");
36 prop.load(is);
37 } catch (Exception e) {
38 System.out.println("Could not load Properties file :\n" + e);
39 }
40 }
41
42 /**
43 * @see com.swabunga.spell.engine.Configuration#getBoolean(String)
44 */
45 public boolean getBoolean(String key) {
46 // return Boolean.getBoolean(prop.getProperty(key));
47 // original breaks inside applets due to security, this version works fine (Howard Kistler)
48 return prop.getProperty(key).equalsIgnoreCase("true");
49 }
50
51 /**
52 * @see com.swabunga.spell.engine.Configuration#getInteger(String)
53 */
54 public int getInteger(String key) {
55 return Integer.parseInt(prop.getProperty(key));
56 }
57
58 /**
59 * @see com.swabunga.spell.engine.Configuration#setBoolean(String, boolean)
60 */
61 public void setBoolean(String key, boolean value) {
62 String string = null;
63 if (value)
64 string = "true";
65 else
66 string = "false";
67
68 prop.setProperty(key, string);
69 save();
70 }
71
72 /**
73 * @see com.swabunga.spell.engine.Configuration#setInteger(String, int)
74 */
75 public void setInteger(String key, int value) {
76 prop.setProperty(key,Integer.toString(value));
77 save();
78 }
79
80 public void save() {
81 try {
82 File file = new File(filename.getFile());
83 FileOutputStream fout = new FileOutputStream(file);
84 prop.store(fout,"HEADER");
85 } catch (FileNotFoundException e) {
86 } catch (IOException e) {
87 }
88 }
89
90 }