KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > preference > PreferenceStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.preference;
12
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.PrintStream JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.Properties JavaDoc;
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 /**
32  * A concrete preference store implementation based on an internal
33  * <code>java.util.Properties</code> object, with support for persisting the
34  * non-default preference values to files or streams.
35  * <p>
36  * This class was not designed to be subclassed.
37  * </p>
38  *
39  * @see IPreferenceStore
40  */

41 public class PreferenceStore extends EventManager implements
42         IPersistentPreferenceStore {
43
44     /**
45      * The mapping from preference name to preference value (represented as
46      * strings).
47      */

48     private Properties JavaDoc properties;
49
50     /**
51      * The mapping from preference name to default preference value (represented
52      * as strings); <code>null</code> if none.
53      */

54     private Properties JavaDoc defaultProperties;
55
56     /**
57      * Indicates whether a value as been changed by <code>setToDefault</code>
58      * or <code>setValue</code>; initially <code>false</code>.
59      */

60     private boolean dirty = false;
61
62     /**
63      * The file name used by the <code>load</code> method to load a property
64      * file. This filename is used to save the properties file when
65      * <code>save</code> is called.
66      */

67     private String JavaDoc filename;
68
69     /**
70      * Creates an empty preference store.
71      * <p>
72      * Use the methods <code>load(InputStream)</code> and
73      * <code>save(InputStream)</code> to load and store this preference store.
74      * </p>
75      *
76      * @see #load(InputStream)
77      * @see #save(OutputStream, String)
78      */

79     public PreferenceStore() {
80         defaultProperties = new Properties JavaDoc();
81         properties = new Properties JavaDoc(defaultProperties);
82     }
83
84     /**
85      * Creates an empty preference store that loads from and saves to the a
86      * file.
87      * <p>
88      * Use the methods <code>load()</code> and <code>save()</code> to load
89      * and store this preference store.
90      * </p>
91      *
92      * @param filename
93      * the file name
94      * @see #load()
95      * @see #save()
96      */

97     public PreferenceStore(String JavaDoc filename) {
98         this();
99         Assert.isNotNull(filename);
100         this.filename = filename;
101     }
102
103     /*
104      * (non-Javadoc) Method declared on IPreferenceStore.
105      */

106     public void addPropertyChangeListener(IPropertyChangeListener listener) {
107         addListenerObject(listener);
108     }
109
110     /*
111      * (non-Javadoc) Method declared on IPreferenceStore.
112      */

113     public boolean contains(String JavaDoc name) {
114         return (properties.containsKey(name) || defaultProperties
115                 .containsKey(name));
116     }
117
118     /*
119      * (non-Javadoc) Method declared on IPreferenceStore.
120      */

121     public void firePropertyChangeEvent(String JavaDoc name, Object JavaDoc oldValue,
122             Object JavaDoc newValue) {
123         final Object JavaDoc[] finalListeners = getListeners();
124         // Do we need to fire an event.
125
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")) { //$NON-NLS-1$
133
public void run() {
134                                 l.propertyChange(pe);
135                             }
136                         });
137             }
138         }
139     }
140
141     /*
142      * (non-Javadoc) Method declared on IPreferenceStore.
143      */

144     public boolean getBoolean(String JavaDoc name) {
145         return getBoolean(properties, name);
146     }
147
148     /**
149      * Helper function: gets boolean for a given name.
150      *
151      * @param p
152      * @param name
153      * @return boolean
154      */

155     private boolean getBoolean(Properties JavaDoc p, String JavaDoc name) {
156         String JavaDoc 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     /*
167      * (non-Javadoc) Method declared on IPreferenceStore.
168      */

169     public boolean getDefaultBoolean(String JavaDoc name) {
170         return getBoolean(defaultProperties, name);
171     }
172
173     /*
174      * (non-Javadoc) Method declared on IPreferenceStore.
175      */

176     public double getDefaultDouble(String JavaDoc name) {
177         return getDouble(defaultProperties, name);
178     }
179
180     /*
181      * (non-Javadoc) Method declared on IPreferenceStore.
182      */

183     public float getDefaultFloat(String JavaDoc name) {
184         return getFloat(defaultProperties, name);
185     }
186
187     /*
188      * (non-Javadoc) Method declared on IPreferenceStore.
189      */

190     public int getDefaultInt(String JavaDoc name) {
191         return getInt(defaultProperties, name);
192     }
193
194     /*
195      * (non-Javadoc) Method declared on IPreferenceStore.
196      */

197     public long getDefaultLong(String JavaDoc name) {
198         return getLong(defaultProperties, name);
199     }
200
201     /*
202      * (non-Javadoc) Method declared on IPreferenceStore.
203      */

204     public String JavaDoc getDefaultString(String JavaDoc name) {
205         return getString(defaultProperties, name);
206     }
207
208     /*
209      * (non-Javadoc) Method declared on IPreferenceStore.
210      */

211     public double getDouble(String JavaDoc name) {
212         return getDouble(properties, name);
213     }
214
215     /**
216      * Helper function: gets double for a given name.
217      *
218      * @param p
219      * @param name
220      * @return double
221      */

222     private double getDouble(Properties JavaDoc p, String JavaDoc name) {
223         String JavaDoc 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 JavaDoc(value).doubleValue();
230         } catch (NumberFormatException JavaDoc e) {
231         }
232         return ival;
233     }
234
235     /*
236      * (non-Javadoc) Method declared on IPreferenceStore.
237      */

238     public float getFloat(String JavaDoc name) {
239         return getFloat(properties, name);
240     }
241
242     /**
243      * Helper function: gets float for a given name.
244      *
245      * @param p
246      * @param name
247      * @return float
248      */

249     private float getFloat(Properties JavaDoc p, String JavaDoc name) {
250         String JavaDoc 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 JavaDoc(value).floatValue();
257         } catch (NumberFormatException JavaDoc e) {
258         }
259         return ival;
260     }
261
262     /*
263      * (non-Javadoc) Method declared on IPreferenceStore.
264      */

265     public int getInt(String JavaDoc name) {
266         return getInt(properties, name);
267     }
268
269     /**
270      * Helper function: gets int for a given name.
271      *
272      * @param p
273      * @param name
274      * @return int
275      */

276     private int getInt(Properties JavaDoc p, String JavaDoc name) {
277         String JavaDoc 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 JavaDoc e) {
285         }
286         return ival;
287     }
288
289     /*
290      * (non-Javadoc) Method declared on IPreferenceStore.
291      */

292     public long getLong(String JavaDoc name) {
293         return getLong(properties, name);
294     }
295
296     /**
297      * Helper function: gets long for a given name.
298      *
299      * @param p
300      * the properties storage (may be <code>null</code>)
301      * @param name
302      * the name of the property
303      * @return the long or a default value of if:
304      * <ul>
305      * <li>properties storage is <code>null</code></li>
306      * <li>property is not found</li>
307      * <li>property value is not a number</li>
308      * </ul>
309      * @see IPreferenceStore#LONG_DEFAULT_DEFAULT
310      */

311     private long getLong(Properties JavaDoc p, String JavaDoc name) {
312         String JavaDoc 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 JavaDoc e) {
320         }
321         return ival;
322     }
323
324     /*
325      * (non-Javadoc) Method declared on IPreferenceStore.
326      */

327     public String JavaDoc getString(String JavaDoc name) {
328         return getString(properties, name);
329     }
330
331     /**
332      * Helper function: gets string for a given name.
333      *
334      * @param p
335      * the properties storage (may be <code>null</code>)
336      * @param name
337      * the name of the property
338      * @return the value or a default value of if:
339      * <ul>
340      * <li>properties storage is <code>null</code></li>
341      * <li>property is not found</li>
342      * <li>property value is not a number</li>
343      * </ul>
344      * @see IPreferenceStore#STRING_DEFAULT_DEFAULT
345      */

346     private String JavaDoc getString(Properties JavaDoc p, String JavaDoc name) {
347         String JavaDoc value = p != null ? p.getProperty(name) : null;
348         if (value == null) {
349             return STRING_DEFAULT_DEFAULT;
350         }
351         return value;
352     }
353
354     /*
355      * (non-Javadoc) Method declared on IPreferenceStore.
356      */

357     public boolean isDefault(String JavaDoc name) {
358         return (!properties.containsKey(name) && defaultProperties
359                 .containsKey(name));
360     }
361
362     /**
363      * Prints the contents of this preference store to the given print stream.
364      *
365      * @param out
366      * the print stream
367      */

368     public void list(PrintStream JavaDoc out) {
369         properties.list(out);
370     }
371
372     /**
373      * Prints the contents of this preference store to the given print writer.
374      *
375      * @param out
376      * the print writer
377      */

378     public void list(PrintWriter JavaDoc out) {
379         properties.list(out);
380     }
381
382     /**
383      * Loads this preference store from the file established in the constructor
384      * <code>PreferenceStore(java.lang.String)</code> (or by
385      * <code>setFileName</code>). Default preference values are not affected.
386      *
387      * @exception java.io.IOException
388      * if there is a problem loading this store
389      */

390     public void load() throws IOException JavaDoc {
391         if (filename == null) {
392             throw new IOException JavaDoc("File name not specified");//$NON-NLS-1$
393
}
394         FileInputStream JavaDoc in = new FileInputStream JavaDoc(filename);
395         load(in);
396         in.close();
397     }
398
399     /**
400      * Loads this preference store from the given input stream. Default
401      * preference values are not affected.
402      *
403      * @param in
404      * the input stream
405      * @exception java.io.IOException
406      * if there is a problem loading this store
407      */

408     public void load(InputStream JavaDoc in) throws IOException JavaDoc {
409         properties.load(in);
410         dirty = false;
411     }
412
413     /*
414      * (non-Javadoc) Method declared on IPreferenceStore.
415      */

416     public boolean needsSaving() {
417         return dirty;
418     }
419
420     /**
421      * Returns an enumeration of all preferences known to this store which have
422      * current values other than their default value.
423      *
424      * @return an array of preference names
425      */

426     public String JavaDoc[] preferenceNames() {
427         ArrayList JavaDoc list = new ArrayList JavaDoc();
428         Enumeration JavaDoc it = properties.propertyNames();
429         while (it.hasMoreElements()) {
430             list.add(it.nextElement());
431         }
432         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
433     }
434
435     /*
436      * (non-Javadoc) Method declared on IPreferenceStore.
437      */

438     public void putValue(String JavaDoc name, String JavaDoc value) {
439         String JavaDoc oldValue = getString(name);
440         if (oldValue == null || !oldValue.equals(value)) {
441             setValue(properties, name, value);
442             dirty = true;
443         }
444     }
445
446     /*
447      * (non-Javadoc) Method declared on IPreferenceStore.
448      */

449     public void removePropertyChangeListener(IPropertyChangeListener listener) {
450         removeListenerObject(listener);
451     }
452
453     /**
454      * Saves the non-default-valued preferences known to this preference store
455      * to the file from which they were originally loaded.
456      *
457      * @exception java.io.IOException
458      * if there is a problem saving this store
459      */

460     public void save() throws IOException JavaDoc {
461         if (filename == null) {
462             throw new IOException JavaDoc("File name not specified");//$NON-NLS-1$
463
}
464         FileOutputStream JavaDoc out = null;
465         try {
466             out = new FileOutputStream JavaDoc(filename);
467             save(out, null);
468         } finally {
469             if (out != null) {
470                 out.close();
471             }
472         }
473     }
474
475     /**
476      * Saves this preference store to the given output stream. The given string
477      * is inserted as header information.
478      *
479      * @param out
480      * the output stream
481      * @param header
482      * the header
483      * @exception java.io.IOException
484      * if there is a problem saving this store
485      */

486     public void save(OutputStream JavaDoc out, String JavaDoc header) throws IOException JavaDoc {
487         properties.store(out, header);
488         dirty = false;
489     }
490
491     /*
492      * (non-Javadoc) Method declared on IPreferenceStore.
493      */

494     public void setDefault(String JavaDoc name, double value) {
495         setValue(defaultProperties, name, value);
496     }
497
498     /*
499      * (non-Javadoc) Method declared on IPreferenceStore.
500      */

501     public void setDefault(String JavaDoc name, float value) {
502         setValue(defaultProperties, name, value);
503     }
504
505     /*
506      * (non-Javadoc) Method declared on IPreferenceStore.
507      */

508     public void setDefault(String JavaDoc name, int value) {
509         setValue(defaultProperties, name, value);
510     }
511
512     /*
513      * (non-Javadoc) Method declared on IPreferenceStore.
514      */

515     public void setDefault(String JavaDoc name, long value) {
516         setValue(defaultProperties, name, value);
517     }
518
519     /*
520      * (non-Javadoc) Method declared on IPreferenceStore.
521      */

522     public void setDefault(String JavaDoc name, String JavaDoc value) {
523         setValue(defaultProperties, name, value);
524     }
525
526     /*
527      * (non-Javadoc) Method declared on IPreferenceStore.
528      */

529     public void setDefault(String JavaDoc name, boolean value) {
530         setValue(defaultProperties, name, value);
531     }
532
533     /**
534      * Sets the name of the file used when loading and storing this preference
535      * store.
536      * <p>
537      * Afterward, the methods <code>load()</code> and <code>save()</code>
538      * can be used to load and store this preference store.
539      * </p>
540      *
541      * @param name
542      * the file name
543      * @see #load()
544      * @see #save()
545      */

546     public void setFilename(String JavaDoc name) {
547         filename = name;
548     }
549
550     /*
551      * (non-Javadoc) Method declared on IPreferenceStore.
552      */

553     public void setToDefault(String JavaDoc name) {
554         Object JavaDoc oldValue = properties.get(name);
555         properties.remove(name);
556         dirty = true;
557         Object JavaDoc newValue = null;
558         if (defaultProperties != null) {
559             newValue = defaultProperties.get(name);
560         }
561         firePropertyChangeEvent(name, oldValue, newValue);
562     }
563
564     /*
565      * (non-Javadoc) Method declared on IPreferenceStore.
566      */

567     public void setValue(String JavaDoc 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 JavaDoc(oldValue), new Double JavaDoc(
573                     value));
574         }
575     }
576
577     /*
578      * (non-Javadoc) Method declared on IPreferenceStore.
579      */

580     public void setValue(String JavaDoc 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 JavaDoc(oldValue), new Float JavaDoc(value));
586         }
587     }
588
589     /*
590      * (non-Javadoc) Method declared on IPreferenceStore.
591      */

592     public void setValue(String JavaDoc 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 JavaDoc(oldValue), new Integer JavaDoc(
598                     value));
599         }
600     }
601
602     /*
603      * (non-Javadoc) Method declared on IPreferenceStore.
604      */

605     public void setValue(String JavaDoc 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 JavaDoc(oldValue), new Long JavaDoc(value));
611         }
612     }
613
614     /*
615      * (non-Javadoc) Method declared on IPreferenceStore.
616      */

617     public void setValue(String JavaDoc name, String JavaDoc value) {
618         String JavaDoc 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     /*
627      * (non-Javadoc) Method declared on IPreferenceStore.
628      */

629     public void setValue(String JavaDoc 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     /**
640      * Helper method: sets value for a given name.
641      *
642      * @param p
643      * @param name
644      * @param value
645      */

646     private void setValue(Properties JavaDoc p, String JavaDoc name, double value) {
647         Assert.isTrue(p != null);
648         p.put(name, Double.toString(value));
649     }
650
651     /**
652      * Helper method: sets value for a given name.
653      *
654      * @param p
655      * @param name
656      * @param value
657      */

658     private void setValue(Properties JavaDoc p, String JavaDoc name, float value) {
659         Assert.isTrue(p != null);
660         p.put(name, Float.toString(value));
661     }
662
663     /**
664      * Helper method: sets value for a given name.
665      *
666      * @param p
667      * @param name
668      * @param value
669      */

670     private void setValue(Properties JavaDoc p, String JavaDoc name, int value) {
671         Assert.isTrue(p != null);
672         p.put(name, Integer.toString(value));
673     }
674
675     /**
676      * Helper method: sets the value for a given name.
677      *
678      * @param p
679      * @param name
680      * @param value
681      */

682     private void setValue(Properties JavaDoc p, String JavaDoc name, long value) {
683         Assert.isTrue(p != null);
684         p.put(name, Long.toString(value));
685     }
686
687     /**
688      * Helper method: sets the value for a given name.
689      *
690      * @param p
691      * @param name
692      * @param value
693      */

694     private void setValue(Properties JavaDoc p, String JavaDoc name, String JavaDoc value) {
695         Assert.isTrue(p != null && value != null);
696         p.put(name, value);
697     }
698
699     /**
700      * Helper method: sets the value for a given name.
701      *
702      * @param p
703      * @param name
704      * @param value
705      */

706     private void setValue(Properties JavaDoc p, String JavaDoc name, boolean value) {
707         Assert.isTrue(p != null);
708         p.put(name, value == true ? IPreferenceStore.TRUE
709                 : IPreferenceStore.FALSE);
710     }
711 }
712
Popular Tags