1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.util;
21
22 import java.awt.Color;
23 import java.awt.Font;
24 import java.beans.PropertyChangeListener;
25
26 import javax.swing.KeyStroke;
27
28 import org.apache.log4j.Logger;
29 import org.xnap.util.prefs.Validator;
30
31 /***
32 * This class acts as a wraper for the {@link Preferences} class. It
33 * is meant to be extended by plugins that need to store preferences.
34 */
35 public abstract class AbstractPluginPreferences
36 implements TablePreferencesProvider {
37
38
39
40 public static final String VERSION_KEY = "props.ver";
41
42
43
44 protected static Logger logger
45 = Logger.getLogger(AbstractPluginPreferences.class);
46
47 protected Preferences prefs = Preferences.getInstance();
48
49 private String namespace = "";
50 private int version;
51
52
53
54 /***
55 * Constructs an AbstractPluginPreference object. Calls the convert
56 * method if neccessary.
57 *
58 * @param namespace a prefix that is used for each key
59 * @param version the current version of the preferences
60 */
61 public AbstractPluginPreferences(String namespace, int version)
62 {
63 if (!namespace.endsWith(".")) {
64 namespace += ".";
65 }
66
67 this.namespace = namespace;
68 this.version = version;
69
70 int oldVersion = getInt(VERSION_KEY);
71 if (oldVersion < version) {
72 logger.debug("converting from version " + oldVersion + " to "
73 + version);
74 convert(oldVersion);
75 }
76 set(VERSION_KEY, version);
77 }
78
79
80
81 public synchronized
82 void addPropertyChangeListener(PropertyChangeListener l)
83 {
84 prefs.addPropertyChangeListener(l);
85 }
86
87 public synchronized
88 void addPropertyChangeListener(String key, PropertyChangeListener l)
89 {
90 prefs.addPropertyChangeListener(key, l);
91 }
92
93 public synchronized
94 void addTableListener(String table, PropertyChangeListener l)
95 {
96 prefs.addTableListener(table, l);
97 }
98
99 public synchronized
100 void removePropertyChangeListener(PropertyChangeListener l) {
101 prefs.removePropertyChangeListener(l);
102 }
103
104 public synchronized
105 void removePropertyChangeListener(String key, PropertyChangeListener l) {
106 prefs.removePropertyChangeListener(key, l);
107 }
108
109 /***
110 * Convert the preferences from <code>oldVersion</code>. The
111 * default implementation does nothing. Sub classes should
112 * overwrite this method.
113 */
114 public void convert(int oldVersion)
115 {
116 }
117
118 public String get(String key)
119 {
120 return prefs.get(namespace + key);
121 }
122
123 public boolean getNotShowDialog(String dialogName)
124 {
125 dialogName = StringHelper.toFirstUpper(dialogName);
126 return prefs.getBoolean(namespace + "notShowDialog" + dialogName);
127 }
128
129 public String[] getArray(String key)
130 {
131 return prefs.getArray(namespace + key);
132 }
133
134 public boolean getBoolean(String key)
135 {
136 return prefs.getBoolean(namespace + key);
137 }
138
139 public Color getColor(String key)
140 {
141 return prefs.getColor(namespace + key);
142 }
143
144 public Font getFont(String key)
145 {
146 return prefs.getFont(namespace + key);
147 }
148
149 public int getInt(String key)
150 {
151 return prefs.getInt(namespace + key);
152 }
153
154 public int[] getIntArray(String key)
155 {
156 return prefs.getIntArray(namespace + key);
157 }
158
159 public KeyStroke getKeyStroke(String key)
160 {
161 return prefs.getKeyStroke(namespace + key);
162 }
163
164 public long getLong(String key)
165 {
166 return prefs.getLong(namespace + key);
167 }
168
169 public String[] getTableColumns(String table)
170 {
171 return prefs.getTableColumns(namespace + table);
172 }
173
174 public int[] getTableColumnWidths(String table)
175 {
176 return prefs.getTableColumnWidths(namespace + table);
177 }
178
179 public boolean getTableMaintainSortOrder(String table)
180 {
181 return prefs.getTableMaintainSortOrder(namespace + table);
182 }
183
184 public int getTableSortedColumn(String table)
185 {
186 return prefs.getTableSortedColumn(namespace + table);
187 }
188
189 public void removeProperty(String key)
190 {
191 prefs.removeProperty(key);
192 }
193
194 /***
195 * Renames a property, used for conversion of property file formats.
196 * Ignores namespace. Does not fire change event.
197 */
198 public void renameProperty(String oldKey, String newKey)
199 {
200 prefs.renameProperty(oldKey, newKey);
201 }
202
203 public void set(String key, String newValue)
204 {
205 prefs.set(namespace + key, newValue);
206 }
207
208 public void set(String key, String[] newValue)
209 {
210 prefs.set(namespace + key, newValue);
211 }
212
213 public void set(String key, boolean newValue)
214 {
215 prefs.set(namespace + key, newValue);
216 }
217
218 public void set(String key, Color newValue)
219 {
220 prefs.set(namespace + key, newValue);
221 }
222
223 public void set(String key, Font newValue)
224 {
225 prefs.set(namespace + key, newValue);
226 }
227
228 public void set(String key, int newValue)
229 {
230 prefs.set(namespace + key, newValue);
231 }
232
233 public void set(String key, int[] newValue)
234 {
235 prefs.set(namespace + key, newValue);
236 }
237
238 public void set(String key, KeyStroke newValue)
239 {
240 prefs.set(namespace + key, newValue);
241 }
242
243 public void set(String key, long newValue)
244 {
245 prefs.set(namespace + key, newValue);
246 }
247
248 public void setDefault(String key, String value, Validator validator)
249 {
250 prefs.setDefault(namespace + key, value, validator);
251 }
252
253 public void setDefault(String key, String value)
254 {
255 prefs.setDefault(namespace + key, value);
256 }
257
258 public void setNotShowDialog(String dialogName, boolean newValue)
259 {
260 dialogName = StringHelper.toFirstUpper(dialogName);
261 prefs.set(namespace + "notShowDialog" + dialogName, newValue);
262 }
263
264 public void setTableColumns(String table, String[] columns)
265 {
266 prefs.setTableColumns(namespace + table, columns);
267 }
268
269 public void setTableColumnWidths(String table, int[] widths)
270 {
271 prefs.setTableColumnWidths(namespace + table, widths);
272 }
273
274 public void setTableMaintainSortOrder(String table, boolean enable)
275 {
276 prefs.setTableMaintainSortOrder(namespace + table, enable);
277 }
278
279 public void setTableSortedColumn(String table, int column)
280 {
281 prefs.setTableSortedColumn(namespace + table, column);
282 }
283
284 }