wneuper@3922
|
1 |
/*
|
wneuper@3922
|
2 |
* Created on Feb 22, 2005
|
wneuper@3922
|
3 |
*
|
wneuper@3922
|
4 |
* @author Alan Krempler
|
wneuper@3922
|
5 |
*/
|
wneuper@3922
|
6 |
|
wneuper@3922
|
7 |
package isac.util.users;
|
wneuper@3922
|
8 |
|
wneuper@3922
|
9 |
import java.io.FileInputStream;
|
wneuper@3922
|
10 |
import java.io.FileNotFoundException;
|
wneuper@3922
|
11 |
import java.io.FileOutputStream;
|
wneuper@3922
|
12 |
import java.io.IOException;
|
wneuper@3922
|
13 |
import java.util.Enumeration;
|
wneuper@3922
|
14 |
import java.util.HashMap;
|
wneuper@3922
|
15 |
import java.util.Map;
|
wneuper@3922
|
16 |
import java.util.Properties;
|
wneuper@3922
|
17 |
|
wneuper@3922
|
18 |
/**
|
wneuper@3922
|
19 |
* Class for storing/retrieving user settings, i.e. settings which can be set
|
wneuper@3922
|
20 |
* manually by the user as opposed to the
|
wneuper@3922
|
21 |
* {@link isac.util.usersettings.UserModel#}, which is an abstraction of the
|
wneuper@3922
|
22 |
* system's expeience with a user and canot be altered manually. As there is no
|
wneuper@3922
|
23 |
* fixed set of user settings, you can regard this class as a simple storage
|
wneuper@3922
|
24 |
* backend for key/value pairs.
|
wneuper@3922
|
25 |
*
|
wneuper@3922
|
26 |
* @author Alan Krempler
|
wneuper@3922
|
27 |
*
|
wneuper@3922
|
28 |
*/
|
wneuper@3922
|
29 |
public class UserSettings extends Properties implements IUserSettings {
|
wneuper@3922
|
30 |
|
wneuper@3922
|
31 |
private HashMap storage_;
|
wneuper@3922
|
32 |
|
wneuper@3922
|
33 |
String userpath_;
|
wneuper@3922
|
34 |
|
wneuper@3922
|
35 |
String username_;
|
wneuper@3922
|
36 |
|
wneuper@3922
|
37 |
public static Map valstr_valint_;
|
wneuper@3922
|
38 |
|
wneuper@3922
|
39 |
/**
|
wneuper@3922
|
40 |
* connect the String-representation of a UserSettings-value with the
|
wneuper@3922
|
41 |
* respective Integer-representation
|
wneuper@3922
|
42 |
*
|
wneuper@3922
|
43 |
* @see isac.uti.users.IUserSettings
|
wneuper@3922
|
44 |
*/
|
wneuper@3922
|
45 |
private HashMap createValstrValint() {
|
wneuper@3922
|
46 |
HashMap m = new HashMap();
|
wneuper@3922
|
47 |
m.put("DUMMY", new Integer(IUserSettings.DUMMY));
|
wneuper@3922
|
48 |
m.put("VAL1_SKIP_SPECIFY_TO_START_SOLVE", new Integer(
|
wneuper@3922
|
49 |
IUserSettings.VAL1_SKIP_SPECIFY_TO_START_SOLVE));
|
wneuper@3922
|
50 |
m.put("VAL1_POP_CALCHEAD_WITH_DESCRIPTIONS", new Integer(
|
wneuper@3922
|
51 |
IUserSettings.VAL1_POP_CALCHEAD_WITH_DESCRIPTIONS));
|
wneuper@3922
|
52 |
m.put("VAL2_FORMULAE_ONLY", new Integer(
|
wneuper@3922
|
53 |
IUserSettings.VAL2_FORMULAE_ONLY));
|
wneuper@3922
|
54 |
m.put("VAL2_FORMULA_TACTIC_FORMULA_", new Integer(
|
wneuper@3922
|
55 |
IUserSettings.VAL2_FORMULA_TACTIC_FORMULA_));
|
wneuper@3922
|
56 |
//m.put("",
|
wneuper@3922
|
57 |
// new Integer(IUserSettings.));
|
wneuper@3922
|
58 |
return m;
|
wneuper@3922
|
59 |
}
|
wneuper@3922
|
60 |
|
wneuper@3922
|
61 |
public UserSettings(String userpath, String username) {
|
wneuper@3922
|
62 |
userpath_ = userpath;
|
wneuper@3922
|
63 |
username_ = username;
|
wneuper@3922
|
64 |
|
wneuper@3922
|
65 |
//load Properties (keystr, valstr) temporarily
|
wneuper@3922
|
66 |
Properties keystr_valstr = new Properties();
|
wneuper@3922
|
67 |
String filename = userpath_ + username + "_settings.txt";
|
wneuper@3922
|
68 |
try {
|
wneuper@3922
|
69 |
FileInputStream propInFile = new FileInputStream(filename);
|
wneuper@3922
|
70 |
keystr_valstr.load(propInFile);
|
wneuper@3922
|
71 |
keystr_valstr.list(System.out);
|
wneuper@3922
|
72 |
} catch (FileNotFoundException e) {
|
wneuper@3922
|
73 |
System.err.println("Can?t find " + filename);
|
wneuper@3922
|
74 |
} catch (IOException e) {
|
wneuper@3922
|
75 |
System.err.println("I/O failed.");
|
wneuper@3922
|
76 |
}
|
wneuper@3922
|
77 |
|
wneuper@3922
|
78 |
//create HashMap (valstr, valint) temporarily
|
wneuper@3922
|
79 |
HashMap valstr_valint = new HashMap();
|
wneuper@3922
|
80 |
valstr_valint = this.createValstrValint();
|
wneuper@3922
|
81 |
|
wneuper@3922
|
82 |
//create HashMap (keystr, \\valstr\\ valint) and store it
|
wneuper@3922
|
83 |
storage_ = new HashMap();
|
wneuper@3922
|
84 |
Enumeration keystrs = keystr_valstr.propertyNames();//WN050311 how
|
wneuper@3922
|
85 |
// better ?
|
wneuper@3922
|
86 |
String keystr = null;
|
wneuper@3922
|
87 |
String valstr = null;
|
wneuper@3922
|
88 |
Object valint = null;
|
wneuper@3922
|
89 |
while (keystrs.hasMoreElements()) {
|
wneuper@3922
|
90 |
keystr = (String) keystrs.nextElement();
|
wneuper@3922
|
91 |
valstr = keystr_valstr.getProperty(keystr);
|
wneuper@3922
|
92 |
valint = valstr_valint.get(valstr);
|
wneuper@3922
|
93 |
storage_.put(keystr, valint);
|
wneuper@3922
|
94 |
}
|
wneuper@3922
|
95 |
}
|
wneuper@3922
|
96 |
|
wneuper@3922
|
97 |
/**
|
wneuper@3922
|
98 |
* @param key
|
wneuper@3922
|
99 |
* A string key
|
wneuper@3922
|
100 |
* @return The string value associated with the key parameter; null, if
|
wneuper@3922
|
101 |
* nothing is stored under this key
|
wneuper@3922
|
102 |
*/
|
wneuper@3922
|
103 |
public int getValue(String key) {
|
wneuper@3922
|
104 |
return ((Integer) storage_.get(key)).intValue();
|
wneuper@3922
|
105 |
}
|
wneuper@3922
|
106 |
|
wneuper@3922
|
107 |
/**
|
wneuper@3922
|
108 |
* @param key
|
wneuper@3922
|
109 |
* The key under which "value" is to be stored. Existing data
|
wneuper@3922
|
110 |
* unde the same key will be replaced.
|
wneuper@3922
|
111 |
* @param value
|
wneuper@3922
|
112 |
* The value to be stored under "key"
|
wneuper@3922
|
113 |
* @return true in case of success, false in case of error, for example if
|
wneuper@3922
|
114 |
* trying to replace stored values without proper permissions
|
wneuper@3922
|
115 |
*/
|
wneuper@3922
|
116 |
public boolean setValue(String key, int value) {
|
wneuper@3922
|
117 |
//TODO.WN050311 storage_.put(key, value);
|
wneuper@3922
|
118 |
return true;
|
wneuper@3922
|
119 |
}
|
wneuper@3922
|
120 |
|
wneuper@3922
|
121 |
//WN050311 just for creation of the first file
|
wneuper@3922
|
122 |
//beware of the fix path !
|
wneuper@3922
|
123 |
public static void main(String[] args) {
|
wneuper@3922
|
124 |
UserSettings s = new UserSettings("isac/xmldata/users/", "x");
|
wneuper@3922
|
125 |
s.setProperty("start_worksheet", "SKIP_SPECIFY_START_SOLVE");
|
wneuper@3922
|
126 |
s.setProperty("enter_specify", "SKIP_SPECIFY_START_SOLVE");
|
wneuper@3922
|
127 |
s.setProperty("next_button", "FORMULAE_ONLY");
|
wneuper@3922
|
128 |
|
wneuper@3922
|
129 |
String filename = "isac/xmldata/users/x_settings.txt";
|
wneuper@3922
|
130 |
try {
|
wneuper@3922
|
131 |
FileOutputStream propOutFile = new FileOutputStream(filename);
|
wneuper@3922
|
132 |
s.store(propOutFile, "static dialog data of user 'x'");
|
wneuper@3922
|
133 |
|
wneuper@3922
|
134 |
} catch (IOException e) {
|
wneuper@3922
|
135 |
System.err.println("I/O failed.");
|
wneuper@3922
|
136 |
}
|
wneuper@3922
|
137 |
}
|
wneuper@3922
|
138 |
|
wneuper@3922
|
139 |
}
|
wneuper@3922
|
140 |
|