|
Added
Link Here
|
| 1 |
package org.eclipse.ptp.etfw; |
| 2 |
|
| 3 |
import org.eclipse.core.runtime.Platform; |
| 4 |
import org.eclipse.core.runtime.preferences.DefaultScope; |
| 5 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences; |
| 6 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; |
| 7 |
import org.eclipse.core.runtime.preferences.IScopeContext; |
| 8 |
import org.eclipse.core.runtime.preferences.InstanceScope; |
| 9 |
import org.eclipse.ptp.remote.core.PTPRemoteCorePlugin; |
| 10 |
import org.osgi.service.prefs.BackingStoreException; |
| 11 |
|
| 12 |
/** |
| 13 |
* Convenience class to facilitate using the new {@link IEclipsePreferences} story. Adapted from |
| 14 |
* org.eclipse.debug.internal.core.Preferences. |
| 15 |
* |
| 16 |
* @since 6.0 |
| 17 |
* @noinstantiate This class is not intended to be instantiated by clients. |
| 18 |
*/ |
| 19 |
public final class Preferences { |
| 20 |
private static final IScopeContext[] contexts = new IScopeContext[] { DefaultScope.INSTANCE, InstanceScope.INSTANCE }; |
| 21 |
|
| 22 |
private static final int DEFAULT_CONTEXT = 0; |
| 23 |
private static final int INSTANCE_CONTEXT = 1; |
| 24 |
|
| 25 |
/** |
| 26 |
* Adds the given preference listener to the {@link DefaultScope} and the {@link InstanceScope} |
| 27 |
* |
| 28 |
* @param qualifier |
| 29 |
* @param listener |
| 30 |
*/ |
| 31 |
public static void addPreferenceChangeListener(String qualifier, IPreferenceChangeListener listener) { |
| 32 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).addPreferenceChangeListener(listener); |
| 33 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).addPreferenceChangeListener(listener); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns whether the named preference is know in the preference store. |
| 38 |
* |
| 39 |
* @param qualifier |
| 40 |
* @param name |
| 41 |
* @return |
| 42 |
*/ |
| 43 |
public static boolean contains(String qualifier, String name) { |
| 44 |
return (contexts[INSTANCE_CONTEXT].getNode(qualifier).get(name, null) != null || contexts[DEFAULT_CONTEXT].getNode( |
| 45 |
qualifier).get(name, null) != null); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns the value in the preference store for the given key. If the key |
| 50 |
* is not defined then return the default value. Use the canonical scope |
| 51 |
* lookup order for finding the preference value. |
| 52 |
* |
| 53 |
* @param qualifier |
| 54 |
* @param key |
| 55 |
* @param defaultvalue |
| 56 |
* |
| 57 |
* @return the value of the preference or the given default value |
| 58 |
*/ |
| 59 |
public static boolean getBoolean(String qualifier, String key) { |
| 60 |
return Platform.getPreferencesService().getBoolean(qualifier, key, false, null); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the value in the preference store for the given key. If the key |
| 65 |
* is not defined then return the default value. Use the canonical scope |
| 66 |
* lookup order for finding the preference value. |
| 67 |
* |
| 68 |
* @param qualifier |
| 69 |
* @param key |
| 70 |
* @param defaultvalue |
| 71 |
* |
| 72 |
* @return the value of the preference or the given default value |
| 73 |
*/ |
| 74 |
public static byte[] getByteArray(String qualifier, String key) { |
| 75 |
return Platform.getPreferencesService().getByteArray(qualifier, key, null, null); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns the default boolean value stored in the {@link DefaultScope} for |
| 80 |
* the given key or the specified default value if the key does not appear |
| 81 |
* in the {@link DefaultScope} |
| 82 |
* |
| 83 |
* @param qualifier |
| 84 |
* @param key |
| 85 |
* @param defaultvalue |
| 86 |
* |
| 87 |
* @return the boolean value set in the {@link DefaultScope} for the given |
| 88 |
* key, or the specified default value. |
| 89 |
*/ |
| 90 |
public static synchronized boolean getDefaultBoolean(String qualifier, String key, boolean defaultvalue) { |
| 91 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getBoolean(key, defaultvalue); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns the default byte array value stored in the {@link DefaultScope} for the given key or the specified default value if |
| 96 |
* the key does not |
| 97 |
* appear in the {@link DefaultScope} |
| 98 |
* |
| 99 |
* @param qualifier |
| 100 |
* @param key |
| 101 |
* @param defaultvalue |
| 102 |
* |
| 103 |
* @return the byte array value set in the {@link DefaultScope} for the |
| 104 |
* given key, or the specified default value. |
| 105 |
*/ |
| 106 |
public static synchronized byte[] getDefaultByteArray(String qualifier, String key, byte[] defaultvalue) { |
| 107 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getByteArray(key, defaultvalue); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns the default double value stored in the {@link DefaultScope} for |
| 112 |
* the given key or the specified default value if the key does not appear |
| 113 |
* in the {@link DefaultScope} |
| 114 |
* |
| 115 |
* @param qualifier |
| 116 |
* @param key |
| 117 |
* @param defaultvalue |
| 118 |
* |
| 119 |
* @return the double value set in the {@link DefaultScope} for the given |
| 120 |
* key, or the specified default value. |
| 121 |
*/ |
| 122 |
public static synchronized double getDefaultDouble(String qualifier, String key, double defaultvalue) { |
| 123 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getDouble(key, defaultvalue); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the default float value stored in the {@link DefaultScope} for |
| 128 |
* the given key or the specified default value if the key does not appear |
| 129 |
* in the {@link DefaultScope} |
| 130 |
* |
| 131 |
* @param qualifier |
| 132 |
* @param key |
| 133 |
* @param defaultvalue |
| 134 |
* |
| 135 |
* @return the float value set in the {@link DefaultScope} for the given |
| 136 |
* key, or the specified default value. |
| 137 |
*/ |
| 138 |
public static synchronized float getDefaultFloat(String qualifier, String key, float defaultvalue) { |
| 139 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getFloat(key, defaultvalue); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the default integer value stored in the {@link DefaultScope} for |
| 144 |
* the given key or the specified default value if the key does not appear |
| 145 |
* in the {@link DefaultScope} |
| 146 |
* |
| 147 |
* @param qualifier |
| 148 |
* @param key |
| 149 |
* @param defaultvalue |
| 150 |
* |
| 151 |
* @return the integer value set in the {@link DefaultScope} for the given |
| 152 |
* key, or the specified default value. |
| 153 |
*/ |
| 154 |
public static synchronized int getDefaultInt(String qualifier, String key, int defaultvalue) { |
| 155 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getInt(key, defaultvalue); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns the default long value stored in the {@link DefaultScope} for the |
| 160 |
* given key or the specified default value if the key does not appear in |
| 161 |
* the {@link DefaultScope} |
| 162 |
* |
| 163 |
* @param qualifier |
| 164 |
* @param key |
| 165 |
* @param defaultvalue |
| 166 |
* |
| 167 |
* @return the long value set in the {@link DefaultScope} for the given key, |
| 168 |
* or the specified default value. |
| 169 |
*/ |
| 170 |
public static synchronized long getDefaultLong(String qualifier, String key, long defaultvalue) { |
| 171 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).getLong(key, defaultvalue); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Returns the default string value stored in the {@link DefaultScope} for |
| 176 |
* the given key or the specified default value if the key does not appear |
| 177 |
* in the {@link DefaultScope} |
| 178 |
* |
| 179 |
* @param qualifier |
| 180 |
* @param key |
| 181 |
* @param defaultvalue |
| 182 |
* |
| 183 |
* @return the string value set in the {@link DefaultScope} for the given |
| 184 |
* key, or the specified default value. |
| 185 |
*/ |
| 186 |
public static synchronized String getDefaultString(String qualifier, String key, String defaultvalue) { |
| 187 |
return contexts[DEFAULT_CONTEXT].getNode(qualifier).get(key, defaultvalue); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Returns the value in the preference store for the given key. If the key |
| 192 |
* is not defined then return the default value. Use the canonical scope |
| 193 |
* lookup order for finding the preference value. |
| 194 |
* |
| 195 |
* @param qualifier |
| 196 |
* @param key |
| 197 |
* @param defaultvalue |
| 198 |
* |
| 199 |
* @return the value of the preference or the given default value |
| 200 |
*/ |
| 201 |
public static double getDouble(String qualifier, String key) { |
| 202 |
return Platform.getPreferencesService().getDouble(qualifier, key, 0.0, null); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Returns the value in the preference store for the given key. If the key |
| 207 |
* is not defined then return the default value. Use the canonical scope |
| 208 |
* lookup order for finding the preference value. |
| 209 |
* |
| 210 |
* @param qualifier |
| 211 |
* @param key |
| 212 |
* @param defaultvalue |
| 213 |
* |
| 214 |
* @return the value of the preference or the given default value |
| 215 |
*/ |
| 216 |
public static float getFloat(String qualifier, String key) { |
| 217 |
return Platform.getPreferencesService().getFloat(qualifier, key, 0.0f, null); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the value in the preference store for the given key. If the key |
| 222 |
* is not defined then return the default value. Use the canonical scope |
| 223 |
* lookup order for finding the preference value. |
| 224 |
* |
| 225 |
* @param qualifier |
| 226 |
* @param key |
| 227 |
* @param defaultvalue |
| 228 |
* |
| 229 |
* @return the value of the preference or the given default value |
| 230 |
*/ |
| 231 |
public static int getInt(String qualifier, String key) { |
| 232 |
return Platform.getPreferencesService().getInt(qualifier, key, 0, null); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Returns the value in the preference store for the given key. If the key |
| 237 |
* is not defined then return the default value. Use the canonical scope |
| 238 |
* lookup order for finding the preference value. |
| 239 |
* |
| 240 |
* @param qualifier |
| 241 |
* @param key |
| 242 |
* @param defaultvalue |
| 243 |
* |
| 244 |
* @return the value of the preference or the given default value |
| 245 |
*/ |
| 246 |
public static long getLong(String qualifier, String key) { |
| 247 |
return Platform.getPreferencesService().getLong(qualifier, key, 0L, null); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Returns the value in the preference store for the given key. If the key |
| 252 |
* is not defined then return the default value. Use the canonical scope |
| 253 |
* lookup order for finding the preference value. |
| 254 |
* |
| 255 |
* @param qualifier |
| 256 |
* @param key |
| 257 |
* @param defaultvalue |
| 258 |
* |
| 259 |
* @return the value of the preference or the given default value |
| 260 |
*/ |
| 261 |
public static String getString(String qualifier, String key) { |
| 262 |
return Platform.getPreferencesService().getString(qualifier, key, null, null); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Returns true if the named preference has the default value. |
| 267 |
* |
| 268 |
* @param qualifier |
| 269 |
* @param name |
| 270 |
* @return |
| 271 |
*/ |
| 272 |
public static boolean isDefault(String qualifier, String name) { |
| 273 |
String defVal = contexts[DEFAULT_CONTEXT].getNode(qualifier).get(name, null); |
| 274 |
if (defVal != null) { |
| 275 |
String val = contexts[INSTANCE_CONTEXT].getNode(qualifier).get(name, null); |
| 276 |
return (val != null && val.equals(defVal)); |
| 277 |
} |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Removes the given preference listener from the {@link DefaultScope} and |
| 283 |
* the {@link InstanceScope} |
| 284 |
* |
| 285 |
* @param qualifier |
| 286 |
* @param listener |
| 287 |
*/ |
| 288 |
public static void removePreferenceChangeListener(String qualifier, IPreferenceChangeListener listener) { |
| 289 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).removePreferenceChangeListener(listener); |
| 290 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).removePreferenceChangeListener(listener); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Save the preferences for the given plugin identifier. It should be noted |
| 295 |
* that all pending preference changes will be flushed with this method. |
| 296 |
* |
| 297 |
* @param qualifier |
| 298 |
*/ |
| 299 |
public static synchronized void savePreferences(String qualifier) { |
| 300 |
try { |
| 301 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).flush(); |
| 302 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).flush(); |
| 303 |
} catch (BackingStoreException bse) { |
| 304 |
PTPRemoteCorePlugin.log(bse); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Sets a boolean preference in the {@link InstanceScope}. |
| 310 |
* |
| 311 |
* @param qualifier |
| 312 |
* @param key |
| 313 |
* the key |
| 314 |
* @param value |
| 315 |
* the value |
| 316 |
*/ |
| 317 |
public static synchronized void setBoolean(String qualifier, String key, boolean value) { |
| 318 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putBoolean(key, value); |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Sets a byte array preference in the {@link InstanceScope}. |
| 323 |
* |
| 324 |
* @param qualifier |
| 325 |
* @param key |
| 326 |
* the key |
| 327 |
* @param value |
| 328 |
* the value |
| 329 |
*/ |
| 330 |
public static synchronized void setByteArray(String qualifier, String key, byte[] value) { |
| 331 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putByteArray(key, value); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Sets a boolean in the {@link DefaultScope} |
| 336 |
* |
| 337 |
* @param qualifier |
| 338 |
* @param key |
| 339 |
* the key |
| 340 |
* @param value |
| 341 |
* the new value |
| 342 |
*/ |
| 343 |
public static synchronized void setDefaultBoolean(String qualifier, String key, boolean value) { |
| 344 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putBoolean(key, value); |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Sets a byte array in the {@link DefaultScope} |
| 349 |
* |
| 350 |
* @param qualifier |
| 351 |
* @param key |
| 352 |
* the key |
| 353 |
* @param value |
| 354 |
* the new value |
| 355 |
*/ |
| 356 |
public static synchronized void setDefaultByteArray(String qualifier, String key, byte[] value) { |
| 357 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putByteArray(key, value); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Sets a double in the {@link DefaultScope} |
| 362 |
* |
| 363 |
* @param qualifier |
| 364 |
* @param key |
| 365 |
* the key |
| 366 |
* @param value |
| 367 |
* the new value |
| 368 |
*/ |
| 369 |
public static synchronized void setDefaultDouble(String qualifier, String key, double value) { |
| 370 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putDouble(key, value); |
| 371 |
} |
| 372 |
|
| 373 |
/** |
| 374 |
* Sets a float in the {@link DefaultScope} |
| 375 |
* |
| 376 |
* @param qualifier |
| 377 |
* @param key |
| 378 |
* the key |
| 379 |
* @param value |
| 380 |
* the new value |
| 381 |
*/ |
| 382 |
public static synchronized void setDefaultFloat(String qualifier, String key, float value) { |
| 383 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putFloat(key, value); |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Sets a integer in the {@link DefaultScope} |
| 388 |
* |
| 389 |
* @param qualifier |
| 390 |
* @param key |
| 391 |
* the key |
| 392 |
* @param value |
| 393 |
* the new value |
| 394 |
*/ |
| 395 |
public static synchronized void setDefaultInt(String qualifier, String key, int value) { |
| 396 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putInt(key, value); |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Sets a long in the {@link DefaultScope} |
| 401 |
* |
| 402 |
* @param qualifier |
| 403 |
* @param key |
| 404 |
* the key |
| 405 |
* @param value |
| 406 |
* the new value |
| 407 |
*/ |
| 408 |
public static synchronized void setDefaultLong(String qualifier, String key, long value) { |
| 409 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).putLong(key, value); |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Sets a string in the {@link DefaultScope} |
| 414 |
* |
| 415 |
* @param qualifier |
| 416 |
* @param key |
| 417 |
* the key |
| 418 |
* @param value |
| 419 |
* the new value |
| 420 |
*/ |
| 421 |
public static synchronized void setDefaultString(String qualifier, String key, String value) { |
| 422 |
contexts[DEFAULT_CONTEXT].getNode(qualifier).put(key, value); |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Sets a double preference in the {@link InstanceScope}. |
| 427 |
* |
| 428 |
* @param qualifier |
| 429 |
* @param key |
| 430 |
* the key |
| 431 |
* @param value |
| 432 |
* the value |
| 433 |
*/ |
| 434 |
public static synchronized void setDouble(String qualifier, String key, double value) { |
| 435 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putDouble(key, value); |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Sets a float preference in the {@link InstanceScope}. |
| 440 |
* |
| 441 |
* @param qualifier |
| 442 |
* @param key |
| 443 |
* the key |
| 444 |
* @param value |
| 445 |
* the value |
| 446 |
*/ |
| 447 |
public static synchronized void setFloat(String qualifier, String key, float value) { |
| 448 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putFloat(key, value); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Sets a integer preference in the {@link InstanceScope}. |
| 453 |
* |
| 454 |
* @param qualifier |
| 455 |
* @param key |
| 456 |
* the key |
| 457 |
* @param value |
| 458 |
* the value |
| 459 |
*/ |
| 460 |
public static synchronized void setInt(String qualifier, String key, int value) { |
| 461 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putInt(key, value); |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Sets a long preference in the {@link InstanceScope}. |
| 466 |
* |
| 467 |
* @param qualifier |
| 468 |
* @param key |
| 469 |
* the key |
| 470 |
* @param value |
| 471 |
* the value |
| 472 |
*/ |
| 473 |
public static synchronized void setLong(String qualifier, String key, long value) { |
| 474 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).putLong(key, value); |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* Sets a string preference in the {@link InstanceScope}. |
| 479 |
* |
| 480 |
* @param qualifier |
| 481 |
* @param key |
| 482 |
* the key |
| 483 |
* @param value |
| 484 |
* the value |
| 485 |
*/ |
| 486 |
public static synchronized void setString(String qualifier, String key, String value) { |
| 487 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).put(key, value); |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Sets the given preference to its default value. This is done by removing |
| 492 |
* any set value from the {@link InstanceScope}. Has no effect if the given |
| 493 |
* key is <code>null</code>. |
| 494 |
* |
| 495 |
* @param qualifier |
| 496 |
* @param key |
| 497 |
* the key for the preference |
| 498 |
*/ |
| 499 |
public static synchronized void setToDefault(String qualifier, String key) { |
| 500 |
if (key != null) { |
| 501 |
contexts[INSTANCE_CONTEXT].getNode(qualifier).remove(key); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
/** |
| 506 |
* Constructor |
| 507 |
*/ |
| 508 |
private Preferences() { |
| 509 |
// no direct instantiation |
| 510 |
} |
| 511 |
} |