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 |
import java.util.prefs.Preferences; |
27 |
|
28 |
/** |
29 |
* |
30 |
* @author Grigor Iliev |
31 |
*/ |
32 |
public class JSPrefs extends PropertyChangeSupport { |
33 |
private final String pathName; |
34 |
private final Preferences userPrefs; |
35 |
|
36 |
/** |
37 |
* Creates a new instance of <code>JSPrefs</code>. |
38 |
* @param pathName The path name of the preferences node. |
39 |
*/ |
40 |
public |
41 |
JSPrefs(String pathName) { |
42 |
super(new Object()); |
43 |
|
44 |
this.pathName = pathName; |
45 |
userPrefs = Preferences.userRoot().node(pathName); |
46 |
} |
47 |
|
48 |
private Preferences |
49 |
user() { return userPrefs; } |
50 |
|
51 |
/** |
52 |
* Gets a string property. |
53 |
* @param name The name of the property. |
54 |
* @return The value of the specified property. |
55 |
* If the property is not set, the return value is <code>null</code>. |
56 |
* @see #getDefaultStringValue |
57 |
*/ |
58 |
public String |
59 |
getStringProperty(String name) { |
60 |
return getStringProperty(name, getDefaultStringValue(name)); |
61 |
} |
62 |
|
63 |
/** |
64 |
* Gets a string property. |
65 |
* @param name The name of the property. |
66 |
* @param defaultValue The value to return if the property is not set. |
67 |
* @return The value of the specified property. |
68 |
*/ |
69 |
public String |
70 |
getStringProperty(String name, String defaultValue) { |
71 |
return user().get(name, defaultValue); |
72 |
} |
73 |
|
74 |
/** |
75 |
* Sets an integer property. |
76 |
* @param name The name of the property. |
77 |
* @param i The new value for the specified property. |
78 |
*/ |
79 |
public void |
80 |
setStringProperty(String name, String s) { |
81 |
String oldValue = getStringProperty(name); |
82 |
|
83 |
if(s == null) user().remove(name); |
84 |
else user().put(name, s); |
85 |
|
86 |
firePropertyChange(name, oldValue, s); |
87 |
} |
88 |
|
89 |
/** |
90 |
* Gets the default value for the specified property. |
91 |
* The default value is used when the property is not set. |
92 |
* Override this method to provide custom default values for specific properties. |
93 |
* @name The name of the property whose default value should be obtained. |
94 |
* @return <code>null</code> |
95 |
* @see #getStringProperty(String name) |
96 |
*/ |
97 |
public String |
98 |
getDefaultStringValue(String name) { return null; } |
99 |
|
100 |
/** |
101 |
* Gets an integer property. |
102 |
* @param name The name of the property. |
103 |
* @return The value of the specified property. |
104 |
* @see #getDefaultIntValue |
105 |
*/ |
106 |
public int |
107 |
getIntProperty(String name) { |
108 |
return getIntProperty(name, getDefaultIntValue(name)); |
109 |
} |
110 |
|
111 |
/** |
112 |
* Gets an integer property. |
113 |
* @param name The name of the property. |
114 |
* @param defaultValue The value to return if the property is not set. |
115 |
* @return The value of the specified property. |
116 |
*/ |
117 |
public int |
118 |
getIntProperty(String name, int defaultValue) { |
119 |
return user().getInt(name, defaultValue); |
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>0</code> |
128 |
* @see #getIntProperty(String name) |
129 |
*/ |
130 |
public int |
131 |
getDefaultIntValue(String name) { return 0; } |
132 |
|
133 |
/** |
134 |
* Sets an integer property. |
135 |
* @param name The name of the property. |
136 |
* @param i The new value for the specified property. |
137 |
*/ |
138 |
public void |
139 |
setIntProperty(String name, int i) { |
140 |
int oldValue = getIntProperty(name); |
141 |
user().putInt(name, i); |
142 |
firePropertyChange(name, oldValue, i); |
143 |
} |
144 |
|
145 |
|
146 |
/** |
147 |
* Gets a boolean property. |
148 |
* @param name The name of the property. |
149 |
* @return The value of the specified property. |
150 |
* @see #getDefaultBoolValue |
151 |
*/ |
152 |
public boolean |
153 |
getBoolProperty(String name) { |
154 |
return getBoolProperty(name, getDefaultBoolValue(name)); |
155 |
} |
156 |
/** |
157 |
* Gets a boolean property. |
158 |
* @param name The name of the property. |
159 |
* @param defaultValue The value to return if the property is not set. |
160 |
* @return The value of the specified property. |
161 |
*/ |
162 |
public boolean |
163 |
getBoolProperty(String name, boolean defaultValue) { |
164 |
return user().getBoolean(name, defaultValue); |
165 |
} |
166 |
|
167 |
/** |
168 |
* Sets a boolean property. |
169 |
* @param name The name of the property. |
170 |
* @param b The new value for the specified property. |
171 |
*/ |
172 |
public void |
173 |
setBoolProperty(String name, boolean b) { |
174 |
boolean oldValue = getBoolProperty(name); |
175 |
user().putBoolean(name, b); |
176 |
firePropertyChange(name, oldValue, b); |
177 |
} |
178 |
|
179 |
/** |
180 |
* Gets the default value for the specified property. |
181 |
* The default value is used when the property is not set. |
182 |
* Override this method to provide custom default values for specific properties. |
183 |
* @name The name of the property whose default value should be obtained. |
184 |
* @return <code>false</code> |
185 |
* @see #getBoolProperty(String name) |
186 |
*/ |
187 |
public boolean |
188 |
getDefaultBoolValue(String name) { return false; } |
189 |
} |