1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2007 Grigor Iliev <grigor@grigoriliev.com> |
5 |
* |
6 |
* This file is part of JSampler. |
7 |
* |
8 |
* JSampler is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License version 2 |
10 |
* as published by the Free Software Foundation. |
11 |
* |
12 |
* JSampler is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with JSampler; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
* MA 02111-1307 USA |
21 |
*/ |
22 |
|
23 |
package org.jsampler; |
24 |
|
25 |
import java.beans.PropertyChangeSupport; |
26 |
|
27 |
import java.io.BufferedReader; |
28 |
import java.io.StringReader; |
29 |
|
30 |
import java.util.Vector; |
31 |
import java.util.prefs.Preferences; |
32 |
|
33 |
/** |
34 |
* |
35 |
* @author Grigor Iliev |
36 |
*/ |
37 |
public class JSPrefs extends PropertyChangeSupport { |
38 |
private final String pathName; |
39 |
private final Preferences userPrefs; |
40 |
|
41 |
/** |
42 |
* Creates a new instance of <code>JSPrefs</code>. |
43 |
* @param pathName The path name of the preferences node. |
44 |
*/ |
45 |
public |
46 |
JSPrefs(String pathName) { |
47 |
super(new Object()); |
48 |
|
49 |
this.pathName = pathName; |
50 |
userPrefs = Preferences.userRoot().node(pathName); |
51 |
} |
52 |
|
53 |
private Preferences |
54 |
user() { return userPrefs; } |
55 |
|
56 |
/** |
57 |
* Gets a string property. |
58 |
* @param name The name of the property. |
59 |
* @return The value of the specified property. |
60 |
* If the property is not set, the return value is <code>null</code>. |
61 |
* @see #getDefaultStringValue |
62 |
*/ |
63 |
public String |
64 |
getStringProperty(String name) { |
65 |
return getStringProperty(name, getDefaultStringValue(name)); |
66 |
} |
67 |
|
68 |
/** |
69 |
* Gets a string property. |
70 |
* @param name The name of the property. |
71 |
* @param defaultValue The value to return if the property is not set. |
72 |
* @return The value of the specified property. |
73 |
*/ |
74 |
public String |
75 |
getStringProperty(String name, String defaultValue) { |
76 |
return user().get(name, defaultValue); |
77 |
} |
78 |
|
79 |
/** |
80 |
* Sets a string property. |
81 |
* @param name The name of the property. |
82 |
* @param s The new value for the specified property. |
83 |
*/ |
84 |
public void |
85 |
setStringProperty(String name, String s) { |
86 |
String oldValue = getStringProperty(name); |
87 |
|
88 |
if(s == null) user().remove(name); |
89 |
else user().put(name, s); |
90 |
|
91 |
firePropertyChange(name, oldValue, s); |
92 |
} |
93 |
|
94 |
/** |
95 |
* Gets the default value for the specified property. |
96 |
* The default value is used when the property is not set. |
97 |
* Override this method to provide custom default values for specific properties. |
98 |
* @name The name of the property whose default value should be obtained. |
99 |
* @return <code>null</code> |
100 |
* @see #getStringProperty(String name) |
101 |
*/ |
102 |
public String |
103 |
getDefaultStringValue(String name) { return null; } |
104 |
|
105 |
/** |
106 |
* Gets a string list property. |
107 |
* @param name The name of the property. |
108 |
* @return The value of the specified property. |
109 |
* If the property is not set, the return value is an empty array. |
110 |
* @see #getDefaultStringListValue |
111 |
*/ |
112 |
public String[] |
113 |
getStringListProperty(String name) { |
114 |
return getStringListProperty(name, getDefaultStringListValue(name)); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Gets a string list property. |
119 |
* @param name The name of the property. |
120 |
* @param defaultValue The value to return if the property is not set. |
121 |
* @return The value of the specified property. |
122 |
*/ |
123 |
public String[] |
124 |
getStringListProperty(String name, String[] defaultValue) { |
125 |
String s = user().get(name, null); |
126 |
if(s == null) return defaultValue; |
127 |
if(s.length() == 0) return new String[0]; |
128 |
|
129 |
BufferedReader br = new BufferedReader(new StringReader(s)); |
130 |
Vector<String> v = new Vector(); |
131 |
|
132 |
try { |
133 |
s = br.readLine(); |
134 |
while(s != null) { |
135 |
v.add(s); |
136 |
s = br.readLine(); |
137 |
} |
138 |
} catch(Exception x) { |
139 |
x.printStackTrace(); |
140 |
} |
141 |
|
142 |
return v.toArray(new String[v.size()]); |
143 |
} |
144 |
|
145 |
/** |
146 |
* Sets a string list property. |
147 |
* Note that the string elements may not contain new lines. |
148 |
* @param name The name of the property. |
149 |
* @param list The new value for the specified property. |
150 |
*/ |
151 |
public void |
152 |
setStringListProperty(String name, String[] list) { |
153 |
String[] oldValue = getStringListProperty(name); |
154 |
|
155 |
if(list == null) user().remove(name); |
156 |
else { |
157 |
StringBuffer sb = new StringBuffer(); |
158 |
for(String s : list) sb.append(s).append("\n"); |
159 |
user().put(name, sb.toString()); |
160 |
} |
161 |
|
162 |
firePropertyChange(name, oldValue, list); |
163 |
} |
164 |
|
165 |
/** |
166 |
* Gets the default value for the specified property. |
167 |
* The default value is used when the property is not set. |
168 |
* Override this method to provide custom default values for specific properties. |
169 |
* @name The name of the property whose default value should be obtained. |
170 |
* @return An empty array. |
171 |
* @see #getStringListProperty(String name) |
172 |
*/ |
173 |
public String[] |
174 |
getDefaultStringListValue(String name) { return new String[0]; } |
175 |
|
176 |
/** |
177 |
* Gets an integer property. |
178 |
* @param name The name of the property. |
179 |
* @return The value of the specified property. |
180 |
* @see #getDefaultIntValue |
181 |
*/ |
182 |
public int |
183 |
getIntProperty(String name) { |
184 |
return getIntProperty(name, getDefaultIntValue(name)); |
185 |
} |
186 |
|
187 |
/** |
188 |
* Gets an integer property. |
189 |
* @param name The name of the property. |
190 |
* @param defaultValue The value to return if the property is not set. |
191 |
* @return The value of the specified property. |
192 |
*/ |
193 |
public int |
194 |
getIntProperty(String name, int defaultValue) { |
195 |
return user().getInt(name, defaultValue); |
196 |
} |
197 |
|
198 |
/** |
199 |
* Gets the default value for the specified property. |
200 |
* The default value is used when the property is not set. |
201 |
* Override this method to provide custom default values for specific properties. |
202 |
* @name The name of the property whose default value should be obtained. |
203 |
* @return <code>0</code> |
204 |
* @see #getIntProperty(String name) |
205 |
*/ |
206 |
public int |
207 |
getDefaultIntValue(String name) { return 0; } |
208 |
|
209 |
/** |
210 |
* Sets an integer property. |
211 |
* @param name The name of the property. |
212 |
* @param i The new value for the specified property. |
213 |
*/ |
214 |
public void |
215 |
setIntProperty(String name, int i) { |
216 |
int oldValue = getIntProperty(name); |
217 |
user().putInt(name, i); |
218 |
firePropertyChange(name, oldValue, i); |
219 |
} |
220 |
|
221 |
|
222 |
/** |
223 |
* Gets a boolean property. |
224 |
* @param name The name of the property. |
225 |
* @return The value of the specified property. |
226 |
* @see #getDefaultBoolValue |
227 |
*/ |
228 |
public boolean |
229 |
getBoolProperty(String name) { |
230 |
return getBoolProperty(name, getDefaultBoolValue(name)); |
231 |
} |
232 |
/** |
233 |
* Gets a boolean property. |
234 |
* @param name The name of the property. |
235 |
* @param defaultValue The value to return if the property is not set. |
236 |
* @return The value of the specified property. |
237 |
*/ |
238 |
public boolean |
239 |
getBoolProperty(String name, boolean defaultValue) { |
240 |
return user().getBoolean(name, defaultValue); |
241 |
} |
242 |
|
243 |
/** |
244 |
* Sets a boolean property. |
245 |
* @param name The name of the property. |
246 |
* @param b The new value for the specified property. |
247 |
*/ |
248 |
public void |
249 |
setBoolProperty(String name, boolean b) { |
250 |
boolean oldValue = getBoolProperty(name); |
251 |
user().putBoolean(name, b); |
252 |
firePropertyChange(name, oldValue, b); |
253 |
} |
254 |
|
255 |
/** |
256 |
* Gets the default value for the specified property. |
257 |
* The default value is used when the property is not set. |
258 |
* Override this method to provide custom default values for specific properties. |
259 |
* @name The name of the property whose default value should be obtained. |
260 |
* @return <code>false</code> |
261 |
* @see #getBoolProperty(String name) |
262 |
*/ |
263 |
public boolean |
264 |
getDefaultBoolValue(String name) { return false; } |
265 |
} |