1 11 package org.eclipse.jface.preference; 12 13 import java.io.FileInputStream ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.io.OutputStream ; 18 import java.io.PrintStream ; 19 import java.io.PrintWriter ; 20 import java.util.ArrayList ; 21 import java.util.Enumeration ; 22 import java.util.Properties ; 23 24 import org.eclipse.core.commands.common.EventManager; 25 import org.eclipse.jface.resource.JFaceResources; 26 import org.eclipse.core.runtime.Assert; 27 import org.eclipse.jface.util.IPropertyChangeListener; 28 import org.eclipse.jface.util.PropertyChangeEvent; 29 import org.eclipse.jface.util.SafeRunnable; 30 31 41 public class PreferenceStore extends EventManager implements 42 IPersistentPreferenceStore { 43 44 48 private Properties properties; 49 50 54 private Properties defaultProperties; 55 56 60 private boolean dirty = false; 61 62 67 private String filename; 68 69 79 public PreferenceStore() { 80 defaultProperties = new Properties (); 81 properties = new Properties (defaultProperties); 82 } 83 84 97 public PreferenceStore(String filename) { 98 this(); 99 Assert.isNotNull(filename); 100 this.filename = filename; 101 } 102 103 106 public void addPropertyChangeListener(IPropertyChangeListener listener) { 107 addListenerObject(listener); 108 } 109 110 113 public boolean contains(String name) { 114 return (properties.containsKey(name) || defaultProperties 115 .containsKey(name)); 116 } 117 118 121 public void firePropertyChangeEvent(String name, Object oldValue, 122 Object newValue) { 123 final Object [] finalListeners = getListeners(); 124 if (finalListeners.length > 0 126 && (oldValue == null || !oldValue.equals(newValue))) { 127 final PropertyChangeEvent pe = new PropertyChangeEvent(this, name, 128 oldValue, newValue); 129 for (int i = 0; i < finalListeners.length; ++i) { 130 final IPropertyChangeListener l = (IPropertyChangeListener) finalListeners[i]; 131 SafeRunnable.run(new SafeRunnable(JFaceResources 132 .getString("PreferenceStore.changeError")) { public void run() { 134 l.propertyChange(pe); 135 } 136 }); 137 } 138 } 139 } 140 141 144 public boolean getBoolean(String name) { 145 return getBoolean(properties, name); 146 } 147 148 155 private boolean getBoolean(Properties p, String name) { 156 String value = p != null ? p.getProperty(name) : null; 157 if (value == null) { 158 return BOOLEAN_DEFAULT_DEFAULT; 159 } 160 if (value.equals(IPreferenceStore.TRUE)) { 161 return true; 162 } 163 return false; 164 } 165 166 169 public boolean getDefaultBoolean(String name) { 170 return getBoolean(defaultProperties, name); 171 } 172 173 176 public double getDefaultDouble(String name) { 177 return getDouble(defaultProperties, name); 178 } 179 180 183 public float getDefaultFloat(String name) { 184 return getFloat(defaultProperties, name); 185 } 186 187 190 public int getDefaultInt(String name) { 191 return getInt(defaultProperties, name); 192 } 193 194 197 public long getDefaultLong(String name) { 198 return getLong(defaultProperties, name); 199 } 200 201 204 public String getDefaultString(String name) { 205 return getString(defaultProperties, name); 206 } 207 208 211 public double getDouble(String name) { 212 return getDouble(properties, name); 213 } 214 215 222 private double getDouble(Properties p, String name) { 223 String value = p != null ? p.getProperty(name) : null; 224 if (value == null) { 225 return DOUBLE_DEFAULT_DEFAULT; 226 } 227 double ival = DOUBLE_DEFAULT_DEFAULT; 228 try { 229 ival = new Double (value).doubleValue(); 230 } catch (NumberFormatException e) { 231 } 232 return ival; 233 } 234 235 238 public float getFloat(String name) { 239 return getFloat(properties, name); 240 } 241 242 249 private float getFloat(Properties p, String name) { 250 String value = p != null ? p.getProperty(name) : null; 251 if (value == null) { 252 return FLOAT_DEFAULT_DEFAULT; 253 } 254 float ival = FLOAT_DEFAULT_DEFAULT; 255 try { 256 ival = new Float (value).floatValue(); 257 } catch (NumberFormatException e) { 258 } 259 return ival; 260 } 261 262 265 public int getInt(String name) { 266 return getInt(properties, name); 267 } 268 269 276 private int getInt(Properties p, String name) { 277 String value = p != null ? p.getProperty(name) : null; 278 if (value == null) { 279 return INT_DEFAULT_DEFAULT; 280 } 281 int ival = 0; 282 try { 283 ival = Integer.parseInt(value); 284 } catch (NumberFormatException e) { 285 } 286 return ival; 287 } 288 289 292 public long getLong(String name) { 293 return getLong(properties, name); 294 } 295 296 311 private long getLong(Properties p, String name) { 312 String value = p != null ? p.getProperty(name) : null; 313 if (value == null) { 314 return LONG_DEFAULT_DEFAULT; 315 } 316 long ival = LONG_DEFAULT_DEFAULT; 317 try { 318 ival = Long.parseLong(value); 319 } catch (NumberFormatException e) { 320 } 321 return ival; 322 } 323 324 327 public String getString(String name) { 328 return getString(properties, name); 329 } 330 331 346 private String getString(Properties p, String name) { 347 String value = p != null ? p.getProperty(name) : null; 348 if (value == null) { 349 return STRING_DEFAULT_DEFAULT; 350 } 351 return value; 352 } 353 354 357 public boolean isDefault(String name) { 358 return (!properties.containsKey(name) && defaultProperties 359 .containsKey(name)); 360 } 361 362 368 public void list(PrintStream out) { 369 properties.list(out); 370 } 371 372 378 public void list(PrintWriter out) { 379 properties.list(out); 380 } 381 382 390 public void load() throws IOException { 391 if (filename == null) { 392 throw new IOException ("File name not specified"); } 394 FileInputStream in = new FileInputStream (filename); 395 load(in); 396 in.close(); 397 } 398 399 408 public void load(InputStream in) throws IOException { 409 properties.load(in); 410 dirty = false; 411 } 412 413 416 public boolean needsSaving() { 417 return dirty; 418 } 419 420 426 public String [] preferenceNames() { 427 ArrayList list = new ArrayList (); 428 Enumeration it = properties.propertyNames(); 429 while (it.hasMoreElements()) { 430 list.add(it.nextElement()); 431 } 432 return (String []) list.toArray(new String [list.size()]); 433 } 434 435 438 public void putValue(String name, String value) { 439 String oldValue = getString(name); 440 if (oldValue == null || !oldValue.equals(value)) { 441 setValue(properties, name, value); 442 dirty = true; 443 } 444 } 445 446 449 public void removePropertyChangeListener(IPropertyChangeListener listener) { 450 removeListenerObject(listener); 451 } 452 453 460 public void save() throws IOException { 461 if (filename == null) { 462 throw new IOException ("File name not specified"); } 464 FileOutputStream out = null; 465 try { 466 out = new FileOutputStream (filename); 467 save(out, null); 468 } finally { 469 if (out != null) { 470 out.close(); 471 } 472 } 473 } 474 475 486 public void save(OutputStream out, String header) throws IOException { 487 properties.store(out, header); 488 dirty = false; 489 } 490 491 494 public void setDefault(String name, double value) { 495 setValue(defaultProperties, name, value); 496 } 497 498 501 public void setDefault(String name, float value) { 502 setValue(defaultProperties, name, value); 503 } 504 505 508 public void setDefault(String name, int value) { 509 setValue(defaultProperties, name, value); 510 } 511 512 515 public void setDefault(String name, long value) { 516 setValue(defaultProperties, name, value); 517 } 518 519 522 public void setDefault(String name, String value) { 523 setValue(defaultProperties, name, value); 524 } 525 526 529 public void setDefault(String name, boolean value) { 530 setValue(defaultProperties, name, value); 531 } 532 533 546 public void setFilename(String name) { 547 filename = name; 548 } 549 550 553 public void setToDefault(String name) { 554 Object oldValue = properties.get(name); 555 properties.remove(name); 556 dirty = true; 557 Object newValue = null; 558 if (defaultProperties != null) { 559 newValue = defaultProperties.get(name); 560 } 561 firePropertyChangeEvent(name, oldValue, newValue); 562 } 563 564 567 public void setValue(String name, double value) { 568 double oldValue = getDouble(name); 569 if (oldValue != value) { 570 setValue(properties, name, value); 571 dirty = true; 572 firePropertyChangeEvent(name, new Double (oldValue), new Double ( 573 value)); 574 } 575 } 576 577 580 public void setValue(String name, float value) { 581 float oldValue = getFloat(name); 582 if (oldValue != value) { 583 setValue(properties, name, value); 584 dirty = true; 585 firePropertyChangeEvent(name, new Float (oldValue), new Float (value)); 586 } 587 } 588 589 592 public void setValue(String name, int value) { 593 int oldValue = getInt(name); 594 if (oldValue != value) { 595 setValue(properties, name, value); 596 dirty = true; 597 firePropertyChangeEvent(name, new Integer (oldValue), new Integer ( 598 value)); 599 } 600 } 601 602 605 public void setValue(String name, long value) { 606 long oldValue = getLong(name); 607 if (oldValue != value) { 608 setValue(properties, name, value); 609 dirty = true; 610 firePropertyChangeEvent(name, new Long (oldValue), new Long (value)); 611 } 612 } 613 614 617 public void setValue(String name, String value) { 618 String oldValue = getString(name); 619 if (oldValue == null || !oldValue.equals(value)) { 620 setValue(properties, name, value); 621 dirty = true; 622 firePropertyChangeEvent(name, oldValue, value); 623 } 624 } 625 626 629 public void setValue(String name, boolean value) { 630 boolean oldValue = getBoolean(name); 631 if (oldValue != value) { 632 setValue(properties, name, value); 633 dirty = true; 634 firePropertyChangeEvent(name, oldValue ? Boolean.TRUE 635 : Boolean.FALSE, value ? Boolean.TRUE : Boolean.FALSE); 636 } 637 } 638 639 646 private void setValue(Properties p, String name, double value) { 647 Assert.isTrue(p != null); 648 p.put(name, Double.toString(value)); 649 } 650 651 658 private void setValue(Properties p, String name, float value) { 659 Assert.isTrue(p != null); 660 p.put(name, Float.toString(value)); 661 } 662 663 670 private void setValue(Properties p, String name, int value) { 671 Assert.isTrue(p != null); 672 p.put(name, Integer.toString(value)); 673 } 674 675 682 private void setValue(Properties p, String name, long value) { 683 Assert.isTrue(p != null); 684 p.put(name, Long.toString(value)); 685 } 686 687 694 private void setValue(Properties p, String name, String value) { 695 Assert.isTrue(p != null && value != null); 696 p.put(name, value); 697 } 698 699 706 private void setValue(Properties p, String name, boolean value) { 707 Assert.isTrue(p != null); 708 p.put(name, value == true ? IPreferenceStore.TRUE 709 : IPreferenceStore.FALSE); 710 } 711 } 712 | Popular Tags |