1 11 12 package org.eclipse.ui.internal.preferences; 13 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import org.eclipse.core.commands.common.EventManager; 21 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 22 import org.eclipse.core.runtime.preferences.IPreferenceNodeVisitor; 23 import org.osgi.service.prefs.BackingStoreException; 24 import org.osgi.service.prefs.Preferences; 25 26 37 public class WorkingCopyPreferences extends EventManager implements 38 IEclipsePreferences { 39 40 private static final String TRUE = "true"; 42 private final Map temporarySettings; 43 private final IEclipsePreferences original; 44 private boolean removed = false; 45 private org.eclipse.ui.preferences.WorkingCopyManager manager; 46 47 51 public WorkingCopyPreferences(IEclipsePreferences original, org.eclipse.ui.preferences.WorkingCopyManager manager) { 52 super(); 53 this.original = original; 54 this.manager = manager; 55 this.temporarySettings = new HashMap (); 56 } 57 58 62 private void checkRemoved() { 63 if (removed) { 64 String message = "Preference node: " + absolutePath() + " has been removed."; throw new IllegalStateException (message); 66 } 67 } 68 69 72 public void addNodeChangeListener(INodeChangeListener listener) { 73 } 75 76 79 public void removeNodeChangeListener(INodeChangeListener listener) { 80 } 82 83 86 public void addPreferenceChangeListener(IPreferenceChangeListener listener) { 87 checkRemoved(); 88 addListenerObject(listener); 89 } 90 91 94 public void removePreferenceChangeListener(IPreferenceChangeListener listener) { 95 checkRemoved(); 96 removeListenerObject(listener); 97 } 98 99 102 public void removeNode() throws BackingStoreException { 103 checkRemoved(); 104 105 String [] keys = keys(); 107 for (int i = 0; i < keys.length; i++) { 108 remove(keys[i]); 109 } 110 111 String [] childNames = childrenNames(); 113 for (int i = 0; i < childNames.length; i++) { 114 node(childNames[i]).removeNode(); 115 } 116 117 removed = true; 119 } 120 121 122 125 public Preferences node(String path) { 126 checkRemoved(); 127 return manager.getWorkingCopy((IEclipsePreferences) getOriginal().node(path)); 128 } 129 130 133 public void accept(IPreferenceNodeVisitor visitor) throws BackingStoreException { 134 checkRemoved(); 135 if (!visitor.visit(this)) { 136 return; 137 } 138 String [] childNames = childrenNames(); 139 for (int i = 0; i < childNames.length; i++) { 140 ((IEclipsePreferences) node(childNames[i])).accept(visitor); 141 } 142 } 143 144 147 public void put(String key, String value) { 148 checkRemoved(); 149 if (key == null || value == null) { 150 throw new NullPointerException (); 151 } 152 String oldValue = null; 153 if (temporarySettings.containsKey(key)) { 154 oldValue = (String ) temporarySettings.get(key); 155 } else { 156 oldValue = getOriginal().get(key, null); 157 } 158 temporarySettings.put(key, value); 159 if (!value.equals(oldValue)) { 160 firePropertyChangeEvent(key, oldValue, value); 161 } 162 } 163 164 private void firePropertyChangeEvent(String key, Object oldValue, Object newValue) { 165 Object [] listeners = getListeners(); 166 if (listeners.length == 0) { 167 return; 168 } 169 PreferenceChangeEvent event = new PreferenceChangeEvent(this, key, oldValue, newValue); 170 for (int i = 0; i < listeners.length; i++) { 171 ((IPreferenceChangeListener) listeners[i]).preferenceChange(event); 172 } 173 } 174 175 178 public String get(String key, String defaultValue) { 179 checkRemoved(); 180 return internalGet(key, defaultValue); 181 } 182 183 private String internalGet(String key, String defaultValue) { 184 if (key == null) { 185 throw new NullPointerException (); 186 } 187 if (temporarySettings.containsKey(key)) { 188 Object value = temporarySettings.get(key); 189 return value == null ? defaultValue : (String ) value; 190 } 191 return getOriginal().get(key, defaultValue); 192 } 193 194 197 public void remove(String key) { 198 checkRemoved(); 199 if (key == null) { 200 throw new NullPointerException (); 201 } 202 Object oldValue = null; 203 if (temporarySettings.containsKey(key)) { 204 oldValue = temporarySettings.get(key); 205 } else { 206 oldValue = original.get(key, null); 207 } 208 if (oldValue == null) { 209 return; 210 } 211 temporarySettings.put(key, null); 212 firePropertyChangeEvent(key, oldValue, null); 213 } 214 215 218 public void clear() { 219 checkRemoved(); 220 for (Iterator i = temporarySettings.keySet().iterator(); i.hasNext();) { 221 String key = (String ) i.next(); 222 Object value = temporarySettings.get(key); 223 if (value != null) { 224 temporarySettings.put(key, null); 225 firePropertyChangeEvent(key, value, null); 226 } 227 } 228 } 229 230 233 public void putInt(String key, int value) { 234 checkRemoved(); 235 if (key == null) { 236 throw new NullPointerException (); 237 } 238 String oldValue = null; 239 if (temporarySettings.containsKey(key)) { 240 oldValue = (String ) temporarySettings.get(key); 241 } else { 242 oldValue = getOriginal().get(key, null); 243 } 244 String newValue = Integer.toString(value); 245 temporarySettings.put(key, newValue); 246 if (!newValue.equals(oldValue)) { 247 firePropertyChangeEvent(key, oldValue, newValue); 248 } 249 } 250 251 254 public int getInt(String key, int defaultValue) { 255 checkRemoved(); 256 String value = internalGet(key, null); 257 int result = defaultValue; 258 if (value != null) { 259 try { 260 result = Integer.parseInt(value); 261 } catch (NumberFormatException e) { 262 } 264 } 265 return result; 266 } 267 268 271 public void putLong(String key, long value) { 272 checkRemoved(); 273 if (key == null) { 274 throw new NullPointerException (); 275 } 276 String oldValue = null; 277 if (temporarySettings.containsKey(key)) { 278 oldValue = (String ) temporarySettings.get(key); 279 } else { 280 oldValue = getOriginal().get(key, null); 281 } 282 String newValue = Long.toString(value); 283 temporarySettings.put(key, newValue); 284 if (!newValue.equals(oldValue)) { 285 firePropertyChangeEvent(key, oldValue, newValue); 286 } 287 } 288 289 292 public long getLong(String key, long defaultValue) { 293 checkRemoved(); 294 String value = internalGet(key, null); 295 long result = defaultValue; 296 if (value != null) { 297 try { 298 result = Long.parseLong(value); 299 } catch (NumberFormatException e) { 300 } 302 } 303 return result; 304 } 305 306 309 public void putBoolean(String key, boolean value) { 310 checkRemoved(); 311 if (key == null) { 312 throw new NullPointerException (); 313 } 314 String oldValue = null; 315 if (temporarySettings.containsKey(key)) { 316 oldValue = (String ) temporarySettings.get(key); 317 } else { 318 oldValue = getOriginal().get(key, null); 319 } 320 String newValue = String.valueOf(value); 321 temporarySettings.put(key, newValue); 322 if (!newValue.equalsIgnoreCase(oldValue)) { 323 firePropertyChangeEvent(key, oldValue, newValue); 324 } 325 } 326 327 330 public boolean getBoolean(String key, boolean defaultValue) { 331 checkRemoved(); 332 String value = internalGet(key, null); 333 return value == null ? defaultValue : TRUE.equalsIgnoreCase(value); 334 } 335 336 339 public void putFloat(String key, float value) { 340 checkRemoved(); 341 if (key == null) { 342 throw new NullPointerException (); 343 } 344 String oldValue = null; 345 if (temporarySettings.containsKey(key)) { 346 oldValue = (String ) temporarySettings.get(key); 347 } else { 348 oldValue = getOriginal().get(key, null); 349 } 350 String newValue = Float.toString(value); 351 temporarySettings.put(key, newValue); 352 if (!newValue.equals(oldValue)) { 353 firePropertyChangeEvent(key, oldValue, newValue); 354 } 355 } 356 357 360 public float getFloat(String key, float defaultValue) { 361 checkRemoved(); 362 String value = internalGet(key, null); 363 float result = defaultValue; 364 if (value != null) { 365 try { 366 result = Float.parseFloat(value); 367 } catch (NumberFormatException e) { 368 } 370 } 371 return result; 372 } 373 374 377 public void putDouble(String key, double value) { 378 checkRemoved(); 379 if (key == null) { 380 throw new NullPointerException (); 381 } 382 String oldValue = null; 383 if (temporarySettings.containsKey(key)) { 384 oldValue = (String ) temporarySettings.get(key); 385 } else { 386 oldValue = getOriginal().get(key, null); 387 } 388 String newValue = Double.toString(value); 389 temporarySettings.put(key, newValue); 390 if (!newValue.equals(oldValue)) { 391 firePropertyChangeEvent(key, oldValue, newValue); 392 } 393 } 394 395 398 public double getDouble(String key, double defaultValue) { 399 checkRemoved(); 400 String value = internalGet(key, null); 401 double result = defaultValue; 402 if (value != null) { 403 try { 404 result = Double.parseDouble(value); 405 } catch (NumberFormatException e) { 406 } 408 } 409 return result; 410 } 411 412 415 public void putByteArray(String key, byte[] value) { 416 checkRemoved(); 417 if (key == null || value == null) { 418 throw new NullPointerException (); 419 } 420 String oldValue = null; 421 if (temporarySettings.containsKey(key)) { 422 oldValue = (String ) temporarySettings.get(key); 423 } else { 424 oldValue = getOriginal().get(key, null); 425 } 426 String newValue = new String (Base64.encode(value)); 427 temporarySettings.put(key, newValue); 428 if (!newValue.equals(oldValue)) { 429 firePropertyChangeEvent(key, oldValue, newValue); 430 } 431 } 432 433 436 public byte[] getByteArray(String key, byte[] defaultValue) { 437 checkRemoved(); 438 String value = internalGet(key, null); 439 return value == null ? defaultValue : Base64.decode(value.getBytes()); 440 } 441 442 445 public String [] keys() throws BackingStoreException { 446 checkRemoved(); 447 HashSet allKeys = new HashSet (Arrays.asList(getOriginal().keys())); 448 allKeys.addAll(temporarySettings.keySet()); 449 return (String []) allKeys.toArray(new String [allKeys.size()]); 450 } 451 452 455 public String [] childrenNames() throws BackingStoreException { 456 checkRemoved(); 457 return getOriginal().childrenNames(); 458 } 459 460 463 public Preferences parent() { 464 checkRemoved(); 465 return manager.getWorkingCopy((IEclipsePreferences) getOriginal().parent()); 466 } 467 468 471 public boolean nodeExists(String pathName) throws BackingStoreException { 472 if (pathName.length() == 0) { 474 return removed ? false : getOriginal().nodeExists(pathName); 475 } 476 return getOriginal().nodeExists(pathName); 477 } 478 479 482 public String name() { 483 return getOriginal().name(); 484 } 485 486 489 public String absolutePath() { 490 return getOriginal().absolutePath(); 491 } 492 493 496 public void flush() throws BackingStoreException { 497 if (removed) { 498 getOriginal().removeNode(); 499 return; 500 } 501 checkRemoved(); 502 for (Iterator i = temporarySettings.keySet().iterator(); i.hasNext();) { 504 String key = (String ) i.next(); 505 String value = (String ) temporarySettings.get(key); 506 if (value == null) { 507 getOriginal().remove(key); 508 } else { 509 getOriginal().put(key, value); 510 } 511 } 512 temporarySettings.clear(); 514 515 getOriginal().flush(); 517 } 518 519 522 public void sync() throws BackingStoreException { 523 checkRemoved(); 524 temporarySettings.clear(); 526 getOriginal().sync(); 528 } 529 530 533 private IEclipsePreferences getOriginal() { 534 return original; 535 } 536 } 537 | Popular Tags |