1 |
iliev |
1286 |
/* |
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 |
iliev |
1308 |
|
27 |
|
|
import java.io.BufferedReader; |
28 |
|
|
import java.io.StringReader; |
29 |
|
|
|
30 |
|
|
import java.util.Vector; |
31 |
iliev |
1286 |
import java.util.prefs.Preferences; |
32 |
|
|
|
33 |
|
|
/** |
34 |
|
|
* |
35 |
|
|
* @author Grigor Iliev |
36 |
|
|
*/ |
37 |
|
|
public class JSPrefs extends PropertyChangeSupport { |
38 |
iliev |
1313 |
/** |
39 |
|
|
* Property which specifies whether to apply default |
40 |
|
|
* actions to newly created sampler channels. |
41 |
|
|
*/ |
42 |
|
|
public final static String USE_CHANNEL_DEFAULTS = "samplerChannel.useDefaultActions"; |
43 |
|
|
|
44 |
|
|
/** |
45 |
|
|
* Property representing the default engine to be used when |
46 |
|
|
* new sampler channel is created. The action is taken only if |
47 |
|
|
* <code>USE_CHANNEL_DEFAULTS</code> is <code>true</code>. |
48 |
|
|
*/ |
49 |
|
|
public final static String DEFAULT_ENGINE = "defaultEngine"; |
50 |
|
|
|
51 |
|
|
/** |
52 |
|
|
* Property representing the default MIDI input to be used when |
53 |
|
|
* new sampler channel is created. The action is taken only if |
54 |
|
|
* <code>USE_CHANNEL_DEFAULTS</code> is <code>true</code>. |
55 |
|
|
*/ |
56 |
|
|
public final static String DEFAULT_MIDI_INPUT = "defaultMidiInput"; |
57 |
|
|
|
58 |
|
|
/** |
59 |
|
|
* Property representing the default audio output to be used when |
60 |
|
|
* new sampler channel is created. The action is taken only if |
61 |
|
|
* <code>USE_CHANNEL_DEFAULTS</code> is <code>true</code>. |
62 |
|
|
*/ |
63 |
|
|
public final static String DEFAULT_AUDIO_OUTPUT = "defaultAudioOutput"; |
64 |
|
|
|
65 |
|
|
|
66 |
iliev |
1286 |
private final String pathName; |
67 |
|
|
private final Preferences userPrefs; |
68 |
|
|
|
69 |
|
|
/** |
70 |
|
|
* Creates a new instance of <code>JSPrefs</code>. |
71 |
|
|
* @param pathName The path name of the preferences node. |
72 |
|
|
*/ |
73 |
|
|
public |
74 |
|
|
JSPrefs(String pathName) { |
75 |
|
|
super(new Object()); |
76 |
|
|
|
77 |
|
|
this.pathName = pathName; |
78 |
|
|
userPrefs = Preferences.userRoot().node(pathName); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
private Preferences |
82 |
|
|
user() { return userPrefs; } |
83 |
|
|
|
84 |
|
|
/** |
85 |
|
|
* Gets a string property. |
86 |
|
|
* @param name The name of the property. |
87 |
|
|
* @return The value of the specified property. |
88 |
|
|
* If the property is not set, the return value is <code>null</code>. |
89 |
|
|
* @see #getDefaultStringValue |
90 |
|
|
*/ |
91 |
|
|
public String |
92 |
|
|
getStringProperty(String name) { |
93 |
|
|
return getStringProperty(name, getDefaultStringValue(name)); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* Gets a string property. |
98 |
|
|
* @param name The name of the property. |
99 |
|
|
* @param defaultValue The value to return if the property is not set. |
100 |
|
|
* @return The value of the specified property. |
101 |
|
|
*/ |
102 |
|
|
public String |
103 |
|
|
getStringProperty(String name, String defaultValue) { |
104 |
|
|
return user().get(name, defaultValue); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
/** |
108 |
iliev |
1308 |
* Sets a string property. |
109 |
iliev |
1286 |
* @param name The name of the property. |
110 |
iliev |
1308 |
* @param s The new value for the specified property. |
111 |
iliev |
1286 |
*/ |
112 |
|
|
public void |
113 |
|
|
setStringProperty(String name, String s) { |
114 |
|
|
String oldValue = getStringProperty(name); |
115 |
|
|
|
116 |
|
|
if(s == null) user().remove(name); |
117 |
|
|
else user().put(name, s); |
118 |
|
|
|
119 |
|
|
firePropertyChange(name, oldValue, s); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Gets the default value for the specified property. |
124 |
|
|
* The default value is used when the property is not set. |
125 |
|
|
* Override this method to provide custom default values for specific properties. |
126 |
|
|
* @name The name of the property whose default value should be obtained. |
127 |
|
|
* @return <code>null</code> |
128 |
|
|
* @see #getStringProperty(String name) |
129 |
|
|
*/ |
130 |
|
|
public String |
131 |
|
|
getDefaultStringValue(String name) { return null; } |
132 |
|
|
|
133 |
|
|
/** |
134 |
iliev |
1308 |
* Gets a string list property. |
135 |
|
|
* @param name The name of the property. |
136 |
|
|
* @return The value of the specified property. |
137 |
|
|
* If the property is not set, the return value is an empty array. |
138 |
|
|
* @see #getDefaultStringListValue |
139 |
|
|
*/ |
140 |
|
|
public String[] |
141 |
|
|
getStringListProperty(String name) { |
142 |
|
|
return getStringListProperty(name, getDefaultStringListValue(name)); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
/** |
146 |
|
|
* Gets a string list property. |
147 |
|
|
* @param name The name of the property. |
148 |
|
|
* @param defaultValue The value to return if the property is not set. |
149 |
|
|
* @return The value of the specified property. |
150 |
|
|
*/ |
151 |
|
|
public String[] |
152 |
|
|
getStringListProperty(String name, String[] defaultValue) { |
153 |
|
|
String s = user().get(name, null); |
154 |
|
|
if(s == null) return defaultValue; |
155 |
|
|
if(s.length() == 0) return new String[0]; |
156 |
|
|
|
157 |
|
|
BufferedReader br = new BufferedReader(new StringReader(s)); |
158 |
|
|
Vector<String> v = new Vector(); |
159 |
|
|
|
160 |
|
|
try { |
161 |
|
|
s = br.readLine(); |
162 |
|
|
while(s != null) { |
163 |
|
|
v.add(s); |
164 |
|
|
s = br.readLine(); |
165 |
|
|
} |
166 |
|
|
} catch(Exception x) { |
167 |
|
|
x.printStackTrace(); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
return v.toArray(new String[v.size()]); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
/** |
174 |
|
|
* Sets a string list property. |
175 |
|
|
* Note that the string elements may not contain new lines. |
176 |
|
|
* @param name The name of the property. |
177 |
|
|
* @param list The new value for the specified property. |
178 |
|
|
*/ |
179 |
|
|
public void |
180 |
|
|
setStringListProperty(String name, String[] list) { |
181 |
|
|
String[] oldValue = getStringListProperty(name); |
182 |
|
|
|
183 |
|
|
if(list == null) user().remove(name); |
184 |
|
|
else { |
185 |
|
|
StringBuffer sb = new StringBuffer(); |
186 |
|
|
for(String s : list) sb.append(s).append("\n"); |
187 |
|
|
user().put(name, sb.toString()); |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
firePropertyChange(name, oldValue, list); |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
/** |
194 |
|
|
* Gets the default value for the specified property. |
195 |
|
|
* The default value is used when the property is not set. |
196 |
|
|
* Override this method to provide custom default values for specific properties. |
197 |
|
|
* @name The name of the property whose default value should be obtained. |
198 |
|
|
* @return An empty array. |
199 |
|
|
* @see #getStringListProperty(String name) |
200 |
|
|
*/ |
201 |
|
|
public String[] |
202 |
|
|
getDefaultStringListValue(String name) { return new String[0]; } |
203 |
|
|
|
204 |
|
|
/** |
205 |
iliev |
1286 |
* Gets an integer property. |
206 |
|
|
* @param name The name of the property. |
207 |
|
|
* @return The value of the specified property. |
208 |
|
|
* @see #getDefaultIntValue |
209 |
|
|
*/ |
210 |
|
|
public int |
211 |
|
|
getIntProperty(String name) { |
212 |
|
|
return getIntProperty(name, getDefaultIntValue(name)); |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/** |
216 |
|
|
* Gets an integer property. |
217 |
|
|
* @param name The name of the property. |
218 |
|
|
* @param defaultValue The value to return if the property is not set. |
219 |
|
|
* @return The value of the specified property. |
220 |
|
|
*/ |
221 |
|
|
public int |
222 |
|
|
getIntProperty(String name, int defaultValue) { |
223 |
|
|
return user().getInt(name, defaultValue); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
/** |
227 |
|
|
* Gets the default value for the specified property. |
228 |
|
|
* The default value is used when the property is not set. |
229 |
|
|
* Override this method to provide custom default values for specific properties. |
230 |
|
|
* @name The name of the property whose default value should be obtained. |
231 |
|
|
* @return <code>0</code> |
232 |
|
|
* @see #getIntProperty(String name) |
233 |
|
|
*/ |
234 |
|
|
public int |
235 |
|
|
getDefaultIntValue(String name) { return 0; } |
236 |
|
|
|
237 |
|
|
/** |
238 |
|
|
* Sets an integer property. |
239 |
|
|
* @param name The name of the property. |
240 |
|
|
* @param i The new value for the specified property. |
241 |
|
|
*/ |
242 |
|
|
public void |
243 |
|
|
setIntProperty(String name, int i) { |
244 |
|
|
int oldValue = getIntProperty(name); |
245 |
|
|
user().putInt(name, i); |
246 |
|
|
firePropertyChange(name, oldValue, i); |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
|
250 |
|
|
/** |
251 |
|
|
* Gets a boolean property. |
252 |
|
|
* @param name The name of the property. |
253 |
|
|
* @return The value of the specified property. |
254 |
|
|
* @see #getDefaultBoolValue |
255 |
|
|
*/ |
256 |
|
|
public boolean |
257 |
|
|
getBoolProperty(String name) { |
258 |
|
|
return getBoolProperty(name, getDefaultBoolValue(name)); |
259 |
|
|
} |
260 |
|
|
/** |
261 |
|
|
* Gets a boolean property. |
262 |
|
|
* @param name The name of the property. |
263 |
|
|
* @param defaultValue The value to return if the property is not set. |
264 |
|
|
* @return The value of the specified property. |
265 |
|
|
*/ |
266 |
|
|
public boolean |
267 |
|
|
getBoolProperty(String name, boolean defaultValue) { |
268 |
|
|
return user().getBoolean(name, defaultValue); |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
/** |
272 |
|
|
* Sets a boolean property. |
273 |
|
|
* @param name The name of the property. |
274 |
|
|
* @param b The new value for the specified property. |
275 |
|
|
*/ |
276 |
|
|
public void |
277 |
|
|
setBoolProperty(String name, boolean b) { |
278 |
|
|
boolean oldValue = getBoolProperty(name); |
279 |
|
|
user().putBoolean(name, b); |
280 |
|
|
firePropertyChange(name, oldValue, b); |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
/** |
284 |
|
|
* Gets the default value for the specified property. |
285 |
|
|
* The default value is used when the property is not set. |
286 |
|
|
* Override this method to provide custom default values for specific properties. |
287 |
|
|
* @name The name of the property whose default value should be obtained. |
288 |
|
|
* @return <code>false</code> |
289 |
|
|
* @see #getBoolProperty(String name) |
290 |
|
|
*/ |
291 |
|
|
public boolean |
292 |
|
|
getDefaultBoolValue(String name) { return false; } |
293 |
|
|
} |