1 11 package org.eclipse.core.runtime; 12 13 import java.io.*; 14 import java.util.*; 15 import org.eclipse.core.internal.preferences.PreferencesService; 16 import org.eclipse.core.internal.preferences.PrefsMessages; 17 import org.eclipse.core.runtime.preferences.*; 18 import org.eclipse.osgi.util.NLS; 19 20 88 public class Preferences { 89 90 93 public static final boolean BOOLEAN_DEFAULT_DEFAULT = false; 94 95 98 public static final double DOUBLE_DEFAULT_DEFAULT = 0.0; 99 100 103 public static final float FLOAT_DEFAULT_DEFAULT = 0.0f; 104 105 108 public static final int INT_DEFAULT_DEFAULT = 0; 109 110 113 public static final long LONG_DEFAULT_DEFAULT = 0L; 114 115 118 public static final String STRING_DEFAULT_DEFAULT = ""; 120 124 protected static final String TRUE = "true"; 126 130 protected static final String FALSE = "false"; 132 135 private static final String [] EMPTY_STRING_ARRAY = new String [0]; 136 137 145 public static final String PT_PREFERENCES = "preferences"; 147 163 public static class PropertyChangeEvent extends EventObject { 164 167 private static final long serialVersionUID = 1L; 168 169 172 private String propertyName; 173 174 178 private Object oldValue; 179 180 184 private Object newValue; 185 186 197 protected PropertyChangeEvent(Object source, String property, Object oldValue, Object newValue) { 198 199 super(source); 200 if (property == null) { 201 throw new IllegalArgumentException (); 202 } 203 this.propertyName = property; 204 this.oldValue = oldValue; 205 this.newValue = newValue; 206 } 207 208 218 public String getProperty() { 219 return propertyName; 220 } 221 222 228 public Object getNewValue() { 229 return newValue; 230 } 231 232 238 public Object getOldValue() { 239 return oldValue; 240 } 241 } 242 243 267 public interface IPropertyChangeListener extends EventListener { 268 269 279 public void propertyChange(Preferences.PropertyChangeEvent event); 280 } 281 282 288 protected ListenerList listeners = new ListenerList(); 289 290 294 private Properties properties; 295 296 301 private Properties defaultProperties; 302 303 307 protected boolean dirty = false; 308 309 324 public static void exportPreferences(IPath path) throws CoreException { 325 File file = path.toFile(); 326 if (file.exists()) 327 file.delete(); 328 file.getParentFile().mkdirs(); 329 IPreferencesService service = PreferencesService.getDefault(); 330 OutputStream output = null; 331 FileOutputStream fos = null; 332 try { 333 fos = new FileOutputStream(file); 334 output = new BufferedOutputStream(fos); 335 IEclipsePreferences node = (IEclipsePreferences) service.getRootNode().node(InstanceScope.SCOPE); 336 service.exportPreferences(node, output, (String []) null); 337 output.flush(); 338 fos.getFD().sync(); 339 } catch (IOException e) { 340 String message = NLS.bind(PrefsMessages.preferences_errorWriting, file, e.getMessage()); 341 IStatus status = new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e); 342 throw new CoreException(status); 343 } finally { 344 if (output != null) 345 try { 346 output.close(); 347 } catch (IOException e) { 348 } 350 } 351 } 352 353 375 public static void importPreferences(IPath path) throws CoreException { 376 if (!path.toFile().exists()) { 377 String msg = NLS.bind(PrefsMessages.preferences_fileNotFound, path.toOSString()); 378 throw new CoreException(new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, 1, msg, null)); 379 } 380 IPreferencesService service = PreferencesService.getDefault(); 381 InputStream input = null; 382 try { 383 input = new BufferedInputStream(new FileInputStream(path.toFile())); 384 service.importPreferences(input); 385 } catch (FileNotFoundException e) { 386 String msg = NLS.bind(PrefsMessages.preferences_fileNotFound, path.toOSString()); 387 throw new CoreException(new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, 1, msg, e)); 388 } finally { 389 if (input != null) 390 try { 391 input.close(); 392 } catch (IOException e) { 393 } 395 } 396 } 397 398 420 public static IStatus validatePreferenceVersions(IPath file) { 421 PreferencesService service = PreferencesService.getDefault(); 422 return service.validateVersions(file); 423 } 424 425 434 public Preferences() { 435 defaultProperties = new Properties(); 436 properties = new Properties(defaultProperties); 437 } 438 439 451 public void addPropertyChangeListener(IPropertyChangeListener listener) { 452 listeners.add(listener); 453 } 454 455 461 public void removePropertyChangeListener(IPropertyChangeListener listener) { 462 listeners.remove(listener); 463 } 464 465 474 public boolean contains(String name) { 475 return (properties.containsKey(name) || defaultProperties.containsKey(name)); 476 } 477 478 489 protected void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { 490 if (name == null) 491 throw new IllegalArgumentException (); 492 Object [] changeListeners = this.listeners.getListeners(); 493 if (changeListeners.length == 0) 495 return; 496 final PropertyChangeEvent pe = new PropertyChangeEvent(this, name, oldValue, newValue); 497 for (int i = 0; i < changeListeners.length; ++i) { 498 final IPropertyChangeListener l = (IPropertyChangeListener) changeListeners[i]; 499 ISafeRunnable job = new ISafeRunnable() { 500 public void handleException(Throwable exception) { 501 } 503 504 public void run() throws Exception { 505 l.propertyChange(pe); 506 } 507 }; 508 SafeRunner.run(job); 509 } 510 } 511 512 523 public boolean getBoolean(String name) { 524 String value = properties.getProperty(name); 525 if (value == null) { 526 return BOOLEAN_DEFAULT_DEFAULT; 527 } 528 return value.equals(Preferences.TRUE); 529 } 530 531 550 public void setValue(String name, boolean value) { 551 boolean defaultValue = getDefaultBoolean(name); 552 boolean oldValue = getBoolean(name); 553 if (value == defaultValue) { 554 Object removed = properties.remove(name); 555 if (removed != null) { 556 dirty = true; 558 } 559 } else { 560 properties.put(name, value ? Preferences.TRUE : Preferences.FALSE); 561 } 562 if (oldValue != value) { 563 dirty = true; 565 firePropertyChangeEvent(name, oldValue ? Boolean.TRUE : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE); 567 } 568 } 569 570 581 public boolean getDefaultBoolean(String name) { 582 String value = defaultProperties.getProperty(name); 583 if (value == null) { 584 return BOOLEAN_DEFAULT_DEFAULT; 585 } 586 return value.equals(Preferences.TRUE); 587 } 588 589 604 public void setDefault(String name, boolean value) { 605 defaultProperties.put(name, value ? Preferences.TRUE : Preferences.FALSE); 606 } 607 608 619 public double getDouble(String name) { 620 return convertToDouble(properties.getProperty(name), DOUBLE_DEFAULT_DEFAULT); 621 } 622 623 643 public void setValue(String name, double value) { 644 if (Double.isNaN(value)) { 645 throw new IllegalArgumentException (); 646 } 647 double defaultValue = getDefaultDouble(name); 648 double oldValue = getDouble(name); 649 if (value == defaultValue) { 650 Object removed = properties.remove(name); 651 if (removed != null) { 652 dirty = true; 654 } 655 } else { 656 properties.put(name, Double.toString(value)); 657 } 658 if (oldValue != value) { 659 dirty = true; 661 firePropertyChangeEvent(name, new Double (oldValue), new Double (value)); 663 } 664 } 665 666 677 public double getDefaultDouble(String name) { 678 return convertToDouble(defaultProperties.getProperty(name), DOUBLE_DEFAULT_DEFAULT); 679 } 680 681 697 public void setDefault(String name, double value) { 698 if (Double.isNaN(value)) { 699 throw new IllegalArgumentException (); 700 } 701 defaultProperties.put(name, Double.toString(value)); 702 } 703 704 714 private double convertToDouble(String rawPropertyValue, double defaultValue) { 715 double result = defaultValue; 716 if (rawPropertyValue != null) { 717 try { 718 result = Double.parseDouble(rawPropertyValue); 719 } catch (NumberFormatException e) { 720 } 722 } 723 return result; 724 } 725 726 737 public float getFloat(String name) { 738 return convertToFloat(properties.getProperty(name), FLOAT_DEFAULT_DEFAULT); 739 } 740 741 761 public void setValue(String name, float value) { 762 if (Float.isNaN(value)) { 763 throw new IllegalArgumentException (); 764 } 765 float defaultValue = getDefaultFloat(name); 766 float oldValue = getFloat(name); 767 if (value == defaultValue) { 768 Object removed = properties.remove(name); 769 if (removed != null) { 770 dirty = true; 772 } 773 } else { 774 properties.put(name, Float.toString(value)); 775 } 776 if (oldValue != value) { 777 dirty = true; 779 firePropertyChangeEvent(name, new Float (oldValue), new Float (value)); 781 } 782 } 783 784 795 public float getDefaultFloat(String name) { 796 return convertToFloat(defaultProperties.getProperty(name), FLOAT_DEFAULT_DEFAULT); 797 } 798 799 815 public void setDefault(String name, float value) { 816 if (Float.isNaN(value)) { 817 throw new IllegalArgumentException (); 818 } 819 defaultProperties.put(name, Float.toString(value)); 820 } 821 822 832 private float convertToFloat(String rawPropertyValue, float defaultValue) { 833 float result = defaultValue; 834 if (rawPropertyValue != null) { 835 try { 836 result = Float.parseFloat(rawPropertyValue); 837 } catch (NumberFormatException e) { 838 } 840 } 841 return result; 842 } 843 844 855 public int getInt(String name) { 856 return convertToInt(properties.getProperty(name), INT_DEFAULT_DEFAULT); 857 } 858 859 878 public void setValue(String name, int value) { 879 int defaultValue = getDefaultInt(name); 880 int oldValue = getInt(name); 881 if (value == defaultValue) { 882 Object removed = properties.remove(name); 883 if (removed != null) { 884 dirty = true; 886 } 887 } else { 888 properties.put(name, Integer.toString(value)); 889 } 890 if (oldValue != value) { 891 dirty = true; 893 firePropertyChangeEvent(name, new Integer (oldValue), new Integer (value)); 895 } 896 } 897 898 909 public int getDefaultInt(String name) { 910 return convertToInt(defaultProperties.getProperty(name), INT_DEFAULT_DEFAULT); 911 } 912 913 928 public void setDefault(String name, int value) { 929 defaultProperties.put(name, Integer.toString(value)); 930 } 931 932 942 private int convertToInt(String rawPropertyValue, int defaultValue) { 943 int result = defaultValue; 944 if (rawPropertyValue != null) { 945 try { 946 result = Integer.parseInt(rawPropertyValue); 947 } catch (NumberFormatException e) { 948 } 950 } 951 return result; 952 } 953 954 965 public long getLong(String name) { 966 return convertToLong(properties.getProperty(name), LONG_DEFAULT_DEFAULT); 967 } 968 969 988 public void setValue(String name, long value) { 989 long defaultValue = getDefaultLong(name); 990 long oldValue = getLong(name); 991 if (value == defaultValue) { 992 Object removed = properties.remove(name); 993 if (removed != null) { 994 dirty = true; 996 } 997 } else { 998 properties.put(name, Long.toString(value)); 999 } 1000 if (oldValue != value) { 1001 dirty = true; 1003 firePropertyChangeEvent(name, new Long (oldValue), new Long (value)); 1005 } 1006 } 1007 1008 1019 public long getDefaultLong(String name) { 1020 return convertToLong(defaultProperties.getProperty(name), LONG_DEFAULT_DEFAULT); 1021 } 1022 1023 1038 public void setDefault(String name, long value) { 1039 defaultProperties.put(name, Long.toString(value)); 1040 } 1041 1042 1052 private long convertToLong(String rawPropertyValue, long defaultValue) { 1053 long result = defaultValue; 1054 if (rawPropertyValue != null) { 1055 try { 1056 result = Long.parseLong(rawPropertyValue); 1057 } catch (NumberFormatException e) { 1058 } 1060 } 1061 return result; 1062 } 1063 1064 1074 public String getString(String name) { 1075 String value = properties.getProperty(name); 1076 return (value != null ? value : STRING_DEFAULT_DEFAULT); 1077 } 1078 1079 1098 public void setValue(String name, String value) { 1099 if (value == null) { 1100 throw new IllegalArgumentException (); 1101 } 1102 String defaultValue = getDefaultString(name); 1103 String oldValue = getString(name); 1104 if (value.equals(defaultValue)) { 1105 Object removed = properties.remove(name); 1106 if (removed != null) { 1107 dirty = true; 1109 } 1110 } else { 1111 properties.put(name, value); 1112 } 1113 if (!oldValue.equals(value)) { 1114 dirty = true; 1116 firePropertyChangeEvent(name, oldValue, value); 1118 } 1119 } 1120 1121 1132 public String getDefaultString(String name) { 1133 String value = defaultProperties.getProperty(name); 1134 return (value != null ? value : STRING_DEFAULT_DEFAULT); 1135 } 1136 1137 1152 public void setDefault(String name, String value) { 1153 if (value == null) { 1154 throw new IllegalArgumentException (); 1155 } 1156 defaultProperties.put(name, value); 1157 } 1158 1159 1169 public boolean isDefault(String name) { 1170 return !properties.containsKey(name); 1171 } 1172 1173 1192 public void setToDefault(String name) { 1193 Object oldPropertyValue = properties.remove(name); 1194 if (oldPropertyValue != null) { 1195 dirty = true; 1196 } 1197 String newValue = defaultProperties.getProperty(name, null); 1198 firePropertyChangeEvent(name, oldPropertyValue, newValue); 1201 } 1202 1203 1209 public String [] propertyNames() { 1210 return (String []) properties.keySet().toArray(EMPTY_STRING_ARRAY); 1211 } 1212 1213 1219 public String [] defaultPropertyNames() { 1220 return (String []) defaultProperties.keySet().toArray(EMPTY_STRING_ARRAY); 1221 } 1222 1223 1231 public boolean needsSaving() { 1232 return dirty; 1233 } 1234 1235 1250 public void store(OutputStream out, String header) throws IOException { 1251 properties.store(out, header); 1252 dirty = false; 1253 } 1254 1255 1266 public void load(InputStream in) throws IOException { 1267 properties.load(in); 1268 dirty = false; 1269 } 1270} 1271 | Popular Tags |