1 11 package org.eclipse.core.internal.preferences.legacy; 12 13 import java.io.*; 14 import java.util.Iterator ; 15 import java.util.Properties ; 16 import org.eclipse.core.internal.preferences.*; 17 import org.eclipse.core.internal.runtime.RuntimeLog; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.core.runtime.preferences.DefaultScope; 20 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 21 import org.eclipse.core.runtime.preferences.InstanceScope; 22 import org.osgi.service.prefs.BackingStoreException; 23 24 33 public class PreferenceForwarder extends Preferences implements IEclipsePreferences.IPreferenceChangeListener, IEclipsePreferences.INodeChangeListener { 34 35 private static final byte[] BYTE_ARRAY_DEFAULT_DEFAULT = new byte[0]; 36 37 private IEclipsePreferences pluginRoot = (IEclipsePreferences) PreferencesService.getDefault().getRootNode().node(InstanceScope.SCOPE); 38 private DefaultPreferences defaultsRoot = (DefaultPreferences) PreferencesService.getDefault().getRootNode().node(DefaultScope.SCOPE); 39 private String pluginID; 40 private Object plugin; 41 private boolean notify = true; 44 45 48 public PreferenceForwarder(String pluginID) { 49 this(null, pluginID); 50 } 51 52 public PreferenceForwarder(Object plugin, String pluginID) { 53 super(); 54 this.plugin = plugin; 55 this.pluginID = pluginID; 56 } 57 58 61 public synchronized void added(IEclipsePreferences.NodeChangeEvent event) { 62 if (listeners.size() > 0 && pluginID.equals(event.getChild().name())) { 63 try { 64 EclipsePreferences prefs = (EclipsePreferences) event.getChild(); 65 prefs.addPreferenceChangeListener(this); 66 } catch (ClassCastException e) { 67 throw new RuntimeException ("Plug-in preferences must be instances of EclipsePreferences: " + e.getMessage()); } 69 } 70 } 71 72 75 public synchronized void removed(IEclipsePreferences.NodeChangeEvent event) { 76 } 79 80 86 public synchronized void addPropertyChangeListener(IPropertyChangeListener listener) { 87 if (listeners.size() == 0) { 88 EclipsePreferences prefs = getPluginPreferences(false); 89 if (prefs != null) { 90 prefs.addPreferenceChangeListener(this); 91 } 92 pluginRoot.addNodeChangeListener(this); 93 } 94 listeners.add(listener); 95 } 96 97 98 101 public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) { 102 if (!notify) 104 return; 105 Object oldValue = event.getOldValue(); 106 Object newValue = event.getNewValue(); 107 String key = event.getKey(); 108 if (newValue == null) 109 newValue = getDefault(key, oldValue); 110 else if (oldValue == null) 111 oldValue = getDefault(key, newValue); 112 firePropertyChangeEvent(key, oldValue, newValue); 113 } 114 115 private EclipsePreferences getPluginPreferences(boolean create) { 116 try { 117 if (!create && !pluginRoot.nodeExists(pluginID)) 118 return null; 119 } catch (BackingStoreException e) { 120 return null; 121 } 122 try { 123 return (EclipsePreferences) pluginRoot.node(pluginID); 124 } catch (ClassCastException e) { 125 throw new RuntimeException ("Plug-in preferences must be instances of EclipsePreferences: " + e.getMessage()); } 127 } 128 129 private IEclipsePreferences getDefaultPreferences() { 130 return defaultsRoot.node(pluginID, plugin); 131 } 132 133 139 public synchronized void removePropertyChangeListener(IPropertyChangeListener listener) { 140 listeners.remove(listener); 141 if (listeners.size() == 0) { 142 EclipsePreferences prefs = getPluginPreferences(false); 143 if (prefs != null) { 144 prefs.removePreferenceChangeListener(this); 145 } 146 pluginRoot.removeNodeChangeListener(this); 147 } 148 } 149 150 151 157 private Object getDefault(String key, Object obj) { 158 IEclipsePreferences defaults = getDefaultPreferences(); 159 if (obj instanceof String ) 160 return defaults.get(key, STRING_DEFAULT_DEFAULT); 161 else if (obj instanceof Integer ) 162 return new Integer (defaults.getInt(key, INT_DEFAULT_DEFAULT)); 163 else if (obj instanceof Double ) 164 return new Double (defaults.getDouble(key, DOUBLE_DEFAULT_DEFAULT)); 165 else if (obj instanceof Float ) 166 return new Float (defaults.getFloat(key, FLOAT_DEFAULT_DEFAULT)); 167 else if (obj instanceof Long ) 168 return new Long (defaults.getLong(key, LONG_DEFAULT_DEFAULT)); 169 else if (obj instanceof byte[]) 170 return defaults.getByteArray(key, BYTE_ARRAY_DEFAULT_DEFAULT); 171 else if (obj instanceof Boolean ) 172 return defaults.getBoolean(key, BOOLEAN_DEFAULT_DEFAULT) ? Boolean.TRUE : Boolean.FALSE; 173 else 174 return null; 175 } 176 177 186 public boolean contains(String name) { 187 if (name == null) 188 return false; 189 String value = getPluginPreferences(true).get(name, null); 190 if (value != null) 191 return true; 192 return getDefaultPreferences().get(name, null) != null; 193 } 194 195 205 public boolean getBoolean(String name) { 206 return getPluginPreferences(true).getBoolean(name, getDefaultPreferences().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT)); 207 } 208 209 228 public void setValue(String name, boolean value) { 229 Boolean oldValue = getBoolean(name) ? Boolean.TRUE : Boolean.FALSE; 230 Boolean newValue = value ? Boolean.TRUE : Boolean.FALSE; 231 if (newValue == oldValue) 232 return; 233 try { 234 notify = false; 235 if (getDefaultBoolean(name) == value) 236 getPluginPreferences(true).remove(name); 237 else 238 getPluginPreferences(true).putBoolean(name, value); 239 firePropertyChangeEvent(name, oldValue, newValue); 240 } finally { 241 notify = true; 242 } 243 } 244 245 255 public boolean getDefaultBoolean(String name) { 256 return getDefaultPreferences().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT); 257 } 258 259 274 public void setDefault(String name, boolean value) { 275 getDefaultPreferences().putBoolean(name, value); 276 } 277 278 288 public double getDouble(String name) { 289 return getPluginPreferences(true).getDouble(name, getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT)); 290 } 291 292 312 public void setValue(String name, double value) { 313 if (Double.isNaN(value)) 314 throw new IllegalArgumentException (); 315 final double doubleValue = getDouble(name); 316 if (value == doubleValue) 317 return; 318 Double oldValue = new Double (doubleValue); 319 Double newValue = new Double (value); 320 try { 321 notify = false; 322 if (getDefaultDouble(name) == value) 323 getPluginPreferences(true).remove(name); 324 else 325 getPluginPreferences(true).putDouble(name, value); 326 firePropertyChangeEvent(name, oldValue, newValue); 327 } finally { 328 notify = true; 329 } 330 } 331 332 342 public double getDefaultDouble(String name) { 343 return getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT); 344 } 345 346 362 public void setDefault(String name, double value) { 363 if (Double.isNaN(value)) 364 throw new IllegalArgumentException (); 365 getDefaultPreferences().putDouble(name, value); 366 } 367 368 378 public float getFloat(String name) { 379 return getPluginPreferences(true).getFloat(name, getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT)); 380 } 381 382 402 public void setValue(String name, float value) { 403 if (Float.isNaN(value)) 404 throw new IllegalArgumentException (); 405 final float floatValue = getFloat(name); 406 if (value == floatValue) 407 return; 408 Float oldValue = new Float (floatValue); 409 Float newValue = new Float (value); 410 try { 411 notify = false; 412 if (getDefaultFloat(name) == value) 413 getPluginPreferences(true).remove(name); 414 else 415 getPluginPreferences(true).putFloat(name, value); 416 firePropertyChangeEvent(name, oldValue, newValue); 417 } finally { 418 notify = true; 419 } 420 } 421 422 432 public float getDefaultFloat(String name) { 433 return getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT); 434 } 435 436 452 public void setDefault(String name, float value) { 453 if (Float.isNaN(value)) 454 throw new IllegalArgumentException (); 455 getDefaultPreferences().putFloat(name, value); 456 } 457 458 468 public int getInt(String name) { 469 return getPluginPreferences(true).getInt(name, getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT)); 470 } 471 472 491 public void setValue(String name, int value) { 492 final int intValue = getInt(name); 493 if (value == intValue) 494 return; 495 Integer oldValue = new Integer (intValue); 496 Integer newValue = new Integer (value); 497 try { 498 notify = false; 499 if (getDefaultInt(name) == value) 500 getPluginPreferences(true).remove(name); 501 else 502 getPluginPreferences(true).putInt(name, value); 503 firePropertyChangeEvent(name, oldValue, newValue); 504 } finally { 505 notify = true; 506 } 507 } 508 509 519 public int getDefaultInt(String name) { 520 return getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT); 521 } 522 523 538 public void setDefault(String name, int value) { 539 getDefaultPreferences().putInt(name, value); 540 } 541 542 552 public long getLong(String name) { 553 return getPluginPreferences(true).getLong(name, getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT)); 554 } 555 556 575 public void setValue(String name, long value) { 576 final long longValue = getLong(name); 577 if (value == longValue) 578 return; 579 Long oldValue = new Long (longValue); 580 Long newValue = new Long (value); 581 try { 582 notify = false; 583 if (getDefaultLong(name) == value) 584 getPluginPreferences(true).remove(name); 585 else 586 getPluginPreferences(true).putLong(name, value); 587 firePropertyChangeEvent(name, oldValue, newValue); 588 } finally { 589 notify = true; 590 } 591 } 592 593 603 public long getDefaultLong(String name) { 604 return getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT); 605 } 606 607 622 public void setDefault(String name, long value) { 623 getDefaultPreferences().putLong(name, value); 624 } 625 626 635 public String getString(String name) { 636 return getPluginPreferences(true).get(name, getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT)); 637 } 638 639 658 public void setValue(String name, String value) { 659 if (value == null) 660 throw new IllegalArgumentException (); 661 String oldValue = getString(name); 662 if (value.equals(oldValue)) 663 return; 664 try { 665 notify = false; 666 if (getDefaultString(name).equals(value)) 667 getPluginPreferences(true).remove(name); 668 else 669 getPluginPreferences(true).put(name, value); 670 firePropertyChangeEvent(name, oldValue, value); 671 } finally { 672 notify = true; 673 } 674 } 675 676 686 public String getDefaultString(String name) { 687 return getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT); 688 } 689 690 705 public void setDefault(String name, String value) { 706 if (value == null) 707 throw new IllegalArgumentException (); 708 getDefaultPreferences().put(name, value); 709 } 710 711 720 public boolean isDefault(String name) { 721 if (name == null) 722 return false; 723 return getPluginPreferences(true).get(name, null) == null; 724 } 725 726 745 public void setToDefault(String name) { 746 IEclipsePreferences preferences = getPluginPreferences(true); 747 Object oldValue = preferences.get(name, null); 748 if (oldValue != null) 749 preferences.remove(name); 750 } 751 752 758 public String [] propertyNames() { 759 return getPluginPreferences(true).keys(); 760 } 761 762 768 public String [] defaultPropertyNames() { 769 try { 770 return getDefaultPreferences().keys(); 771 } catch (BackingStoreException e) { 772 logError(e.getMessage(), e); 773 return new String [0]; 774 } 775 } 776 777 785 public boolean needsSaving() { 786 return getPluginPreferences(true).isDirty(); 787 } 788 789 794 public void flush() throws BackingStoreException { 795 IEclipsePreferences node = getPluginPreferences(false); 796 if (node != null) 797 node.flush(); 798 } 799 800 803 private void logError(String message, Exception e) { 804 IStatus status = new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e); 805 RuntimeLog.log(status); 806 } 807 808 811 public void load(InputStream in) throws IOException { 812 Properties result = new Properties (); 813 result.load(in); 814 convertFromProperties(result); 815 try { 819 flush(); 820 } catch (BackingStoreException e) { 821 throw new IOException(e.getMessage()); 822 } 823 } 824 825 828 public void store(OutputStream out, String header) throws IOException { 829 Properties result = convertToProperties(); 830 result.store(out, header); 831 try { 835 flush(); 836 } catch (BackingStoreException e) { 837 throw new IOException(e.getMessage()); 838 } 839 } 840 841 private void convertFromProperties(Properties props) { 842 IEclipsePreferences preferences = getPluginPreferences(true); 843 for (Iterator i = props.keySet().iterator(); i.hasNext();) { 844 String key = (String ) i.next(); 845 String value = props.getProperty(key); 846 if (value != null) 847 preferences.put(key, value); 848 } 849 } 850 851 public String toString() { 852 return "PreferenceForwarder(" + pluginID + ")"; } 854 855 859 private Properties convertToProperties() { 860 Properties result = new Properties (); 861 String [] keys = propertyNames(); 862 for (int i = 0; i < keys.length; i++) { 863 String key = keys[i]; 864 String value = getString(key); 865 if (!Preferences.STRING_DEFAULT_DEFAULT.equals(value)) 866 result.put(key, value); 867 } 868 return result; 869 } 870 } 871 | Popular Tags |