1 11 package org.eclipse.ui.preferences; 12 13 import java.io.IOException ; 14 15 import org.eclipse.core.commands.common.EventManager; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.Platform; 18 import org.eclipse.core.runtime.Plugin; 19 import org.eclipse.core.runtime.SafeRunner; 20 import org.eclipse.core.runtime.preferences.DefaultScope; 21 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 22 import org.eclipse.core.runtime.preferences.IScopeContext; 23 import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener; 24 import org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent; 25 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; 26 import org.eclipse.jface.preference.IPersistentPreferenceStore; 27 import org.eclipse.jface.preference.IPreferenceStore; 28 import org.eclipse.jface.resource.JFaceResources; 29 import org.eclipse.jface.util.IPropertyChangeListener; 30 import org.eclipse.jface.util.PropertyChangeEvent; 31 import org.eclipse.jface.util.SafeRunnable; 32 import org.eclipse.ui.internal.WorkbenchMessages; 33 import org.osgi.service.prefs.BackingStoreException; 34 35 50 public class ScopedPreferenceStore extends EventManager implements 51 IPreferenceStore, IPersistentPreferenceStore { 52 53 58 private IScopeContext storeContext; 59 60 64 private IScopeContext[] searchContexts; 65 66 69 protected boolean silentRunning = false; 70 71 75 IEclipsePreferences.IPreferenceChangeListener preferencesListener; 76 77 81 private IScopeContext defaultContext = new DefaultScope(); 82 83 86 String nodeQualifier; 87 88 91 String defaultQualifier; 92 93 97 private boolean dirty; 98 99 111 public ScopedPreferenceStore(IScopeContext context, String qualifier, 112 String defaultQualifierPath) { 113 this(context, qualifier); 114 this.defaultQualifier = defaultQualifierPath; 115 } 116 117 126 public ScopedPreferenceStore(IScopeContext context, String qualifier) { 127 storeContext = context; 128 this.nodeQualifier = qualifier; 129 this.defaultQualifier = qualifier; 130 131 ((IEclipsePreferences) getStorePreferences().parent()) 132 .addNodeChangeListener(getNodeChangeListener()); 133 } 134 135 141 private INodeChangeListener getNodeChangeListener() { 142 return new IEclipsePreferences.INodeChangeListener() { 143 148 public void added(NodeChangeEvent event) { 149 if (nodeQualifier.equals(event.getChild().name()) 150 && isListenerAttached()) { 151 getStorePreferences().addPreferenceChangeListener( 152 preferencesListener); 153 } 154 } 155 156 161 public void removed(NodeChangeEvent event) { 162 } 164 }; 165 } 166 167 170 private void initializePreferencesListener() { 171 if (preferencesListener == null) { 172 preferencesListener = new IEclipsePreferences.IPreferenceChangeListener() { 173 178 public void preferenceChange(PreferenceChangeEvent event) { 179 180 if (silentRunning) { 181 return; 182 } 183 184 Object oldValue = event.getOldValue(); 185 Object newValue = event.getNewValue(); 186 String key = event.getKey(); 187 if (newValue == null) { 188 newValue = getDefault(key, oldValue); 189 } else if (oldValue == null) { 190 oldValue = getDefault(key, newValue); 191 } 192 firePropertyChangeEvent(event.getKey(), oldValue, newValue); 193 } 194 }; 195 getStorePreferences().addPreferenceChangeListener( 196 preferencesListener); 197 } 198 199 } 200 201 213 Object getDefault(String key, Object obj) { 214 IEclipsePreferences defaults = getDefaultPreferences(); 215 if (obj instanceof String ) { 216 return defaults.get(key, STRING_DEFAULT_DEFAULT); 217 } else if (obj instanceof Integer ) { 218 return new Integer (defaults.getInt(key, INT_DEFAULT_DEFAULT)); 219 } else if (obj instanceof Double ) { 220 return new Double (defaults.getDouble(key, DOUBLE_DEFAULT_DEFAULT)); 221 } else if (obj instanceof Float ) { 222 return new Float (defaults.getFloat(key, FLOAT_DEFAULT_DEFAULT)); 223 } else if (obj instanceof Long ) { 224 return new Long (defaults.getLong(key, LONG_DEFAULT_DEFAULT)); 225 } else if (obj instanceof Boolean ) { 226 return defaults.getBoolean(key, BOOLEAN_DEFAULT_DEFAULT) ? Boolean.TRUE : Boolean.FALSE; 227 } else { 228 return null; 229 } 230 } 231 232 237 IEclipsePreferences getStorePreferences() { 238 return storeContext.getNode(nodeQualifier); 239 } 240 241 246 private IEclipsePreferences getDefaultPreferences() { 247 return defaultContext.getNode(defaultQualifier); 248 } 249 250 255 public void addPropertyChangeListener(IPropertyChangeListener listener) { 256 initializePreferencesListener(); addListenerObject(listener); 259 } 260 261 275 private IEclipsePreferences[] getPreferenceNodes(boolean includeDefault) { 276 if (searchContexts == null) { 279 if (includeDefault) { 280 return new IEclipsePreferences[] { getStorePreferences(), 281 getDefaultPreferences() }; 282 } 283 return new IEclipsePreferences[] { getStorePreferences() }; 284 } 285 int length = searchContexts.length; 288 if (includeDefault) { 289 length++; 290 } 291 IEclipsePreferences[] preferences = new IEclipsePreferences[length]; 292 for (int i = 0; i < searchContexts.length; i++) { 293 preferences[i] = searchContexts[i].getNode(nodeQualifier); 294 } 295 if (includeDefault) { 296 preferences[length - 1] = getDefaultPreferences(); 297 } 298 return preferences; 299 } 300 301 319 public void setSearchContexts(IScopeContext[] scopes) { 320 this.searchContexts = scopes; 321 if (scopes == null) { 322 return; 323 } 324 325 for (int i = 0; i < scopes.length; i++) { 328 if (scopes[i].equals(defaultContext)) { 329 Assert 330 .isTrue( 331 false, 332 WorkbenchMessages.ScopedPreferenceStore_DefaultAddedError); 333 } 334 } 335 } 336 337 342 public boolean contains(String name) { 343 if (name == null) { 344 return false; 345 } 346 return (Platform.getPreferencesService().get(name, null, 347 getPreferenceNodes(true))) != null; 348 } 349 350 356 public void firePropertyChangeEvent(String name, Object oldValue, 357 Object newValue) { 358 final Object [] list = getListeners(); 361 if (list.length == 0) { 362 return; 363 } 364 final PropertyChangeEvent event = new PropertyChangeEvent(this, name, 365 oldValue, newValue); 366 for (int i = 0; i < list.length; i++) { 367 final IPropertyChangeListener listener = (IPropertyChangeListener) list[i]; 368 SafeRunner.run(new SafeRunnable(JFaceResources 369 .getString("PreferenceStore.changeError")) { public void run() { 371 listener.propertyChange(event); 372 } 373 }); 374 } 375 } 376 377 382 public boolean getBoolean(String name) { 383 String value = internalGet(name); 384 return value == null ? BOOLEAN_DEFAULT_DEFAULT : Boolean.valueOf(value) 385 .booleanValue(); 386 } 387 388 393 public boolean getDefaultBoolean(String name) { 394 return getDefaultPreferences() 395 .getBoolean(name, BOOLEAN_DEFAULT_DEFAULT); 396 } 397 398 403 public double getDefaultDouble(String name) { 404 return getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT); 405 } 406 407 412 public float getDefaultFloat(String name) { 413 return getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT); 414 } 415 416 421 public int getDefaultInt(String name) { 422 return getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT); 423 } 424 425 430 public long getDefaultLong(String name) { 431 return getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT); 432 } 433 434 439 public String getDefaultString(String name) { 440 return getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT); 441 } 442 443 448 public double getDouble(String name) { 449 String value = internalGet(name); 450 if (value == null) { 451 return DOUBLE_DEFAULT_DEFAULT; 452 } 453 try { 454 return Double.parseDouble(value); 455 } catch (NumberFormatException e) { 456 return DOUBLE_DEFAULT_DEFAULT; 457 } 458 } 459 460 469 private String internalGet(String key) { 470 return Platform.getPreferencesService().get(key, null, 471 getPreferenceNodes(true)); 472 } 473 474 479 public float getFloat(String name) { 480 String value = internalGet(name); 481 if (value == null) { 482 return FLOAT_DEFAULT_DEFAULT; 483 } 484 try { 485 return Float.parseFloat(value); 486 } catch (NumberFormatException e) { 487 return FLOAT_DEFAULT_DEFAULT; 488 } 489 } 490 491 496 public int getInt(String name) { 497 String value = internalGet(name); 498 if (value == null) { 499 return INT_DEFAULT_DEFAULT; 500 } 501 try { 502 return Integer.parseInt(value); 503 } catch (NumberFormatException e) { 504 return INT_DEFAULT_DEFAULT; 505 } 506 } 507 508 513 public long getLong(String name) { 514 String value = internalGet(name); 515 if (value == null) { 516 return LONG_DEFAULT_DEFAULT; 517 } 518 try { 519 return Long.parseLong(value); 520 } catch (NumberFormatException e) { 521 return LONG_DEFAULT_DEFAULT; 522 } 523 } 524 525 530 public String getString(String name) { 531 String value = internalGet(name); 532 return value == null ? STRING_DEFAULT_DEFAULT : value; 533 } 534 535 540 public boolean isDefault(String name) { 541 if (name == null) { 542 return false; 543 } 544 return (Platform.getPreferencesService().get(name, null, 545 getPreferenceNodes(false))) == null; 546 } 547 548 553 public boolean needsSaving() { 554 return dirty; 555 } 556 557 563 public void putValue(String name, String value) { 564 try { 565 silentRunning = true; 567 getStorePreferences().put(name, value); 568 } finally { 569 silentRunning = false; 571 dirty = true; 572 } 573 } 574 575 580 public void removePropertyChangeListener(IPropertyChangeListener listener) { 581 removeListenerObject(listener); 582 if (!isListenerAttached()) { 583 disposePreferenceStoreListener(); 584 } 585 } 586 587 593 public void setDefault(String name, double value) { 594 getDefaultPreferences().putDouble(name, value); 595 } 596 597 603 public void setDefault(String name, float value) { 604 getDefaultPreferences().putFloat(name, value); 605 } 606 607 613 public void setDefault(String name, int value) { 614 getDefaultPreferences().putInt(name, value); 615 } 616 617 623 public void setDefault(String name, long value) { 624 getDefaultPreferences().putLong(name, value); 625 } 626 627 633 public void setDefault(String name, String defaultObject) { 634 getDefaultPreferences().put(name, defaultObject); 635 } 636 637 643 public void setDefault(String name, boolean value) { 644 getDefaultPreferences().putBoolean(name, value); 645 } 646 647 652 public void setToDefault(String name) { 653 654 String oldValue = getString(name); 655 String defaultValue = getDefaultString(name); 656 try { 657 silentRunning = true; getStorePreferences().remove(name); 661 dirty = true; 662 firePropertyChangeEvent(name, oldValue, defaultValue); 663 } finally { 664 silentRunning = false; } 666 667 } 668 669 675 public void setValue(String name, double value) { 676 double oldValue = getDouble(name); 677 if (oldValue == value) { 678 return; 679 } 680 try { 681 silentRunning = true; if (getDefaultDouble(name) == value) { 683 getStorePreferences().remove(name); 684 } else { 685 getStorePreferences().putDouble(name, value); 686 } 687 dirty = true; 688 firePropertyChangeEvent(name, new Double (oldValue), new Double ( 689 value)); 690 } finally { 691 silentRunning = false; } 693 } 694 695 701 public void setValue(String name, float value) { 702 float oldValue = getFloat(name); 703 if (oldValue == value) { 704 return; 705 } 706 try { 707 silentRunning = true; if (getDefaultFloat(name) == value) { 709 getStorePreferences().remove(name); 710 } else { 711 getStorePreferences().putFloat(name, value); 712 } 713 dirty = true; 714 firePropertyChangeEvent(name, new Float (oldValue), new Float (value)); 715 } finally { 716 silentRunning = false; } 718 } 719 720 726 public void setValue(String name, int value) { 727 int oldValue = getInt(name); 728 if (oldValue == value) { 729 return; 730 } 731 try { 732 silentRunning = true; if (getDefaultInt(name) == value) { 734 getStorePreferences().remove(name); 735 } else { 736 getStorePreferences().putInt(name, value); 737 } 738 dirty = true; 739 firePropertyChangeEvent(name, new Integer (oldValue), new Integer ( 740 value)); 741 } finally { 742 silentRunning = false; } 744 } 745 746 752 public void setValue(String name, long value) { 753 long oldValue = getLong(name); 754 if (oldValue == value) { 755 return; 756 } 757 try { 758 silentRunning = true; if (getDefaultLong(name) == value) { 760 getStorePreferences().remove(name); 761 } else { 762 getStorePreferences().putLong(name, value); 763 } 764 dirty = true; 765 firePropertyChangeEvent(name, new Long (oldValue), new Long (value)); 766 } finally { 767 silentRunning = false; } 769 } 770 771 777 public void setValue(String name, String value) { 778 if (getDefaultString(name).equals(value)) { 780 getStorePreferences().remove(name); 781 } else { 782 getStorePreferences().put(name, value); 783 } 784 dirty = true; 785 } 786 787 793 public void setValue(String name, boolean value) { 794 boolean oldValue = getBoolean(name); 795 if (oldValue == value) { 796 return; 797 } 798 try { 799 silentRunning = true; if (getDefaultBoolean(name) == value) { 801 getStorePreferences().remove(name); 802 } else { 803 getStorePreferences().putBoolean(name, value); 804 } 805 dirty = true; 806 firePropertyChangeEvent(name, oldValue ? Boolean.TRUE : Boolean.FALSE, 807 value ? Boolean.TRUE : Boolean.FALSE); 808 } finally { 809 silentRunning = false; } 811 } 812 813 818 public void save() throws IOException { 819 try { 820 getStorePreferences().flush(); 821 dirty = false; 822 } catch (BackingStoreException e) { 823 throw new IOException (e.getMessage()); 824 } 825 826 } 827 828 831 private void disposePreferenceStoreListener() { 832 833 IEclipsePreferences root = (IEclipsePreferences) Platform 834 .getPreferencesService().getRootNode().node( 835 Plugin.PLUGIN_PREFERENCE_SCOPE); 836 try { 837 if (!(root.nodeExists(nodeQualifier))) { 838 return; 839 } 840 } catch (BackingStoreException e) { 841 return; } 844 845 IEclipsePreferences preferences = getStorePreferences(); 846 if (preferences == null) { 847 return; 848 } 849 if (preferencesListener != null) { 850 preferences.removePreferenceChangeListener(preferencesListener); 851 preferencesListener = null; 852 } 853 } 854 855 } 856 | Popular Tags |