|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.debug.internal.core; |
| 12 |
|
| 13 |
import org.eclipse.core.runtime.preferences.DefaultScope; |
| 14 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 15 |
import org.eclipse.core.runtime.preferences.IScopeContext; |
| 16 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
| 17 |
import org.eclipse.debug.core.DebugPlugin; |
| 18 |
import org.osgi.service.prefs.BackingStoreException; |
| 19 |
|
| 20 |
/** |
| 21 |
* Convenience class to facilitate using the new {@link IEclipsePreferences} story |
| 22 |
* |
| 23 |
* @since 3.6 |
| 24 |
* @noinstantiate This class is not intended to be instantiated by clients. |
| 25 |
*/ |
| 26 |
public final class Preferences { |
| 27 |
|
| 28 |
static final IScopeContext[] contexts = new IScopeContext[] {new DefaultScope(), new InstanceScope()}; |
| 29 |
|
| 30 |
static final int DEFAULT_CONTEXT = 0; |
| 31 |
static final int INSTANCE_CONTEXT = 1; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
*/ |
| 36 |
private Preferences() { |
| 37 |
// no direct instantiation |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Sets a string preference in the {@link InstanceScope} or the given {@link IScopeContext} if it |
| 42 |
* is not <code>null</code>. Preferences set in a given context are flushed as they are set. |
| 43 |
* @param qualifier |
| 44 |
* @param key the key |
| 45 |
* @param value the value |
| 46 |
* @param context |
| 47 |
*/ |
| 48 |
public static synchronized void setString(String qualifier, String key, String value, IScopeContext context) { |
| 49 |
if(context != null) { |
| 50 |
try { |
| 51 |
IEclipsePreferences node = context.getNode(qualifier); |
| 52 |
node.put(key, value); |
| 53 |
node.flush(); |
| 54 |
} |
| 55 |
catch(BackingStoreException bse) { |
| 56 |
DebugPlugin.log(bse); |
| 57 |
} |
| 58 |
} |
| 59 |
else { |
| 60 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).put(key, value); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Sets a boolean preference in the {@link InstanceScope} or the given {@link IScopeContext} if it |
| 66 |
* is not <code>null</code>. Preferences set in a given context are flushed as they are set. |
| 67 |
* @param qualifier |
| 68 |
* @param key the key |
| 69 |
* @param value the value |
| 70 |
* @param context |
| 71 |
*/ |
| 72 |
public static synchronized void setBoolean(String qualifier, String key, boolean value, IScopeContext context) { |
| 73 |
if(context != null) { |
| 74 |
try { |
| 75 |
IEclipsePreferences node = context.getNode(qualifier); |
| 76 |
node.putBoolean(key, value); |
| 77 |
node.flush(); |
| 78 |
} |
| 79 |
catch(BackingStoreException bse) { |
| 80 |
DebugPlugin.log(bse); |
| 81 |
} |
| 82 |
} |
| 83 |
else { |
| 84 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putBoolean(key, value); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Sets a integer preference in the {@link InstanceScope} or the given {@link IScopeContext} if it |
| 90 |
* is not <code>null</code>. Preferences set in a given context are flushed as they are set. |
| 91 |
* @param qualifier |
| 92 |
* @param key the key |
| 93 |
* @param value the value |
| 94 |
* @param context |
| 95 |
*/ |
| 96 |
public static synchronized void setInt(String qualifier, String key, int value, IScopeContext context) { |
| 97 |
if(context != null) { |
| 98 |
try { |
| 99 |
IEclipsePreferences node = context.getNode(qualifier); |
| 100 |
node.putInt(key, value); |
| 101 |
node.flush(); |
| 102 |
} |
| 103 |
catch(BackingStoreException bse) { |
| 104 |
DebugPlugin.log(bse); |
| 105 |
} |
| 106 |
} |
| 107 |
else { |
| 108 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putInt(key, value); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Sets a long preference in the {@link InstanceScope} or the given {@link IScopeContext} if it |
| 114 |
* is not <code>null</code>. Preferences set in a given context are flushed as they are set. |
| 115 |
* @param qualifier |
| 116 |
* @param key the key |
| 117 |
* @param value the value |
| 118 |
* @param context |
| 119 |
*/ |
| 120 |
public static synchronized void setLong(String qualifier, String key, long value, IScopeContext context) { |
| 121 |
if(context != null) { |
| 122 |
try { |
| 123 |
IEclipsePreferences node = context.getNode(qualifier); |
| 124 |
node.putLong(key, value); |
| 125 |
node.flush(); |
| 126 |
} |
| 127 |
catch(BackingStoreException bse) { |
| 128 |
DebugPlugin.log(bse); |
| 129 |
} |
| 130 |
} |
| 131 |
else { |
| 132 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putLong(key, value); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Sets a byte array preference in the {@link InstanceScope} or the given {@link IScopeContext} if it |
| 138 |
* is not <code>null</code>. Preferences set in a given context are flushed as they are set. |
| 139 |
* @param qualifier |
| 140 |
* @param key the key |
| 141 |
* @param value the value |
| 142 |
* @param context |
| 143 |
*/ |
| 144 |
public static synchronized void setByteArray(String qualifier, String key, byte[] value, IScopeContext context) { |
| 145 |
if(context != null) { |
| 146 |
try { |
| 147 |
IEclipsePreferences node = context.getNode(qualifier); |
| 148 |
node.putByteArray(key, value); |
| 149 |
node.flush(); |
| 150 |
} |
| 151 |
catch(BackingStoreException bse) { |
| 152 |
DebugPlugin.log(bse); |
| 153 |
} |
| 154 |
} |
| 155 |
else { |
| 156 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putByteArray(key, value); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Sets a double preference in the {@link InstanceScope} or the given {@link IScopeContext} if it |
| 162 |
* is not <code>null</code>. Preferences set in a given context are flushed as they are set. |
| 163 |
* @param qualifier |
| 164 |
* @param key the key |
| 165 |
* @param value the value |
| 166 |
* @param context |
| 167 |
*/ |
| 168 |
public static synchronized void setDouble(String qualifier, String key, double value, IScopeContext context) { |
| 169 |
if(context != null) { |
| 170 |
try { |
| 171 |
IEclipsePreferences node = context.getNode(qualifier); |
| 172 |
node.putDouble(key, value); |
| 173 |
node.flush(); |
| 174 |
} |
| 175 |
catch(BackingStoreException bse) { |
| 176 |
DebugPlugin.log(bse); |
| 177 |
} |
| 178 |
} |
| 179 |
else { |
| 180 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putDouble(key, value); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Sets a float preference in the {@link InstanceScope} or the given {@link IScopeContext} if it |
| 186 |
* is not <code>null</code>. Preferences set in a given context are flushed as they are set. |
| 187 |
* @param qualifier |
| 188 |
* @param key the key |
| 189 |
* @param value the value |
| 190 |
* @param context |
| 191 |
*/ |
| 192 |
public static synchronized void setFloat(String qualifier, String key, float value, IScopeContext context) { |
| 193 |
if(context != null) { |
| 194 |
try { |
| 195 |
IEclipsePreferences node = context.getNode(qualifier); |
| 196 |
node.putFloat(key, value); |
| 197 |
node.flush(); |
| 198 |
} |
| 199 |
catch(BackingStoreException bse) { |
| 200 |
DebugPlugin.log(bse); |
| 201 |
} |
| 202 |
} |
| 203 |
else { |
| 204 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putFloat(key, value); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Sets a string in the {@link DefaultScope} |
| 210 |
* @param qualifier |
| 211 |
* @param key the key |
| 212 |
* @param value the new value |
| 213 |
*/ |
| 214 |
public static synchronized void setDefaultString(String qualifier, String key, String value) { |
| 215 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).put(key, value); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Sets a boolean in the {@link DefaultScope} |
| 220 |
* @param qualifier |
| 221 |
* @param key the key |
| 222 |
* @param value the new value |
| 223 |
*/ |
| 224 |
public static synchronized void setDefaultBoolean(String qualifier, String key, boolean value) { |
| 225 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putBoolean(key, value); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Sets a byte array in the {@link DefaultScope} |
| 230 |
* @param qualifier |
| 231 |
* @param key the key |
| 232 |
* @param value the new value |
| 233 |
*/ |
| 234 |
public static synchronized void setDefaultByteArray(String qualifier, String key, byte[] value) { |
| 235 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putByteArray(key, value); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Sets a double in the {@link DefaultScope} |
| 240 |
* @param qualifier |
| 241 |
* @param key the key |
| 242 |
* @param value the new value |
| 243 |
*/ |
| 244 |
public static synchronized void setDefaultDouble(String qualifier, String key, double value) { |
| 245 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putDouble(key, value); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Sets a float in the {@link DefaultScope} |
| 250 |
* @param qualifier |
| 251 |
* @param key the key |
| 252 |
* @param value the new value |
| 253 |
*/ |
| 254 |
public static synchronized void setDefaultFloat(String qualifier, String key, float value) { |
| 255 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putFloat(key, value); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Sets a integer in the {@link DefaultScope} |
| 260 |
* @param qualifier |
| 261 |
* @param key the key |
| 262 |
* @param value the new value |
| 263 |
*/ |
| 264 |
public static synchronized void setDefaultInt(String qualifier, String key, int value) { |
| 265 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putInt(key, value); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Sets a long in the {@link DefaultScope} |
| 270 |
* @param qualifier |
| 271 |
* @param key the key |
| 272 |
* @param value the new value |
| 273 |
*/ |
| 274 |
public static synchronized void setDefaultLong(String qualifier, String key, long value) { |
| 275 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putLong(key, value); |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Sets the given preference to its default value. This is done by removing any set value |
| 280 |
* from the {@link InstanceScope}. Has no effect if the given key is <code>null</code>. |
| 281 |
* @param qualifier |
| 282 |
* @param key the key for the preference |
| 283 |
*/ |
| 284 |
public static synchronized void setToDefault(String qualifier, String key) { |
| 285 |
if(key != null) { |
| 286 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).remove(key); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Returns the default boolean value stored in the {@link DefaultScope} for the given key |
| 292 |
* or the specified default value if the key does not appear in the {@link DefaultScope} |
| 293 |
* @param qualifier |
| 294 |
* @param key |
| 295 |
* @param defaultvalue |
| 296 |
* |
| 297 |
* @return the boolean value set in the {@link DefaultScope} for the given key, or the specified default value. |
| 298 |
*/ |
| 299 |
public static synchronized boolean getDefaultBoolean(String qualifier, String key, boolean defaultvalue) { |
| 300 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getBoolean(key, defaultvalue); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Returns the default string value stored in the {@link DefaultScope} for the given key |
| 305 |
* or the specified default value if the key does not appear in the {@link DefaultScope} |
| 306 |
* @param qualifier |
| 307 |
* @param key |
| 308 |
* @param defaultvalue |
| 309 |
* |
| 310 |
* @return the string value set in the {@link DefaultScope} for the given key, or the specified default value. |
| 311 |
*/ |
| 312 |
public static synchronized String getDefaultString(String qualifier, String key, String defaultvalue) { |
| 313 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).get(key, defaultvalue); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns the default byte array value stored in the {@link DefaultScope} for the given key |
| 318 |
* or the specified default value if the key does not appear in the {@link DefaultScope} |
| 319 |
* @param qualifier |
| 320 |
* @param key |
| 321 |
* @param defaultvalue |
| 322 |
* |
| 323 |
* @return the byte array value set in the {@link DefaultScope} for the given key, or the specified default value. |
| 324 |
*/ |
| 325 |
public static synchronized byte[] getDefaultByteArray(String qualifier, String key, byte[] defaultvalue) { |
| 326 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getByteArray(key, defaultvalue); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Returns the default integer value stored in the {@link DefaultScope} for the given key |
| 331 |
* or the specified default value if the key does not appear in the {@link DefaultScope} |
| 332 |
* @param qualifier |
| 333 |
* @param key |
| 334 |
* @param defaultvalue |
| 335 |
* |
| 336 |
* @return the integer value set in the {@link DefaultScope} for the given key, or the specified default value. |
| 337 |
*/ |
| 338 |
public static synchronized int getDefaultInt(String qualifier, String key, int defaultvalue) { |
| 339 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getInt(key, defaultvalue); |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Returns the default long value stored in the {@link DefaultScope} for the given key |
| 344 |
* or the specified default value if the key does not appear in the {@link DefaultScope} |
| 345 |
* @param qualifier |
| 346 |
* @param key |
| 347 |
* @param defaultvalue |
| 348 |
* |
| 349 |
* @return the long value set in the {@link DefaultScope} for the given key, or the specified default value. |
| 350 |
*/ |
| 351 |
public static synchronized long getDefaultLong(String qualifier, String key, long defaultvalue) { |
| 352 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getLong(key, defaultvalue); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Returns the default double value stored in the {@link DefaultScope} for the given key |
| 357 |
* or the specified default value if the key does not appear in the {@link DefaultScope} |
| 358 |
* @param qualifier |
| 359 |
* @param key |
| 360 |
* @param defaultvalue |
| 361 |
* |
| 362 |
* @return the double value set in the {@link DefaultScope} for the given key, or the specified default value. |
| 363 |
*/ |
| 364 |
public static synchronized double getDefaultDouble(String qualifier, String key, double defaultvalue) { |
| 365 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getDouble(key, defaultvalue); |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Returns the default float value stored in the {@link DefaultScope} for the given key |
| 370 |
* or the specified default value if the key does not appear in the {@link DefaultScope} |
| 371 |
* @param qualifier |
| 372 |
* @param key |
| 373 |
* @param defaultvalue |
| 374 |
* |
| 375 |
* @return the float value set in the {@link DefaultScope} for the given key, or the specified default value. |
| 376 |
*/ |
| 377 |
public static synchronized float getDefaultFloat(String qualifier, String key, float defaultvalue) { |
| 378 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getFloat(key, defaultvalue); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Save the preferences for the given plugin identifier. |
| 383 |
* It should be noted that all pending preference changes will be flushed with this method. |
| 384 |
* @param qualifier |
| 385 |
*/ |
| 386 |
public static synchronized void savePreferences(String qualifier) { |
| 387 |
try { |
| 388 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).flush(); |
| 389 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).flush(); |
| 390 |
} |
| 391 |
catch(BackingStoreException bse) { |
| 392 |
DebugPlugin.log(bse); |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/** |
| 397 |
* Adds the given preference listener to the {@link DefaultScope} and the {@link InstanceScope} |
| 398 |
* @param qualifier |
| 399 |
* @param listener |
| 400 |
*/ |
| 401 |
public static void addPreferenceListener(String qualifier, IEclipsePreferences.IPreferenceChangeListener listener) { |
| 402 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).addPreferenceChangeListener(listener); |
| 403 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).addPreferenceChangeListener(listener); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Removes the given preference listener from the {@link DefaultScope} and the {@link InstanceScope} |
| 408 |
* @param qualifier |
| 409 |
* @param listener |
| 410 |
*/ |
| 411 |
public static void removePreferenceListener(String qualifier, IEclipsePreferences.IPreferenceChangeListener listener) { |
| 412 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).removePreferenceChangeListener(listener); |
| 413 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).removePreferenceChangeListener(listener); |
| 414 |
} |
| 415 |
} |