Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1e7995c8b0f5d8e373b51820be8a37deace4900e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*******************************************************************************
 * Copyright (c) 2009, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.core;

import org.osgi.service.prefs.BackingStoreException;

import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;

import org.eclipse.debug.core.DebugPlugin;

/**
 * Convenience class to facilitate using the new {@link IEclipsePreferences} story
 *
 * @since 3.6
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public final class Preferences {

	static final IScopeContext[] contexts= new IScopeContext[] { DefaultScope.INSTANCE, InstanceScope.INSTANCE };

	static final int DEFAULT_CONTEXT = 0;
	static final int INSTANCE_CONTEXT = 1;

	/**
	 * Constructor
	 */
	private Preferences() {
		// no direct instantiation
	}

	/**
	 * Sets a string preference in the {@link InstanceScope} or the given {@link IScopeContext} if it
	 * is not <code>null</code>. Preferences set in a given context are flushed as they are set.
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the value
	 * @param context the context to set the value in
	 */
	public static synchronized void setString(String qualifier, String key, String value, IScopeContext context) {
		if(context != null) {
			try {
				IEclipsePreferences node = context.getNode(qualifier);
				node.put(key, value);
				node.flush();
			}
			catch(BackingStoreException bse) {
				DebugPlugin.log(bse);
			}
		}
		else {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).put(key, value);
		}
	}

	/**
	 * Sets a boolean preference in the {@link InstanceScope} or the given {@link IScopeContext} if it
	 * is not <code>null</code>. Preferences set in a given context are flushed as they are set.
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the value
	 * @param context the context to set the value in
	 */
	public static synchronized void setBoolean(String qualifier, String key, boolean value, IScopeContext context) {
		if(context != null) {
			try {
				IEclipsePreferences node = context.getNode(qualifier);
				node.putBoolean(key, value);
				node.flush();
			}
			catch(BackingStoreException bse) {
				DebugPlugin.log(bse);
			}
		}
		else {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).putBoolean(key, value);
		}
	}

	/**
	 * Sets a integer preference in the {@link InstanceScope} or the given {@link IScopeContext} if it
	 * is not <code>null</code>. Preferences set in a given context are flushed as they are set.
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the value
	 * @param context the context to set the value in
	 */
	public static synchronized void setInt(String qualifier, String key, int value, IScopeContext context) {
		if(context != null) {
			try {
				IEclipsePreferences node = context.getNode(qualifier);
				node.putInt(key, value);
				node.flush();
			}
			catch(BackingStoreException bse) {
				DebugPlugin.log(bse);
			}
		}
		else {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).putInt(key, value);
		}
	}

	/**
	 * Sets a long preference in the {@link InstanceScope} or the given {@link IScopeContext} if it
	 * is not <code>null</code>. Preferences set in a given context are flushed as they are set.
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the value
	 * @param context the context to set the value in
	 */
	public static synchronized void setLong(String qualifier, String key, long value, IScopeContext context) {
		if(context != null) {
			try {
				IEclipsePreferences node = context.getNode(qualifier);
				node.putLong(key, value);
				node.flush();
			}
			catch(BackingStoreException bse) {
				DebugPlugin.log(bse);
			}
		}
		else {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).putLong(key, value);
		}
	}

	/**
	 * Sets a byte array preference in the {@link InstanceScope} or the given {@link IScopeContext} if it
	 * is not <code>null</code>. Preferences set in a given context are flushed as they are set.
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the value
	 * @param context the context to set the value in
	 */
	public static synchronized void setByteArray(String qualifier, String key, byte[] value, IScopeContext context) {
		if(context != null) {
			try {
				IEclipsePreferences node = context.getNode(qualifier);
				node.putByteArray(key, value);
				node.flush();
			}
			catch(BackingStoreException bse) {
				DebugPlugin.log(bse);
			}
		}
		else {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).putByteArray(key, value);
		}
	}

	/**
	 * Sets a double preference in the {@link InstanceScope} or the given {@link IScopeContext} if it
	 * is not <code>null</code>. Preferences set in a given context are flushed as they are set.
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the value
	 * @param context the context to set the value in
	 */
	public static synchronized void setDouble(String qualifier, String key, double value, IScopeContext context) {
		if(context != null) {
			try {
				IEclipsePreferences node = context.getNode(qualifier);
				node.putDouble(key, value);
				node.flush();
			}
			catch(BackingStoreException bse) {
				DebugPlugin.log(bse);
			}
		}
		else {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).putDouble(key, value);
		}
	}

	/**
	 * Sets a float preference in the {@link InstanceScope} or the given {@link IScopeContext} if it
	 * is not <code>null</code>. Preferences set in a given context are flushed as they are set.
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the value
	 * @param context the context to setthe value in
	 */
	public static synchronized void setFloat(String qualifier, String key, float value, IScopeContext context) {
		if(context != null) {
			try {
				IEclipsePreferences node = context.getNode(qualifier);
				node.putFloat(key, value);
				node.flush();
			}
			catch(BackingStoreException bse) {
				DebugPlugin.log(bse);
			}
		}
		else {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).putFloat(key, value);
		}
	}

	/**
	 * Sets a string in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the new value
	 */
	public static synchronized void setDefaultString(String qualifier, String key, String value) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).put(key, value);
	}

	/**
	 * Sets a boolean in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the new value
	 */
	public static synchronized void setDefaultBoolean(String qualifier, String key, boolean value) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).putBoolean(key, value);
	}

	/**
	 * Sets a byte array in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the new value
	 */
	public static synchronized void setDefaultByteArray(String qualifier, String key, byte[] value) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).putByteArray(key, value);
	}

	/**
	 * Sets a double in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the new value
	 */
	public static synchronized void setDefaultDouble(String qualifier, String key, double value) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).putDouble(key, value);
	}

	/**
	 * Sets a float in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the new value
	 */
	public static synchronized void setDefaultFloat(String qualifier, String key, float value) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).putFloat(key, value);
	}

	/**
	 * Sets a integer in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the new value
	 */
	public static synchronized void setDefaultInt(String qualifier, String key, int value) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).putInt(key, value);
	}

	/**
	 * Sets a long in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the key
	 * @param value the new value
	 */
	public static synchronized void setDefaultLong(String qualifier, String key, long value) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).putLong(key, value);
	}

	/**
	 * Sets the given preference to its default value. This is done by removing any set value
	 * from the {@link InstanceScope}. Has no effect if the given key is <code>null</code>.
	 * @param qualifier the preference qualifier
	 * @param key the key for the preference
	 */
	public static synchronized void setToDefault(String qualifier, String key) {
		if(key != null) {
			contexts[INSTANCE_CONTEXT].getNode(qualifier).remove(key);
		}
	}

	/**
	 * Returns the default boolean value stored in the {@link DefaultScope} for the given key
	 * or the specified default value if the key does not appear in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the preference key
	 * @param defaultvalue the default value
	 *
	 * @return the boolean value set in the {@link DefaultScope} for the given key, or the specified default value.
	 */
	public static synchronized boolean getDefaultBoolean(String qualifier, String key, boolean defaultvalue) {
		return contexts[DEFAULT_CONTEXT].getNode(qualifier).getBoolean(key, defaultvalue);
	}

	/**
	 * Returns the default string value stored in the {@link DefaultScope} for the given key
	 * or the specified default value if the key does not appear in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the preference key
	 * @param defaultvalue the default value
	 *
	 * @return the string value set in the {@link DefaultScope} for the given key, or the specified default value.
	 */
	public static synchronized String getDefaultString(String qualifier, String key, String defaultvalue) {
		return contexts[DEFAULT_CONTEXT].getNode(qualifier).get(key, defaultvalue);
	}

	/**
	 * Returns the default byte array value stored in the {@link DefaultScope} for the given key
	 * or the specified default value if the key does not appear in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the preference key
	 * @param defaultvalue the default value
	 *
	 * @return the byte array value set in the {@link DefaultScope} for the given key, or the specified default value.
	 */
	public static synchronized byte[] getDefaultByteArray(String qualifier, String key, byte[] defaultvalue) {
		return contexts[DEFAULT_CONTEXT].getNode(qualifier).getByteArray(key, defaultvalue);
	}

	/**
	 * Returns the default integer value stored in the {@link DefaultScope} for the given key
	 * or the specified default value if the key does not appear in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the preference key
	 * @param defaultvalue the default value
	 *
	 * @return the integer value set in the {@link DefaultScope} for the given key, or the specified default value.
	 */
	public static synchronized int getDefaultInt(String qualifier, String key, int defaultvalue) {
		return contexts[DEFAULT_CONTEXT].getNode(qualifier).getInt(key, defaultvalue);
	}

	/**
	 * Returns the default long value stored in the {@link DefaultScope} for the given key
	 * or the specified default value if the key does not appear in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the preference key
	 * @param defaultvalue the default value
	 *
	 * @return the long value set in the {@link DefaultScope} for the given key, or the specified default value.
	 */
	public static synchronized long getDefaultLong(String qualifier, String key, long defaultvalue) {
		return contexts[DEFAULT_CONTEXT].getNode(qualifier).getLong(key, defaultvalue);
	}

	/**
	 * Returns the default double value stored in the {@link DefaultScope} for the given key
	 * or the specified default value if the key does not appear in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the preference key
	 * @param defaultvalue the default value
	 *
	 * @return the double value set in the {@link DefaultScope} for the given key, or the specified default value.
	 */
	public static synchronized double getDefaultDouble(String qualifier, String key, double defaultvalue) {
		return contexts[DEFAULT_CONTEXT].getNode(qualifier).getDouble(key, defaultvalue);
	}

	/**
	 * Returns the default float value stored in the {@link DefaultScope} for the given key
	 * or the specified default value if the key does not appear in the {@link DefaultScope}
	 * @param qualifier the preference qualifier
	 * @param key the preference key
	 * @param defaultvalue the default value
	 *
	 * @return the float value set in the {@link DefaultScope} for the given key, or the specified default value.
	 */
	public static synchronized float getDefaultFloat(String qualifier, String key, float defaultvalue) {
		return contexts[DEFAULT_CONTEXT].getNode(qualifier).getFloat(key, defaultvalue);
	}

	/**
	 * Save the preferences for the given plug-in identifier.
	 * It should be noted that all pending preference changes will be flushed with this method.
	 * @param qualifier the preference qualifier
	 */
	public static synchronized void savePreferences(String qualifier) {
		try {
			contexts[DEFAULT_CONTEXT].getNode(qualifier).flush();
			contexts[INSTANCE_CONTEXT].getNode(qualifier).flush();
		}
		catch(BackingStoreException bse) {
			DebugPlugin.log(bse);
		}
	}

	/**
	 * Adds the given preference listener to the {@link DefaultScope} and the {@link InstanceScope}
	 * @param qualifier the preference qualifier
	 * @param listener the listener to register
	 */
	public static void addPreferenceListener(String qualifier, IEclipsePreferences.IPreferenceChangeListener listener) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).addPreferenceChangeListener(listener);
		contexts[INSTANCE_CONTEXT].getNode(qualifier).addPreferenceChangeListener(listener);
	}

	/**
	 * Removes the given preference listener from the {@link DefaultScope} and the {@link InstanceScope}
	 * @param qualifier the preference qualifier
	 * @param listener the listener to register
	 */
	public static void removePreferenceListener(String qualifier, IEclipsePreferences.IPreferenceChangeListener listener) {
		contexts[DEFAULT_CONTEXT].getNode(qualifier).removePreferenceChangeListener(listener);
		contexts[INSTANCE_CONTEXT].getNode(qualifier).removePreferenceChangeListener(listener);
	}
}

Back to the top