1 |
/* |
2 |
* JSampler - a java front-end for LinuxSampler |
3 |
* |
4 |
* Copyright (C) 2005-2008 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 MIDI instrument maps |
96 |
* should be included in the session script. |
97 |
*/ |
98 |
public final static String EXPORT_MIDI_MAPS_TO_SESSION_SCRIPT = "exportMidiMapsToSessionScript"; |
99 |
|
100 |
/** |
101 |
* Property which specifies whether to set the MIDI instrument loading |
102 |
* in background when exporting MIDI instrument maps to LSCP script. |
103 |
*/ |
104 |
public final static String LOAD_MIDI_INSTRUMENTS_IN_BACKGROUND = "loadMidiInstrumentsInBackground"; |
105 |
|
106 |
/** |
107 |
* Property which specifies whether the user should manually select a server to connect on startup. |
108 |
*/ |
109 |
public final static String MANUAL_SERVER_SELECT_ON_STARTUP = "manualServerSelectOnStartup"; |
110 |
|
111 |
/** |
112 |
* Property which specifies whether the backend should be launched by jsampler |
113 |
* if it is not yet started, when trying to connect locally - 127.0.0.1 or localhost. |
114 |
*/ |
115 |
public final static String LAUNCH_BACKEND_LOCALLY = "launchBackednLocally"; |
116 |
|
117 |
/** |
118 |
* Property which specifies the command to be used to start the backend. |
119 |
*/ |
120 |
public final static String BACKEND_LAUNCH_COMMAND = "backendLaunchCommand"; |
121 |
|
122 |
/** |
123 |
* Property which specifies whether the backend should be launched by jsampler |
124 |
* if it is not yet started, when trying to connect locally - 127.0.0.1 or localhost. |
125 |
*/ |
126 |
public final static String BACKEND_LAUNCH_DELAY = "backendLaunchDelay"; |
127 |
|
128 |
/** |
129 |
* Integer property which provides the index of the server to connect on startup. |
130 |
*/ |
131 |
public final static String SERVER_INDEX = "serverIndex"; |
132 |
|
133 |
/** |
134 |
* Property specifying the MIDI bank numbering, whether |
135 |
* the index of the first MIDI bank is 0 or 1 (zero-based or one-based). |
136 |
*/ |
137 |
public final static String FIRST_MIDI_BANK_NUMBER = "firstMidiBankNumber"; |
138 |
|
139 |
/** |
140 |
* Property specifying the MIDI program numbering, whether |
141 |
* the index of the first MIDI program is 0 or 1 (zero-based or one-based). |
142 |
*/ |
143 |
public final static String FIRST_MIDI_PROGRAM_NUMBER = "firstMidiProgramNumber"; |
144 |
|
145 |
/** Property representing the socket read timeout (in seconds). */ |
146 |
public final static String SOCKET_READ_TIMEOUT = "socketReadTimeout"; |
147 |
|
148 |
|
149 |
private final String pathName; |
150 |
private final Preferences userPrefs; |
151 |
|
152 |
/** |
153 |
* Creates a new instance of <code>JSPrefs</code>. |
154 |
* @param pathName The path name of the preferences node. |
155 |
*/ |
156 |
public |
157 |
JSPrefs(String pathName) { |
158 |
super(new Object()); |
159 |
|
160 |
this.pathName = pathName; |
161 |
userPrefs = Preferences.userRoot().node(pathName); |
162 |
} |
163 |
|
164 |
private Preferences |
165 |
user() { return userPrefs; } |
166 |
|
167 |
/** |
168 |
* Gets a string property. |
169 |
* @param name The name of the property. |
170 |
* @return The value of the specified property. |
171 |
* If the property is not set, the return value is <code>null</code>. |
172 |
* @see #getDefaultStringValue |
173 |
*/ |
174 |
public String |
175 |
getStringProperty(String name) { |
176 |
return getStringProperty(name, getDefaultStringValue(name)); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Gets a string property. |
181 |
* @param name The name of the property. |
182 |
* @param defaultValue The value to return if the property is not set. |
183 |
* @return The value of the specified property. |
184 |
*/ |
185 |
public String |
186 |
getStringProperty(String name, String defaultValue) { |
187 |
return user().get(name, defaultValue); |
188 |
} |
189 |
|
190 |
/** |
191 |
* Sets a string property. |
192 |
* @param name The name of the property. |
193 |
* @param s The new value for the specified property. |
194 |
*/ |
195 |
public void |
196 |
setStringProperty(String name, String s) { |
197 |
String oldValue = getStringProperty(name); |
198 |
|
199 |
if(s == null) user().remove(name); |
200 |
else user().put(name, s); |
201 |
|
202 |
firePropertyChange(name, oldValue, s); |
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 |
* @param name The name of the property whose default value should be obtained. |
210 |
* @return <code>null</code> |
211 |
* @see #getStringProperty(String name) |
212 |
*/ |
213 |
public String |
214 |
getDefaultStringValue(String name) { |
215 |
if(BACKEND_LAUNCH_COMMAND.equals(name)) return "linuxsampler"; |
216 |
return null; |
217 |
} |
218 |
|
219 |
/** |
220 |
* Gets a string list property. |
221 |
* @param name The name of the property. |
222 |
* @return The value of the specified property. |
223 |
* If the property is not set, the return value is an empty array. |
224 |
* @see #getDefaultStringListValue |
225 |
*/ |
226 |
public String[] |
227 |
getStringListProperty(String name) { |
228 |
return getStringListProperty(name, getDefaultStringListValue(name)); |
229 |
} |
230 |
|
231 |
/** |
232 |
* Gets a string list property. |
233 |
* @param name The name of the property. |
234 |
* @param defaultValue The value to return if the property is not set. |
235 |
* @return The value of the specified property. |
236 |
*/ |
237 |
public String[] |
238 |
getStringListProperty(String name, String[] defaultValue) { |
239 |
String s = user().get(name, null); |
240 |
if(s == null) return defaultValue; |
241 |
if(s.length() == 0) return new String[0]; |
242 |
|
243 |
BufferedReader br = new BufferedReader(new StringReader(s)); |
244 |
Vector<String> v = new Vector(); |
245 |
|
246 |
try { |
247 |
s = br.readLine(); |
248 |
while(s != null) { |
249 |
v.add(s); |
250 |
s = br.readLine(); |
251 |
} |
252 |
} catch(Exception x) { |
253 |
x.printStackTrace(); |
254 |
} |
255 |
|
256 |
return v.toArray(new String[v.size()]); |
257 |
} |
258 |
|
259 |
/** |
260 |
* Sets a string list property. |
261 |
* Note that the string elements may not contain new lines. |
262 |
* @param name The name of the property. |
263 |
* @param list The new value for the specified property. |
264 |
*/ |
265 |
public void |
266 |
setStringListProperty(String name, String[] list) { |
267 |
String[] oldValue = getStringListProperty(name); |
268 |
|
269 |
if(list == null) user().remove(name); |
270 |
else { |
271 |
StringBuffer sb = new StringBuffer(); |
272 |
for(String s : list) sb.append(s).append("\n"); |
273 |
user().put(name, sb.toString()); |
274 |
} |
275 |
|
276 |
firePropertyChange(name, oldValue, list); |
277 |
} |
278 |
|
279 |
/** |
280 |
* Gets the default value for the specified property. |
281 |
* The default value is used when the property is not set. |
282 |
* Override this method to provide custom default values for specific properties. |
283 |
* @param name The name of the property whose default value should be obtained. |
284 |
* @return An empty array. |
285 |
* @see #getStringListProperty(String name) |
286 |
*/ |
287 |
public String[] |
288 |
getDefaultStringListValue(String name) { return new String[0]; } |
289 |
|
290 |
/** |
291 |
* Gets an integer property. |
292 |
* @param name The name of the property. |
293 |
* @return The value of the specified property. |
294 |
* @see #getDefaultIntValue |
295 |
*/ |
296 |
public int |
297 |
getIntProperty(String name) { |
298 |
return getIntProperty(name, getDefaultIntValue(name)); |
299 |
} |
300 |
|
301 |
/** |
302 |
* Gets an integer property. |
303 |
* @param name The name of the property. |
304 |
* @param defaultValue The value to return if the property is not set. |
305 |
* @return The value of the specified property. |
306 |
*/ |
307 |
public int |
308 |
getIntProperty(String name, int defaultValue) { |
309 |
return user().getInt(name, defaultValue); |
310 |
} |
311 |
|
312 |
/** |
313 |
* Gets the default value for the specified property. |
314 |
* The default value is used when the property is not set. |
315 |
* Override this method to provide custom default values for specific properties. |
316 |
* @param name The name of the property whose default value should be obtained. |
317 |
* @return <code>0</code> |
318 |
* @see #getIntProperty(String name) |
319 |
*/ |
320 |
public int |
321 |
getDefaultIntValue(String name) { |
322 |
if(SOCKET_READ_TIMEOUT.equals(name)) return 90; |
323 |
if(FIRST_MIDI_BANK_NUMBER.equals(name)) return 1; |
324 |
if(FIRST_MIDI_PROGRAM_NUMBER.equals(name)) return 1; |
325 |
if(BACKEND_LAUNCH_DELAY.equals(name)) return 3; |
326 |
return 0; |
327 |
} |
328 |
|
329 |
/** |
330 |
* Sets an integer property. |
331 |
* @param name The name of the property. |
332 |
* @param i The new value for the specified property. |
333 |
*/ |
334 |
public void |
335 |
setIntProperty(String name, int i) { |
336 |
int oldValue = getIntProperty(name); |
337 |
user().putInt(name, i); |
338 |
firePropertyChange(name, oldValue, i); |
339 |
} |
340 |
|
341 |
|
342 |
/** |
343 |
* Gets a boolean property. |
344 |
* @param name The name of the property. |
345 |
* @return The value of the specified property. |
346 |
* @see #getDefaultBoolValue |
347 |
*/ |
348 |
public boolean |
349 |
getBoolProperty(String name) { |
350 |
return getBoolProperty(name, getDefaultBoolValue(name)); |
351 |
} |
352 |
/** |
353 |
* Gets a boolean property. |
354 |
* @param name The name of the property. |
355 |
* @param defaultValue The value to return if the property is not set. |
356 |
* @return The value of the specified property. |
357 |
*/ |
358 |
public boolean |
359 |
getBoolProperty(String name, boolean defaultValue) { |
360 |
return user().getBoolean(name, defaultValue); |
361 |
} |
362 |
|
363 |
/** |
364 |
* Sets a boolean property. |
365 |
* @param name The name of the property. |
366 |
* @param b The new value for the specified property. |
367 |
*/ |
368 |
public void |
369 |
setBoolProperty(String name, boolean b) { |
370 |
boolean oldValue = getBoolProperty(name); |
371 |
user().putBoolean(name, b); |
372 |
firePropertyChange(name, oldValue, b); |
373 |
} |
374 |
|
375 |
/** |
376 |
* Gets the default value for the specified property. |
377 |
* The default value is used when the property is not set. |
378 |
* Override this method to provide custom default values for specific properties. |
379 |
* @param name The name of the property whose default value should be obtained. |
380 |
* @see #getBoolProperty(String name) |
381 |
*/ |
382 |
public boolean |
383 |
getDefaultBoolValue(String name) { |
384 |
if(VOL_MEASUREMENT_UNIT_DECIBEL.equals(name)) return true; |
385 |
if(EXPORT_MIDI_MAPS_TO_SESSION_SCRIPT.equals(name)) return true; |
386 |
if(LOAD_MIDI_INSTRUMENTS_IN_BACKGROUND.equals(name)) return true; |
387 |
if(LAUNCH_BACKEND_LOCALLY.equals(name)) return true; |
388 |
return false; |
389 |
} |
390 |
} |