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 |
/** |
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 |
* Property representing the default MIDI input driver to be used |
67 |
* when creating new MIDI input device. |
68 |
*/ |
69 |
public final static String DEFAULT_MIDI_DRIVER = "defaultMidiDriver"; |
70 |
|
71 |
/** |
72 |
* Property representing the default audio output driver to be used |
73 |
* when creating new audio output device. |
74 |
*/ |
75 |
public final static String DEFAULT_AUDIO_DRIVER = "defaultAudioDriver"; |
76 |
|
77 |
|
78 |
private final String pathName; |
79 |
private final Preferences userPrefs; |
80 |
|
81 |
/** |
82 |
* Creates a new instance of <code>JSPrefs</code>. |
83 |
* @param pathName The path name of the preferences node. |
84 |
*/ |
85 |
public |
86 |
JSPrefs(String pathName) { |
87 |
super(new Object()); |
88 |
|
89 |
this.pathName = pathName; |
90 |
userPrefs = Preferences.userRoot().node(pathName); |
91 |
} |
92 |
|
93 |
private Preferences |
94 |
user() { return userPrefs; } |
95 |
|
96 |
/** |
97 |
* Gets a string property. |
98 |
* @param name The name of the property. |
99 |
* @return The value of the specified property. |
100 |
* If the property is not set, the return value is <code>null</code>. |
101 |
* @see #getDefaultStringValue |
102 |
*/ |
103 |
public String |
104 |
getStringProperty(String name) { |
105 |
return getStringProperty(name, getDefaultStringValue(name)); |
106 |
} |
107 |
|
108 |
/** |
109 |
* Gets a string property. |
110 |
* @param name The name of the property. |
111 |
* @param defaultValue The value to return if the property is not set. |
112 |
* @return The value of the specified property. |
113 |
*/ |
114 |
public String |
115 |
getStringProperty(String name, String defaultValue) { |
116 |
return user().get(name, defaultValue); |
117 |
} |
118 |
|
119 |
/** |
120 |
* Sets a string property. |
121 |
* @param name The name of the property. |
122 |
* @param s The new value for the specified property. |
123 |
*/ |
124 |
public void |
125 |
setStringProperty(String name, String s) { |
126 |
String oldValue = getStringProperty(name); |
127 |
|
128 |
if(s == null) user().remove(name); |
129 |
else user().put(name, s); |
130 |
|
131 |
firePropertyChange(name, oldValue, s); |
132 |
} |
133 |
|
134 |
/** |
135 |
* Gets the default value for the specified property. |
136 |
* The default value is used when the property is not set. |
137 |
* Override this method to provide custom default values for specific properties. |
138 |
* @name The name of the property whose default value should be obtained. |
139 |
* @return <code>null</code> |
140 |
* @see #getStringProperty(String name) |
141 |
*/ |
142 |
public String |
143 |
getDefaultStringValue(String name) { return null; } |
144 |
|
145 |
/** |
146 |
* Gets a string list property. |
147 |
* @param name The name of the property. |
148 |
* @return The value of the specified property. |
149 |
* If the property is not set, the return value is an empty array. |
150 |
* @see #getDefaultStringListValue |
151 |
*/ |
152 |
public String[] |
153 |
getStringListProperty(String name) { |
154 |
return getStringListProperty(name, getDefaultStringListValue(name)); |
155 |
} |
156 |
|
157 |
/** |
158 |
* Gets a string list property. |
159 |
* @param name The name of the property. |
160 |
* @param defaultValue The value to return if the property is not set. |
161 |
* @return The value of the specified property. |
162 |
*/ |
163 |
public String[] |
164 |
getStringListProperty(String name, String[] defaultValue) { |
165 |
String s = user().get(name, null); |
166 |
if(s == null) return defaultValue; |
167 |
if(s.length() == 0) return new String[0]; |
168 |
|
169 |
BufferedReader br = new BufferedReader(new StringReader(s)); |
170 |
Vector<String> v = new Vector(); |
171 |
|
172 |
try { |
173 |
s = br.readLine(); |
174 |
while(s != null) { |
175 |
v.add(s); |
176 |
s = br.readLine(); |
177 |
} |
178 |
} catch(Exception x) { |
179 |
x.printStackTrace(); |
180 |
} |
181 |
|
182 |
return v.toArray(new String[v.size()]); |
183 |
} |
184 |
|
185 |
/** |
186 |
* Sets a string list property. |
187 |
* Note that the string elements may not contain new lines. |
188 |
* @param name The name of the property. |
189 |
* @param list The new value for the specified property. |
190 |
*/ |
191 |
public void |
192 |
setStringListProperty(String name, String[] list) { |
193 |
String[] oldValue = getStringListProperty(name); |
194 |
|
195 |
if(list == null) user().remove(name); |
196 |
else { |
197 |
StringBuffer sb = new StringBuffer(); |
198 |
for(String s : list) sb.append(s).append("\n"); |
199 |
user().put(name, sb.toString()); |
200 |
} |
201 |
|
202 |
firePropertyChange(name, oldValue, list); |
203 |
} |
204 |
|
205 |
/** |
206 |
* Gets the default value for the specified property. |
207 |
* The default value is used when the property is not set. |
208 |
* Override this method to provide custom default values for specific properties. |
209 |
* @name The name of the property whose default value should be obtained. |
210 |
* @return An empty array. |
211 |
* @see #getStringListProperty(String name) |
212 |
*/ |
213 |
public String[] |
214 |
getDefaultStringListValue(String name) { return new String[0]; } |
215 |
|
216 |
/** |
217 |
* Gets an integer property. |
218 |
* @param name The name of the property. |
219 |
* @return The value of the specified property. |
220 |
* @see #getDefaultIntValue |
221 |
*/ |
222 |
public int |
223 |
getIntProperty(String name) { |
224 |
return getIntProperty(name, getDefaultIntValue(name)); |
225 |
} |
226 |
|
227 |
/** |
228 |
* Gets an integer property. |
229 |
* @param name The name of the property. |
230 |
* @param defaultValue The value to return if the property is not set. |
231 |
* @return The value of the specified property. |
232 |
*/ |
233 |
public int |
234 |
getIntProperty(String name, int defaultValue) { |
235 |
return user().getInt(name, defaultValue); |
236 |
} |
237 |
|
238 |
/** |
239 |
* Gets the default value for the specified property. |
240 |
* The default value is used when the property is not set. |
241 |
* Override this method to provide custom default values for specific properties. |
242 |
* @name The name of the property whose default value should be obtained. |
243 |
* @return <code>0</code> |
244 |
* @see #getIntProperty(String name) |
245 |
*/ |
246 |
public int |
247 |
getDefaultIntValue(String name) { return 0; } |
248 |
|
249 |
/** |
250 |
* Sets an integer property. |
251 |
* @param name The name of the property. |
252 |
* @param i The new value for the specified property. |
253 |
*/ |
254 |
public void |
255 |
setIntProperty(String name, int i) { |
256 |
int oldValue = getIntProperty(name); |
257 |
user().putInt(name, i); |
258 |
firePropertyChange(name, oldValue, i); |
259 |
} |
260 |
|
261 |
|
262 |
/** |
263 |
* Gets a boolean property. |
264 |
* @param name The name of the property. |
265 |
* @return The value of the specified property. |
266 |
* @see #getDefaultBoolValue |
267 |
*/ |
268 |
public boolean |
269 |
getBoolProperty(String name) { |
270 |
return getBoolProperty(name, getDefaultBoolValue(name)); |
271 |
} |
272 |
/** |
273 |
* Gets a boolean property. |
274 |
* @param name The name of the property. |
275 |
* @param defaultValue The value to return if the property is not set. |
276 |
* @return The value of the specified property. |
277 |
*/ |
278 |
public boolean |
279 |
getBoolProperty(String name, boolean defaultValue) { |
280 |
return user().getBoolean(name, defaultValue); |
281 |
} |
282 |
|
283 |
/** |
284 |
* Sets a boolean property. |
285 |
* @param name The name of the property. |
286 |
* @param b The new value for the specified property. |
287 |
*/ |
288 |
public void |
289 |
setBoolProperty(String name, boolean b) { |
290 |
boolean oldValue = getBoolProperty(name); |
291 |
user().putBoolean(name, b); |
292 |
firePropertyChange(name, oldValue, b); |
293 |
} |
294 |
|
295 |
/** |
296 |
* Gets the default value for the specified property. |
297 |
* The default value is used when the property is not set. |
298 |
* Override this method to provide custom default values for specific properties. |
299 |
* @name The name of the property whose default value should be obtained. |
300 |
* @return <code>false</code> |
301 |
* @see #getBoolProperty(String name) |
302 |
*/ |
303 |
public boolean |
304 |
getDefaultBoolValue(String name) { return false; } |
305 |
} |