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