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