KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > preferences > legacy > PreferenceForwarder


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.core.internal.preferences.legacy;
12
13 import java.io.*;
14 import java.util.Iterator JavaDoc;
15 import java.util.Properties JavaDoc;
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 /**
25  * This class represents a convenience layer between the Eclipse 3.0
26  * preferences and pre-3.0 preferences. It acts as a bridge between the
27  * org.eclipse.core.runtime.Preferences object associated with a particular plug-in
28  * object, and its corresponding preference node in the 3.0 preference node
29  * hierarchy.
30  *
31  * @since 3.0
32  */

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 JavaDoc pluginID;
40     private Object JavaDoc plugin;
41     // boolean to check to see if we should re-wrap and forward change
42
// events coming from the new runtime APIs.
43
private boolean notify = true;
44
45     /*
46      * Used for test suites only.
47      */

48     public PreferenceForwarder(String JavaDoc pluginID) {
49         this(null, pluginID);
50     }
51
52     public PreferenceForwarder(Object JavaDoc plugin, String JavaDoc pluginID) {
53         super();
54         this.plugin = plugin;
55         this.pluginID = pluginID;
56     }
57
58     /*
59      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener#added(org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent)
60      */

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 JavaDoc e) {
67                 throw new RuntimeException JavaDoc("Plug-in preferences must be instances of EclipsePreferences: " + e.getMessage()); //$NON-NLS-1$
68
}
69         }
70     }
71
72     /*
73      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener#removed(org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent)
74      */

75     public synchronized void removed(IEclipsePreferences.NodeChangeEvent event) {
76         // Do nothing. We can't remove ourselves from the node's list of preference change
77
// listeners because the node has already been removed.
78
}
79
80     /**
81      * Adds a property change listener to this preference object.
82      * Has no affect if the identical listener is already registered.
83      *
84      * @param listener a property change listener
85      */

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     /*
99      * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent)
100      */

101     public void preferenceChange(IEclipsePreferences.PreferenceChangeEvent event) {
102         // if we are the ones making this change, then don't broadcast
103
if (!notify)
104             return;
105         Object JavaDoc oldValue = event.getOldValue();
106         Object JavaDoc newValue = event.getNewValue();
107         String JavaDoc 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 JavaDoc e) {
125             throw new RuntimeException JavaDoc("Plug-in preferences must be instances of EclipsePreferences: " + e.getMessage()); //$NON-NLS-1$
126
}
127     }
128
129     private IEclipsePreferences getDefaultPreferences() {
130         return defaultsRoot.node(pluginID, plugin);
131     }
132
133     /**
134      * Removes the given listener from this preference object.
135      * Has no affect if the listener is not registered.
136      *
137      * @param listener a property change listener
138      */

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     /**
152      * Does its best at determining the default value for the given key. Checks the
153      * given object's type and then looks in the list of defaults to see if a value
154      * exists. If not or if there is a problem converting the value, the default default
155      * value for that type is returned.
156      */

157     private Object JavaDoc getDefault(String JavaDoc key, Object JavaDoc obj) {
158         IEclipsePreferences defaults = getDefaultPreferences();
159         if (obj instanceof String JavaDoc)
160             return defaults.get(key, STRING_DEFAULT_DEFAULT);
161         else if (obj instanceof Integer JavaDoc)
162             return new Integer JavaDoc(defaults.getInt(key, INT_DEFAULT_DEFAULT));
163         else if (obj instanceof Double JavaDoc)
164             return new Double JavaDoc(defaults.getDouble(key, DOUBLE_DEFAULT_DEFAULT));
165         else if (obj instanceof Float JavaDoc)
166             return new Float JavaDoc(defaults.getFloat(key, FLOAT_DEFAULT_DEFAULT));
167         else if (obj instanceof Long JavaDoc)
168             return new Long JavaDoc(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 JavaDoc)
172             return defaults.getBoolean(key, BOOLEAN_DEFAULT_DEFAULT) ? Boolean.TRUE : Boolean.FALSE;
173         else
174             return null;
175     }
176
177     /**
178      * Returns whether the given property is known to this preference object,
179      * either by having an explicit setting or by having a default
180      * setting.
181      *
182      * @param name the name of the property
183      * @return <code>true</code> if either a current value or a default
184      * value is known for the named property, and <code>false</code>otherwise
185      */

186     public boolean contains(String JavaDoc name) {
187         if (name == null)
188             return false;
189         String JavaDoc value = getPluginPreferences(true).get(name, null);
190         if (value != null)
191             return true;
192         return getDefaultPreferences().get(name, null) != null;
193     }
194
195     /**
196      * Returns the current value of the boolean-valued property with the
197      * given name.
198      * Returns the default-default value (<code>false</code>) if there
199      * is no property with the given name, or if the current value
200      * cannot be treated as a boolean.
201      *
202      * @param name the name of the property
203      * @return the boolean-valued property
204      */

205     public boolean getBoolean(String JavaDoc name) {
206         return getPluginPreferences(true).getBoolean(name, getDefaultPreferences().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT));
207     }
208
209     /**
210      * Sets the current value of the boolean-valued property with the
211      * given name.
212      * <p>
213      * A property change event is reported if the current value of the
214      * property actually changes from its previous value. In the event
215      * object, the property name is the name of the property, and the
216      * old and new values are wrapped as objects.
217      * </p>
218      * <p>
219      * If the given value is the same as the corresponding default value
220      * for the given property, the explicit setting is deleted.
221      * Note that the recommended way of re-initializing a property to its
222      * default value is to call <code>setToDefault</code>.
223      * </p>
224      *
225      * @param name the name of the property
226      * @param value the new current value of the property
227      */

228     public void setValue(String JavaDoc name, boolean value) {
229         Boolean JavaDoc oldValue = getBoolean(name) ? Boolean.TRUE : Boolean.FALSE;
230         Boolean JavaDoc 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     /**
246      * Returns the default value for the boolean-valued property
247      * with the given name.
248      * Returns the default-default value (<code>false</code>) if there
249      * is no default property with the given name, or if the default
250      * value cannot be treated as a boolean.
251      *
252      * @param name the name of the property
253      * @return the default value of the named property
254      */

255     public boolean getDefaultBoolean(String JavaDoc name) {
256         return getDefaultPreferences().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT);
257     }
258
259     /**
260      * Sets the default value for the boolean-valued property with the
261      * given name.
262      * <p>
263      * Note that the current value of the property is affected if
264      * the property's current value was its old default value, in which
265      * case it changes to the new default value. If the property's current
266      * is different from its old default value, its current value is
267      * unaffected. No property change events are reported by changing default
268      * values.
269      * </p>
270      *
271      * @param name the name of the property
272      * @param value the new default value for the property
273      */

274     public void setDefault(String JavaDoc name, boolean value) {
275         getDefaultPreferences().putBoolean(name, value);
276     }
277
278     /**
279      * Returns the current value of the double-valued property with the
280      * given name.
281      * Returns the default-default value (<code>0.0</code>) if there
282      * is no property with the given name, or if the current value
283      * cannot be treated as a double.
284      *
285      * @param name the name of the property
286      * @return the double-valued property
287      */

288     public double getDouble(String JavaDoc name) {
289         return getPluginPreferences(true).getDouble(name, getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT));
290     }
291
292     /**
293      * Sets the current value of the double-valued property with the
294      * given name.
295      * <p>
296      * A property change event is reported if the current value of the
297      * property actually changes from its previous value. In the event
298      * object, the property name is the name of the property, and the
299      * old and new values are wrapped as objects.
300      * </p>
301      * <p>
302      * If the given value is the same as the corresponding default value
303      * for the given property, the explicit setting is deleted.
304      * Note that the recommended way of re-initializing a property to its
305      * default value is to call <code>setToDefault</code>.
306      * </p>
307      *
308      * @param name the name of the property
309      * @param value the new current value of the property; must be
310      * a number (not a NaN)
311      */

312     public void setValue(String JavaDoc name, double value) {
313         if (Double.isNaN(value))
314             throw new IllegalArgumentException JavaDoc();
315         final double doubleValue = getDouble(name);
316         if (value == doubleValue)
317             return;
318         Double JavaDoc oldValue = new Double JavaDoc(doubleValue);
319         Double JavaDoc newValue = new Double JavaDoc(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     /**
333      * Returns the default value for the double-valued property
334      * with the given name.
335      * Returns the default-default value (<code>0.0</code>) if there
336      * is no default property with the given name, or if the default
337      * value cannot be treated as a double.
338      *
339      * @param name the name of the property
340      * @return the default value of the named property
341      */

342     public double getDefaultDouble(String JavaDoc name) {
343         return getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT);
344     }
345
346     /**
347      * Sets the default value for the double-valued property with the
348      * given name.
349      * <p>
350      * Note that the current value of the property is affected if
351      * the property's current value was its old default value, in which
352      * case it changes to the new default value. If the property's current
353      * is different from its old default value, its current value is
354      * unaffected. No property change events are reported by changing default
355      * values.
356      * </p>
357      *
358      * @param name the name of the property
359      * @param value the new default value for the property; must be
360      * a number (not a NaN)
361      */

362     public void setDefault(String JavaDoc name, double value) {
363         if (Double.isNaN(value))
364             throw new IllegalArgumentException JavaDoc();
365         getDefaultPreferences().putDouble(name, value);
366     }
367
368     /**
369      * Returns the current value of the float-valued property with the
370      * given name.
371      * Returns the default-default value (<code>0.0f</code>) if there
372      * is no property with the given name, or if the current value
373      * cannot be treated as a float.
374      *
375      * @param name the name of the property
376      * @return the float-valued property
377      */

378     public float getFloat(String JavaDoc name) {
379         return getPluginPreferences(true).getFloat(name, getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT));
380     }
381
382     /**
383      * Sets the current value of the float-valued property with the
384      * given name.
385      * <p>
386      * A property change event is reported if the current value of the
387      * property actually changes from its previous value. In the event
388      * object, the property name is the name of the property, and the
389      * old and new values are wrapped as objects.
390      * </p>
391      * <p>
392      * If the given value is the same as the corresponding default value
393      * for the given property, the explicit setting is deleted.
394      * Note that the recommended way of re-initializing a property to its
395      * default value is to call <code>setToDefault</code>.
396      * </p>
397      *
398      * @param name the name of the property
399      * @param value the new current value of the property; must be
400      * a number (not a NaN)
401      */

402     public void setValue(String JavaDoc name, float value) {
403         if (Float.isNaN(value))
404             throw new IllegalArgumentException JavaDoc();
405         final float floatValue = getFloat(name);
406         if (value == floatValue)
407             return;
408         Float JavaDoc oldValue = new Float JavaDoc(floatValue);
409         Float JavaDoc newValue = new Float JavaDoc(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     /**
423      * Returns the default value for the float-valued property
424      * with the given name.
425      * Returns the default-default value (<code>0.0f</code>) if there
426      * is no default property with the given name, or if the default
427      * value cannot be treated as a float.
428      *
429      * @param name the name of the property
430      * @return the default value of the named property
431      */

432     public float getDefaultFloat(String JavaDoc name) {
433         return getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT);
434     }
435
436     /**
437      * Sets the default value for the float-valued property with the
438      * given name.
439      * <p>
440      * Note that the current value of the property is affected if
441      * the property's current value was its old default value, in which
442      * case it changes to the new default value. If the property's current
443      * is different from its old default value, its current value is
444      * unaffected. No property change events are reported by changing default
445      * values.
446      * </p>
447      *
448      * @param name the name of the property
449      * @param value the new default value for the property; must be
450      * a number (not a NaN)
451      */

452     public void setDefault(String JavaDoc name, float value) {
453         if (Float.isNaN(value))
454             throw new IllegalArgumentException JavaDoc();
455         getDefaultPreferences().putFloat(name, value);
456     }
457
458     /**
459      * Returns the current value of the integer-valued property with the
460      * given name.
461      * Returns the default-default value (<code>0</code>) if there
462      * is no property with the given name, or if the current value
463      * cannot be treated as an integter.
464      *
465      * @param name the name of the property
466      * @return the int-valued property
467      */

468     public int getInt(String JavaDoc name) {
469         return getPluginPreferences(true).getInt(name, getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT));
470     }
471
472     /**
473      * Sets the current value of the integer-valued property with the
474      * given name.
475      * <p>
476      * A property change event is reported if the current value of the
477      * property actually changes from its previous value. In the event
478      * object, the property name is the name of the property, and the
479      * old and new values are wrapped as objects.
480      * </p>
481      * <p>
482      * If the given value is the same as the corresponding default value
483      * for the given property, the explicit setting is deleted.
484      * Note that the recommended way of re-initializing a property to its
485      * default value is to call <code>setToDefault</code>.
486      * </p>
487      *
488      * @param name the name of the property
489      * @param value the new current value of the property
490      */

491     public void setValue(String JavaDoc name, int value) {
492         final int intValue = getInt(name);
493         if (value == intValue)
494             return;
495         Integer JavaDoc oldValue = new Integer JavaDoc(intValue);
496         Integer JavaDoc newValue = new Integer JavaDoc(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     /**
510      * Returns the default value for the integer-valued property
511      * with the given name.
512      * Returns the default-default value (<code>0</code>) if there
513      * is no default property with the given name, or if the default
514      * value cannot be treated as an integer.
515      *
516      * @param name the name of the property
517      * @return the default value of the named property
518      */

519     public int getDefaultInt(String JavaDoc name) {
520         return getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT);
521     }
522
523     /**
524      * Sets the default value for the integer-valued property with the
525      * given name.
526      * <p>
527      * Note that the current value of the property is affected if
528      * the property's current value was its old default value, in which
529      * case it changes to the new default value. If the property's current
530      * is different from its old default value, its current value is
531      * unaffected. No property change events are reported by changing default
532      * values.
533      * </p>
534      *
535      * @param name the name of the property
536      * @param value the new default value for the property
537      */

538     public void setDefault(String JavaDoc name, int value) {
539         getDefaultPreferences().putInt(name, value);
540     }
541
542     /**
543      * Returns the current value of the long-valued property with the
544      * given name.
545      * Returns the default-default value (<code>0L</code>) if there
546      * is no property with the given name, or if the current value
547      * cannot be treated as a long.
548      *
549      * @param name the name of the property
550      * @return the long-valued property
551      */

552     public long getLong(String JavaDoc name) {
553         return getPluginPreferences(true).getLong(name, getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT));
554     }
555
556     /**
557      * Sets the current value of the long-valued property with the
558      * given name.
559      * <p>
560      * A property change event is reported if the current value of the
561      * property actually changes from its previous value. In the event
562      * object, the property name is the name of the property, and the
563      * old and new values are wrapped as objects.
564      * </p>
565      * <p>
566      * If the given value is the same as the corresponding default value
567      * for the given property, the explicit setting is deleted.
568      * Note that the recommended way of re-initializing a property to its
569      * default value is to call <code>setToDefault</code>.
570      * </p>
571      *
572      * @param name the name of the property
573      * @param value the new current value of the property
574      */

575     public void setValue(String JavaDoc name, long value) {
576         final long longValue = getLong(name);
577         if (value == longValue)
578             return;
579         Long JavaDoc oldValue = new Long JavaDoc(longValue);
580         Long JavaDoc newValue = new Long JavaDoc(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     /**
594      * Returns the default value for the long-valued property
595      * with the given name.
596      * Returns the default-default value (<code>0L</code>) if there
597      * is no default property with the given name, or if the default
598      * value cannot be treated as a long.
599      *
600      * @param name the name of the property
601      * @return the default value of the named property
602      */

603     public long getDefaultLong(String JavaDoc name) {
604         return getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT);
605     }
606
607     /**
608      * Sets the default value for the long-valued property with the
609      * given name.
610      * <p>
611      * Note that the current value of the property is affected if
612      * the property's current value was its old default value, in which
613      * case it changes to the new default value. If the property's current
614      * is different from its old default value, its current value is
615      * unaffected. No property change events are reported by changing default
616      * values.
617      * </p>
618      *
619      * @param name the name of the property
620      * @param value the new default value for the property
621      */

622     public void setDefault(String JavaDoc name, long value) {
623         getDefaultPreferences().putLong(name, value);
624     }
625
626     /**
627      * Returns the current value of the string-valued property with the
628      * given name.
629      * Returns the default-default value (the empty string <code>""</code>)
630      * if there is no property with the given name.
631      *
632      * @param name the name of the property
633      * @return the string-valued property
634      */

635     public String JavaDoc getString(String JavaDoc name) {
636         return getPluginPreferences(true).get(name, getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT));
637     }
638
639     /**
640      * Sets the current value of the string-valued property with the
641      * given name.
642      * <p>
643      * A property change event is reported if the current value of the
644      * property actually changes from its previous value. In the event
645      * object, the property name is the name of the property, and the
646      * old and new values are wrapped as objects.
647      * </p>
648      * <p>
649      * If the given value is the same as the corresponding default value
650      * for the given property, the explicit setting is deleted.
651      * Note that the recommended way of re-initializing a property to its
652      * default value is to call <code>setToDefault</code>.
653      * </p>
654      *
655      * @param name the name of the property
656      * @param value the new current value of the property
657      */

658     public void setValue(String JavaDoc name, String JavaDoc value) {
659         if (value == null)
660             throw new IllegalArgumentException JavaDoc();
661         String JavaDoc 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     /**
677      * Returns the default value for the string-valued property
678      * with the given name.
679      * Returns the default-default value (the empty string <code>""</code>)
680      * is no default property with the given name, or if the default
681      * value cannot be treated as a string.
682      *
683      * @param name the name of the property
684      * @return the default value of the named property
685      */

686     public String JavaDoc getDefaultString(String JavaDoc name) {
687         return getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT);
688     }
689
690     /**
691      * Sets the default value for the string-valued property with the
692      * given name.
693      * <p>
694      * Note that the current value of the property is affected if
695      * the property's current value was its old default value, in which
696      * case it changes to the new default value. If the property's current
697      * is different from its old default value, its current value is
698      * unaffected. No property change events are reported by changing default
699      * values.
700      * </p>
701      *
702      * @param name the name of the property
703      * @param value the new default value for the property
704      */

705     public void setDefault(String JavaDoc name, String JavaDoc value) {
706         if (value == null)
707             throw new IllegalArgumentException JavaDoc();
708         getDefaultPreferences().put(name, value);
709     }
710
711     /**
712      * Returns whether the property with the given name has the default value in
713      * virtue of having no explicitly set value.
714      *
715      * @param name the name of the property
716      * @return <code>true</code> if the property has no explicitly set value,
717      * and <code>false</code> otherwise (including the case where the property
718      * is unknown to this object)
719      */

720     public boolean isDefault(String JavaDoc name) {
721         if (name == null)
722             return false;
723         return getPluginPreferences(true).get(name, null) == null;
724     }
725
726     /**
727      * Sets the current value of the property with the given name back
728      * to its default value. Has no effect if the property does not have
729      * its own current value.
730      * <p>
731      * Note that the recommended way of re-initializing a property to the
732      * appropriate default value is to call <code>setToDefault</code>.
733      * This is implemented by removing the named value from the object,
734      * thereby exposing the default value.
735      * </p>
736      * <p>
737      * A property change event is always reported. In the event
738      * object, the property name is the name of the property, and the
739      * old and new values are either strings, or <code>null</code>
740      * indicating the default-default value.
741      * </p>
742      *
743      * @param name the name of the property
744      */

745     public void setToDefault(String JavaDoc name) {
746         IEclipsePreferences preferences = getPluginPreferences(true);
747         Object JavaDoc oldValue = preferences.get(name, null);
748         if (oldValue != null)
749             preferences.remove(name);
750     }
751
752     /**
753      * Returns a list of all properties known to this preference object which
754      * have current values other than their default value.
755      *
756      * @return an array of property names
757      */

758     public String JavaDoc[] propertyNames() {
759         return getPluginPreferences(true).keys();
760     }
761
762     /**
763      * Returns a list of all properties known to this preference object which
764      * have default values other than their default-default value.
765      *
766      * @return an array of property names
767      */

768     public String JavaDoc[] defaultPropertyNames() {
769         try {
770             return getDefaultPreferences().keys();
771         } catch (BackingStoreException e) {
772             logError(e.getMessage(), e);
773             return new String JavaDoc[0];
774         }
775     }
776
777     /**
778      * Returns whether the current values in this preference object
779      * require saving.
780      *
781      * @return <code>true</code> if at least one of the properties
782      * known to this preference object has a current value different from its
783      * default value, and <code>false</code> otherwise
784      */

785     public boolean needsSaving() {
786         return getPluginPreferences(true).isDirty();
787     }
788
789     /**
790      * Flush the values of these plug-in preferences to disk.
791      *
792      * @throws BackingStoreException
793      */

794     public void flush() throws BackingStoreException {
795         IEclipsePreferences node = getPluginPreferences(false);
796         if (node != null)
797             node.flush();
798     }
799
800     /*
801      * Something bad happened so log it.
802      */

803     private void logError(String JavaDoc message, Exception JavaDoc e) {
804         IStatus status = new Status(IStatus.ERROR, PrefsMessages.OWNER_NAME, IStatus.ERROR, message, e);
805         RuntimeLog.log(status);
806     }
807
808     /*
809      * @see org.eclipse.core.runtime.Preferences#load(java.io.InputStream)
810      */

811     public void load(InputStream in) throws IOException {
812         Properties JavaDoc result = new Properties JavaDoc();
813         result.load(in);
814         convertFromProperties(result);
815         // We loaded the prefs from a non-default location so now
816
// store them to disk. This also clears the dirty flag
817
// and therefore sets the #needsSaving() state correctly.
818
try {
819             flush();
820         } catch (BackingStoreException e) {
821             throw new IOException(e.getMessage());
822         }
823     }
824
825     /*
826      * @see org.eclipse.core.runtime.Preferences#store(java.io.OutputStream, java.lang.String)
827      */

828     public void store(OutputStream out, String JavaDoc header) throws IOException {
829         Properties JavaDoc result = convertToProperties();
830         result.store(out, header);
831         // We stored the prefs to a non-default location but the spec
832
// says that the dirty state is cleared so we want to store
833
// them to disk at the default location as well.
834
try {
835             flush();
836         } catch (BackingStoreException e) {
837             throw new IOException(e.getMessage());
838         }
839     }
840
841     private void convertFromProperties(Properties JavaDoc props) {
842         IEclipsePreferences preferences = getPluginPreferences(true);
843         for (Iterator JavaDoc i = props.keySet().iterator(); i.hasNext();) {
844             String JavaDoc key = (String JavaDoc) i.next();
845             String JavaDoc value = props.getProperty(key);
846             if (value != null)
847                 preferences.put(key, value);
848         }
849     }
850
851     public String JavaDoc toString() {
852         return "PreferenceForwarder(" + pluginID + ")"; //$NON-NLS-1$ //$NON-NLS-2$
853
}
854
855     /*
856      * Convert the preferences in this node to a properties file
857      * suitable for persistence.
858      */

859     private Properties JavaDoc convertToProperties() {
860         Properties JavaDoc result = new Properties JavaDoc();
861         String JavaDoc[] keys = propertyNames();
862         for (int i = 0; i < keys.length; i++) {
863             String JavaDoc key = keys[i];
864             String JavaDoc 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