1 11 package org.eclipse.core.internal.preferences; 12 13 import java.io.*; 14 import java.util.Iterator ; 15 import java.util.Properties ; 16 import org.eclipse.core.internal.runtime.InternalPlatform; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.core.runtime.preferences.DefaultScope; 19 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 20 import org.osgi.service.prefs.BackingStoreException; 21 22 31 public class PreferenceForwarder extends Preferences implements IEclipsePreferences.IPreferenceChangeListener, IEclipsePreferences.INodeChangeListener { 32 33 private static final byte[] BYTE_ARRAY_DEFAULT_DEFAULT = new byte[0]; 34 35 private IEclipsePreferences pluginRoot = (IEclipsePreferences) Platform.getPreferencesService().getRootNode().node(Plugin.PLUGIN_PREFERENCE_SCOPE); 36 private DefaultPreferences defaultsRoot = (DefaultPreferences) Platform.getPreferencesService().getRootNode().node(DefaultScope.SCOPE); 37 private String pluginID; 38 private Plugin plugin; 39 private boolean notify = true; 42 43 46 public PreferenceForwarder(String pluginID) { 47 this(null, pluginID); 48 } 49 50 public PreferenceForwarder(Plugin plugin, String pluginID) { 51 super(); 52 this.plugin = plugin; 53 this.pluginID = pluginID; 54 pluginRoot.addNodeChangeListener(this); 55 } 56 57 60 public void added(IEclipsePreferences.NodeChangeEvent event) { 61 if (listeners.size() > 0 && pluginID.equals(event.getChild().name())) 62 getPluginPreferences(true).addPreferenceChangeListener(this); 63 } 64 65 68 public void removed(IEclipsePreferences.NodeChangeEvent event) { 69 } 72 73 79 public void addPropertyChangeListener(IPropertyChangeListener listener) { 80 getPluginPreferences(true).addPreferenceChangeListener(this); 81 listeners.add(listener); 82 } 83 84 87 public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) { 88 if (!notify) 90 return; 91 Object oldValue = event.getOldValue(); 92 Object newValue = event.getNewValue(); 93 String key = event.getKey(); 94 if (newValue == null) 95 newValue = getDefault(key, oldValue); 96 else if (oldValue == null) 97 oldValue = getDefault(key, newValue); 98 firePropertyChangeEvent(key, oldValue, newValue); 99 } 100 101 private EclipsePreferences getPluginPreferences(boolean create) { 102 try { 103 if (!create && !pluginRoot.nodeExists(pluginID)) 104 return null; 105 } catch (BackingStoreException e) { 106 return null; 107 } 108 try { 109 return (EclipsePreferences) pluginRoot.node(pluginID); 110 } catch (ClassCastException e) { 111 throw new RuntimeException ("Plug-in preferences must be instances of EclipsePreferences: " + e.getMessage()); } 113 } 114 115 private IEclipsePreferences getDefaultPreferences() { 116 return defaultsRoot.node(pluginID, plugin); 117 } 118 119 125 public void removePropertyChangeListener(IPropertyChangeListener listener) { 126 listeners.remove(listener); 127 } 128 129 135 private Object getDefault(String key, Object obj) { 136 IEclipsePreferences defaults = getDefaultPreferences(); 137 if (obj instanceof String ) 138 return defaults.get(key, STRING_DEFAULT_DEFAULT); 139 else if (obj instanceof Integer ) 140 return new Integer (defaults.getInt(key, INT_DEFAULT_DEFAULT)); 141 else if (obj instanceof Double ) 142 return new Double (defaults.getDouble(key, DOUBLE_DEFAULT_DEFAULT)); 143 else if (obj instanceof Float ) 144 return new Float (defaults.getFloat(key, FLOAT_DEFAULT_DEFAULT)); 145 else if (obj instanceof Long ) 146 return new Long (defaults.getLong(key, LONG_DEFAULT_DEFAULT)); 147 else if (obj instanceof byte[]) 148 return defaults.getByteArray(key, BYTE_ARRAY_DEFAULT_DEFAULT); 149 else if (obj instanceof Boolean ) 150 return new Boolean (defaults.getBoolean(key, BOOLEAN_DEFAULT_DEFAULT)); 151 else 152 return null; 153 } 154 155 164 public boolean contains(String name) { 165 if (name == null) 166 return false; 167 String value = getPluginPreferences(true).get(name, null); 168 if (value != null) 169 return true; 170 return getDefaultPreferences().get(name, null) != null; 171 } 172 173 183 public boolean getBoolean(String name) { 184 return getPluginPreferences(true).getBoolean(name, getDefaultPreferences().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT)); 185 } 186 187 206 public void setValue(String name, boolean value) { 207 Boolean oldValue = getBoolean(name) ? Boolean.TRUE : Boolean.FALSE; 208 Boolean newValue = value ? Boolean.TRUE : Boolean.FALSE; 209 if (newValue == oldValue) 210 return; 211 try { 212 notify = false; 213 if (getDefaultBoolean(name) == value) 214 getPluginPreferences(true).remove(name); 215 else 216 getPluginPreferences(true).putBoolean(name, value); 217 firePropertyChangeEvent(name, oldValue, newValue); 218 } finally { 219 notify = true; 220 } 221 } 222 223 233 public boolean getDefaultBoolean(String name) { 234 return getDefaultPreferences().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT); 235 } 236 237 252 public void setDefault(String name, boolean value) { 253 getDefaultPreferences().putBoolean(name, value); 254 } 255 256 266 public double getDouble(String name) { 267 return getPluginPreferences(true).getDouble(name, getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT)); 268 } 269 270 290 public void setValue(String name, double value) { 291 if (Double.isNaN(value)) 292 throw new IllegalArgumentException (); 293 final double doubleValue = getDouble(name); 294 if (value == doubleValue) 295 return; 296 Double oldValue = new Double (doubleValue); 297 Double newValue = new Double (value); 298 try { 299 notify = false; 300 if (getDefaultDouble(name) == value) 301 getPluginPreferences(true).remove(name); 302 else 303 getPluginPreferences(true).putDouble(name, value); 304 firePropertyChangeEvent(name, oldValue, newValue); 305 } finally { 306 notify = true; 307 } 308 } 309 310 320 public double getDefaultDouble(String name) { 321 return getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT); 322 } 323 324 340 public void setDefault(String name, double value) { 341 if (Double.isNaN(value)) 342 throw new IllegalArgumentException (); 343 getDefaultPreferences().putDouble(name, value); 344 } 345 346 356 public float getFloat(String name) { 357 return getPluginPreferences(true).getFloat(name, getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT)); 358 } 359 360 380 public void setValue(String name, float value) { 381 if (Float.isNaN(value)) 382 throw new IllegalArgumentException (); 383 final float floatValue = getFloat(name); 384 if (value == floatValue) 385 return; 386 Float oldValue = new Float (floatValue); 387 Float newValue = new Float (value); 388 try { 389 notify = false; 390 if (getDefaultFloat(name) == value) 391 getPluginPreferences(true).remove(name); 392 else 393 getPluginPreferences(true).putFloat(name, value); 394 firePropertyChangeEvent(name, oldValue, newValue); 395 } finally { 396 notify = true; 397 } 398 } 399 400 410 public float getDefaultFloat(String name) { 411 return getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT); 412 } 413 414 430 public void setDefault(String name, float value) { 431 if (Float.isNaN(value)) 432 throw new IllegalArgumentException (); 433 getDefaultPreferences().putFloat(name, value); 434 } 435 436 446 public int getInt(String name) { 447 return getPluginPreferences(true).getInt(name, getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT)); 448 } 449 450 469 public void setValue(String name, int value) { 470 final int intValue = getInt(name); 471 if (value == intValue) 472 return; 473 Integer oldValue = new Integer (intValue); 474 Integer newValue = new Integer (value); 475 try { 476 notify = false; 477 if (getDefaultInt(name) == value) 478 getPluginPreferences(true).remove(name); 479 else 480 getPluginPreferences(true).putInt(name, value); 481 firePropertyChangeEvent(name, oldValue, newValue); 482 } finally { 483 notify = true; 484 } 485 } 486 487 497 public int getDefaultInt(String name) { 498 return getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT); 499 } 500 501 516 public void setDefault(String name, int value) { 517 getDefaultPreferences().putInt(name, value); 518 } 519 520 530 public long getLong(String name) { 531 return getPluginPreferences(true).getLong(name, getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT)); 532 } 533 534 553 public void setValue(String name, long value) { 554 final long longValue = getLong(name); 555 if (value == longValue) 556 return; 557 Long oldValue = new Long (longValue); 558 Long newValue = new Long (value); 559 try { 560 notify = false; 561 if (getDefaultLong(name) == value) 562 getPluginPreferences(true).remove(name); 563 else 564 getPluginPreferences(true).putLong(name, value); 565 firePropertyChangeEvent(name, oldValue, newValue); 566 } finally { 567 notify = true; 568 } 569 } 570 571 581 public long getDefaultLong(String name) { 582 return getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT); 583 } 584 585 600 public void setDefault(String name, long value) { 601 getDefaultPreferences().putLong(name, value); 602 } 603 604 613 public String getString(String name) { 614 return getPluginPreferences(true).get(name, getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT)); 615 } 616 617 636 public void setValue(String name, String value) { 637 if (value == null) 638 throw new IllegalArgumentException (); 639 String oldValue = getString(name); 640 if (value.equals(oldValue)) 641 return; 642 try { 643 notify = false; 644 if (getDefaultString(name).equals(value)) 645 getPluginPreferences(true).remove(name); 646 else 647 getPluginPreferences(true).put(name, value); 648 firePropertyChangeEvent(name, oldValue, value); 649 } finally { 650 notify = true; 651 } 652 } 653 654 664 public String getDefaultString(String name) { 665 return getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT); 666 } 667 668 683 public void setDefault(String name, String value) { 684 if (value == null) 685 throw new IllegalArgumentException (); 686 getDefaultPreferences().put(name, value); 687 } 688 689 698 public boolean isDefault(String name) { 699 if (name == null) 700 return false; 701 return getPluginPreferences(true).get(name, null) == null; 702 } 703 704 723 public void setToDefault(String name) { 724 IEclipsePreferences preferences = getPluginPreferences(true); 725 Object oldValue = preferences.get(name, null); 726 if (oldValue != null) 727 preferences.remove(name); 728 } 729 730 736 public String [] propertyNames() { 737 return getPluginPreferences(true).keys(); 738 } 739 740 746 public String [] defaultPropertyNames() { 747 try { 748 return getDefaultPreferences().keys(); 749 } catch (BackingStoreException e) { 750 logError(e.getMessage(), e); 751 return new String [0]; 752 } 753 } 754 755 763 public boolean needsSaving() { 764 return getPluginPreferences(true).dirty; 765 } 766 767 772 public void flush() throws BackingStoreException { 773 IEclipsePreferences node = getPluginPreferences(false); 774 if (node != null) 775 node.flush(); 776 } 777 778 781 private void logError(String message, Exception e) { 782 IStatus status = new Status(IStatus.ERROR, Platform.PI_RUNTIME, IStatus.ERROR, message, e); 783 InternalPlatform.getDefault().log(status); 784 } 785 786 789 public void load(InputStream in) throws IOException { 790 Properties result = new Properties (); 791 result.load(in); 792 convertFromProperties(result); 793 try { 797 flush(); 798 } catch (BackingStoreException e) { 799 throw new IOException(e.getMessage()); 800 } 801 } 802 803 806 public void store(OutputStream out, String header) throws IOException { 807 Properties result = convertToProperties(); 808 result.store(out, header); 809 try { 813 flush(); 814 } catch (BackingStoreException e) { 815 throw new IOException(e.getMessage()); 816 } 817 } 818 819 private void convertFromProperties(Properties props) { 820 IEclipsePreferences preferences = getPluginPreferences(true); 821 for (Iterator i = props.keySet().iterator(); i.hasNext();) { 822 String key = (String ) i.next(); 823 String value = props.getProperty(key); 824 if (value != null) 825 preferences.put(key, value); 826 } 827 } 828 829 public String toString() { 830 return "PreferenceForwarder(" + pluginID + ")"; } 832 833 837 private Properties convertToProperties() { 838 Properties result = new Properties (); 839 String [] keys = propertyNames(); 840 for (int i = 0; i < keys.length; i++) { 841 String key = keys[i]; 842 String value = getString(key); 843 if (!Preferences.STRING_DEFAULT_DEFAULT.equals(value)) 844 result.put(key, value); 845 } 846 return result; 847 } 848 } 849 | Popular Tags |