KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > preferences > ScopedPreferenceStore


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.ui.preferences;
12
13 import java.io.IOException JavaDoc;
14
15 import org.eclipse.core.commands.common.EventManager;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.core.runtime.Plugin;
19 import org.eclipse.core.runtime.SafeRunner;
20 import org.eclipse.core.runtime.preferences.DefaultScope;
21 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
22 import org.eclipse.core.runtime.preferences.IScopeContext;
23 import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener;
24 import org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent;
25 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
26 import org.eclipse.jface.preference.IPersistentPreferenceStore;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.resource.JFaceResources;
29 import org.eclipse.jface.util.IPropertyChangeListener;
30 import org.eclipse.jface.util.PropertyChangeEvent;
31 import org.eclipse.jface.util.SafeRunnable;
32 import org.eclipse.ui.internal.WorkbenchMessages;
33 import org.osgi.service.prefs.BackingStoreException;
34
35 /**
36  * The ScopedPreferenceStore is an IPreferenceStore that uses the scopes
37  * provided in org.eclipse.core.runtime.preferences.
38  * <p>
39  * A ScopedPreferenceStore does the lookup of a preference based on it's search
40  * scopes and sets the value of the preference based on its store scope.
41  * </p>
42  * <p>
43  * The default scope is always included in the search scopes when searching for
44  * preference values.
45  * </p>
46  *
47  * @see org.eclipse.core.runtime.preferences
48  * @since 3.1
49  */

50 public class ScopedPreferenceStore extends EventManager implements
51         IPreferenceStore, IPersistentPreferenceStore {
52
53     /**
54      * The storeContext is the context where values will stored with the
55      * setValue methods. If there are no searchContexts this will be the search
56      * context. (along with the "default" context)
57      */

58     private IScopeContext storeContext;
59
60     /**
61      * The searchContext is the array of contexts that will be used by the get
62      * methods for searching for values.
63      */

64     private IScopeContext[] searchContexts;
65
66     /**
67      * A boolean to indicate the property changes should not be propagated.
68      */

69     protected boolean silentRunning = false;
70
71     /**
72      * The listener on the IEclipsePreferences. This is used to forward updates
73      * to the property change listeners on the preference store.
74      */

75     IEclipsePreferences.IPreferenceChangeListener preferencesListener;
76
77     /**
78      * The default context is the context where getDefault and setDefault
79      * methods will search. This context is also used in the search.
80      */

81     private IScopeContext defaultContext = new DefaultScope();
82
83     /**
84      * The nodeQualifer is the string used to look up the node in the contexts.
85      */

86     String JavaDoc nodeQualifier;
87
88     /**
89      * The defaultQualifier is the string used to look up the default node.
90      */

91     String JavaDoc defaultQualifier;
92
93     /**
94      * Boolean value indicating whether or not this store has changes to be
95      * saved.
96      */

97     private boolean dirty;
98
99     /**
100      * Create a new instance of the receiver. Store the values in context in the
101      * node looked up by qualifier. <strong>NOTE:</strong> Any instance of
102      * ScopedPreferenceStore should call
103      *
104      * @param context
105      * the scope to store to
106      * @param qualifier
107      * the qualifier used to look up the preference node
108      * @param defaultQualifierPath
109      * the qualifier used when looking up the defaults
110      */

111     public ScopedPreferenceStore(IScopeContext context, String JavaDoc qualifier,
112             String JavaDoc defaultQualifierPath) {
113         this(context, qualifier);
114         this.defaultQualifier = defaultQualifierPath;
115     }
116
117     /**
118      * Create a new instance of the receiver. Store the values in context in the
119      * node looked up by qualifier.
120      *
121      * @param context
122      * the scope to store to
123      * @param qualifier
124      * the qualifer used to look up the preference node
125      */

126     public ScopedPreferenceStore(IScopeContext context, String JavaDoc qualifier) {
127         storeContext = context;
128         this.nodeQualifier = qualifier;
129         this.defaultQualifier = qualifier;
130
131         ((IEclipsePreferences) getStorePreferences().parent())
132                 .addNodeChangeListener(getNodeChangeListener());
133     }
134
135     /**
136      * Return a node change listener that adds a removes the receiver when nodes
137      * change.
138      *
139      * @return INodeChangeListener
140      */

141     private INodeChangeListener getNodeChangeListener() {
142         return new IEclipsePreferences.INodeChangeListener() {
143             /*
144              * (non-Javadoc)
145              *
146              * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener#added(org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent)
147              */

148             public void added(NodeChangeEvent event) {
149                 if (nodeQualifier.equals(event.getChild().name())
150                         && isListenerAttached()) {
151                     getStorePreferences().addPreferenceChangeListener(
152                             preferencesListener);
153                 }
154             }
155
156             /*
157              * (non-Javadoc)
158              *
159              * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener#removed(org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent)
160              */

161             public void removed(NodeChangeEvent event) {
162                 // Do nothing as there are no events from removed node
163
}
164         };
165     }
166
167     /**
168      * Initialize the preferences listener.
169      */

170     private void initializePreferencesListener() {
171         if (preferencesListener == null) {
172             preferencesListener = new IEclipsePreferences.IPreferenceChangeListener() {
173                 /*
174                  * (non-Javadoc)
175                  *
176                  * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent)
177                  */

178                 public void preferenceChange(PreferenceChangeEvent event) {
179
180                     if (silentRunning) {
181                         return;
182                     }
183
184                     Object JavaDoc oldValue = event.getOldValue();
185                     Object JavaDoc newValue = event.getNewValue();
186                     String JavaDoc key = event.getKey();
187                     if (newValue == null) {
188                         newValue = getDefault(key, oldValue);
189                     } else if (oldValue == null) {
190                         oldValue = getDefault(key, newValue);
191                     }
192                     firePropertyChangeEvent(event.getKey(), oldValue, newValue);
193                 }
194             };
195             getStorePreferences().addPreferenceChangeListener(
196                     preferencesListener);
197         }
198
199     }
200
201     /**
202      * Does its best at determining the default value for the given key. Checks
203      * the given object's type and then looks in the list of defaults to see if
204      * a value exists. If not or if there is a problem converting the value, the
205      * default default value for that type is returned.
206      *
207      * @param key
208      * the key to search
209      * @param obj
210      * the object who default we are looking for
211      * @return Object or <code>null</code>
212      */

213     Object JavaDoc getDefault(String JavaDoc key, Object JavaDoc obj) {
214         IEclipsePreferences defaults = getDefaultPreferences();
215         if (obj instanceof String JavaDoc) {
216             return defaults.get(key, STRING_DEFAULT_DEFAULT);
217         } else if (obj instanceof Integer JavaDoc) {
218             return new Integer JavaDoc(defaults.getInt(key, INT_DEFAULT_DEFAULT));
219         } else if (obj instanceof Double JavaDoc) {
220             return new Double JavaDoc(defaults.getDouble(key, DOUBLE_DEFAULT_DEFAULT));
221         } else if (obj instanceof Float JavaDoc) {
222             return new Float JavaDoc(defaults.getFloat(key, FLOAT_DEFAULT_DEFAULT));
223         } else if (obj instanceof Long JavaDoc) {
224             return new Long JavaDoc(defaults.getLong(key, LONG_DEFAULT_DEFAULT));
225         } else if (obj instanceof Boolean JavaDoc) {
226             return defaults.getBoolean(key, BOOLEAN_DEFAULT_DEFAULT) ? Boolean.TRUE : Boolean.FALSE;
227         } else {
228             return null;
229         }
230     }
231
232     /**
233      * Return the IEclipsePreferences node associated with this store.
234      *
235      * @return the preference node for this store
236      */

237     IEclipsePreferences getStorePreferences() {
238         return storeContext.getNode(nodeQualifier);
239     }
240
241     /**
242      * Return the default IEclipsePreferences for this store.
243      *
244      * @return this store's default preference node
245      */

246     private IEclipsePreferences getDefaultPreferences() {
247         return defaultContext.getNode(defaultQualifier);
248     }
249
250     /*
251      * (non-Javadoc)
252      *
253      * @see org.eclipse.jface.preference.IPreferenceStore#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
254      */

255     public void addPropertyChangeListener(IPropertyChangeListener listener) {
256         initializePreferencesListener();// Create the preferences listener if it
257
// does not exist
258
addListenerObject(listener);
259     }
260
261     /**
262      * Return the preference path to search preferences on. This is the list of
263      * preference nodes based on the scope contexts for this store. If there are
264      * no search contexts set, then return this store's context.
265      * <p>
266      * Whether or not the default context should be included in the resulting
267      * list is specified by the <code>includeDefault</code> parameter.
268      * </p>
269      *
270      * @param includeDefault
271      * <code>true</code> if the default context should be included
272      * and <code>false</code> otherwise
273      * @return IEclipsePreferences[]
274      */

275     private IEclipsePreferences[] getPreferenceNodes(boolean includeDefault) {
276         // if the user didn't specify a search order, then return the scope that
277
// this store was created on. (and optionally the default)
278
if (searchContexts == null) {
279             if (includeDefault) {
280                 return new IEclipsePreferences[] { getStorePreferences(),
281                         getDefaultPreferences() };
282             }
283             return new IEclipsePreferences[] { getStorePreferences() };
284         }
285         // otherwise the user specified a search order so return the appropriate
286
// nodes based on it
287
int length = searchContexts.length;
288         if (includeDefault) {
289             length++;
290         }
291         IEclipsePreferences[] preferences = new IEclipsePreferences[length];
292         for (int i = 0; i < searchContexts.length; i++) {
293             preferences[i] = searchContexts[i].getNode(nodeQualifier);
294         }
295         if (includeDefault) {
296             preferences[length - 1] = getDefaultPreferences();
297         }
298         return preferences;
299     }
300
301     /**
302      * Set the search contexts to scopes. When searching for a value the seach
303      * will be done in the order of scope contexts and will not search the
304      * storeContext unless it is in this list.
305      * <p>
306      * If the given list is <code>null</code>, then clear this store's search
307      * contexts. This means that only this store's scope context and default
308      * scope will be used during preference value searching.
309      * </p>
310      * <p>
311      * The defaultContext will be added to the end of this list automatically
312      * and <em>MUST NOT</em> be included by the user.
313      * </p>
314      *
315      * @param scopes
316      * a list of scope contexts to use when searching, or
317      * <code>null</code>
318      */

319     public void setSearchContexts(IScopeContext[] scopes) {
320         this.searchContexts = scopes;
321         if (scopes == null) {
322             return;
323         }
324
325         // Assert that the default was not included (we automatically add it to
326
// the end)
327
for (int i = 0; i < scopes.length; i++) {
328             if (scopes[i].equals(defaultContext)) {
329                 Assert
330                         .isTrue(
331                                 false,
332                                 WorkbenchMessages.ScopedPreferenceStore_DefaultAddedError);
333             }
334         }
335     }
336
337     /*
338      * (non-Javadoc)
339      *
340      * @see org.eclipse.jface.preference.IPreferenceStore#contains(java.lang.String)
341      */

342     public boolean contains(String JavaDoc name) {
343         if (name == null) {
344             return false;
345         }
346         return (Platform.getPreferencesService().get(name, null,
347                 getPreferenceNodes(true))) != null;
348     }
349
350     /*
351      * (non-Javadoc)
352      *
353      * @see org.eclipse.jface.preference.IPreferenceStore#firePropertyChangeEvent(java.lang.String,
354      * java.lang.Object, java.lang.Object)
355      */

356     public void firePropertyChangeEvent(String JavaDoc name, Object JavaDoc oldValue,
357             Object JavaDoc newValue) {
358         // important: create intermediate array to protect against listeners
359
// being added/removed during the notification
360
final Object JavaDoc[] list = getListeners();
361         if (list.length == 0) {
362             return;
363         }
364         final PropertyChangeEvent event = new PropertyChangeEvent(this, name,
365                 oldValue, newValue);
366         for (int i = 0; i < list.length; i++) {
367             final IPropertyChangeListener listener = (IPropertyChangeListener) list[i];
368             SafeRunner.run(new SafeRunnable(JFaceResources
369                     .getString("PreferenceStore.changeError")) { //$NON-NLS-1$
370
public void run() {
371                             listener.propertyChange(event);
372                         }
373                     });
374         }
375     }
376
377     /*
378      * (non-Javadoc)
379      *
380      * @see org.eclipse.jface.preference.IPreferenceStore#getBoolean(java.lang.String)
381      */

382     public boolean getBoolean(String JavaDoc name) {
383         String JavaDoc value = internalGet(name);
384         return value == null ? BOOLEAN_DEFAULT_DEFAULT : Boolean.valueOf(value)
385                 .booleanValue();
386     }
387
388     /*
389      * (non-Javadoc)
390      *
391      * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultBoolean(java.lang.String)
392      */

393     public boolean getDefaultBoolean(String JavaDoc name) {
394         return getDefaultPreferences()
395                 .getBoolean(name, BOOLEAN_DEFAULT_DEFAULT);
396     }
397
398     /*
399      * (non-Javadoc)
400      *
401      * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultDouble(java.lang.String)
402      */

403     public double getDefaultDouble(String JavaDoc name) {
404         return getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT);
405     }
406
407     /*
408      * (non-Javadoc)
409      *
410      * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultFloat(java.lang.String)
411      */

412     public float getDefaultFloat(String JavaDoc name) {
413         return getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT);
414     }
415
416     /*
417      * (non-Javadoc)
418      *
419      * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultInt(java.lang.String)
420      */

421     public int getDefaultInt(String JavaDoc name) {
422         return getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT);
423     }
424
425     /*
426      * (non-Javadoc)
427      *
428      * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultLong(java.lang.String)
429      */

430     public long getDefaultLong(String JavaDoc name) {
431         return getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT);
432     }
433
434     /*
435      * (non-Javadoc)
436      *
437      * @see org.eclipse.jface.preference.IPreferenceStore#getDefaultString(java.lang.String)
438      */

439     public String JavaDoc getDefaultString(String JavaDoc name) {
440         return getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT);
441     }
442
443     /*
444      * (non-Javadoc)
445      *
446      * @see org.eclipse.jface.preference.IPreferenceStore#getDouble(java.lang.String)
447      */

448     public double getDouble(String JavaDoc name) {
449         String JavaDoc value = internalGet(name);
450         if (value == null) {
451             return DOUBLE_DEFAULT_DEFAULT;
452         }
453         try {
454             return Double.parseDouble(value);
455         } catch (NumberFormatException JavaDoc e) {
456             return DOUBLE_DEFAULT_DEFAULT;
457         }
458     }
459
460     /**
461      * Return the string value for the specified key. Look in the nodes which
462      * are specified by this object's list of search scopes. If the value does
463      * not exist then return <code>null</code>.
464      *
465      * @param key
466      * the key to search with
467      * @return String or <code>null</code> if the value does not exist.
468      */

469     private String JavaDoc internalGet(String JavaDoc key) {
470         return Platform.getPreferencesService().get(key, null,
471                 getPreferenceNodes(true));
472     }
473
474     /*
475      * (non-Javadoc)
476      *
477      * @see org.eclipse.jface.preference.IPreferenceStore#getFloat(java.lang.String)
478      */

479     public float getFloat(String JavaDoc name) {
480         String JavaDoc value = internalGet(name);
481         if (value == null) {
482             return FLOAT_DEFAULT_DEFAULT;
483         }
484         try {
485             return Float.parseFloat(value);
486         } catch (NumberFormatException JavaDoc e) {
487             return FLOAT_DEFAULT_DEFAULT;
488         }
489     }
490
491     /*
492      * (non-Javadoc)
493      *
494      * @see org.eclipse.jface.preference.IPreferenceStore#getInt(java.lang.String)
495      */

496     public int getInt(String JavaDoc name) {
497         String JavaDoc value = internalGet(name);
498         if (value == null) {
499             return INT_DEFAULT_DEFAULT;
500         }
501         try {
502             return Integer.parseInt(value);
503         } catch (NumberFormatException JavaDoc e) {
504             return INT_DEFAULT_DEFAULT;
505         }
506     }
507
508     /*
509      * (non-Javadoc)
510      *
511      * @see org.eclipse.jface.preference.IPreferenceStore#getLong(java.lang.String)
512      */

513     public long getLong(String JavaDoc name) {
514         String JavaDoc value = internalGet(name);
515         if (value == null) {
516             return LONG_DEFAULT_DEFAULT;
517         }
518         try {
519             return Long.parseLong(value);
520         } catch (NumberFormatException JavaDoc e) {
521             return LONG_DEFAULT_DEFAULT;
522         }
523     }
524
525     /*
526      * (non-Javadoc)
527      *
528      * @see org.eclipse.jface.preference.IPreferenceStore#getString(java.lang.String)
529      */

530     public String JavaDoc getString(String JavaDoc name) {
531         String JavaDoc value = internalGet(name);
532         return value == null ? STRING_DEFAULT_DEFAULT : value;
533     }
534
535     /*
536      * (non-Javadoc)
537      *
538      * @see org.eclipse.jface.preference.IPreferenceStore#isDefault(java.lang.String)
539      */

540     public boolean isDefault(String JavaDoc name) {
541         if (name == null) {
542             return false;
543         }
544         return (Platform.getPreferencesService().get(name, null,
545                 getPreferenceNodes(false))) == null;
546     }
547
548     /*
549      * (non-Javadoc)
550      *
551      * @see org.eclipse.jface.preference.IPreferenceStore#needsSaving()
552      */

553     public boolean needsSaving() {
554         return dirty;
555     }
556
557     /*
558      * (non-Javadoc)
559      *
560      * @see org.eclipse.jface.preference.IPreferenceStore#putValue(java.lang.String,
561      * java.lang.String)
562      */

563     public void putValue(String JavaDoc name, String JavaDoc value) {
564         try {
565             // Do not notify listeners
566
silentRunning = true;
567             getStorePreferences().put(name, value);
568         } finally {
569             // Be sure that an exception does not stop property updates
570
silentRunning = false;
571             dirty = true;
572         }
573     }
574
575     /*
576      * (non-Javadoc)
577      *
578      * @see org.eclipse.jface.preference.IPreferenceStore#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
579      */

580     public void removePropertyChangeListener(IPropertyChangeListener listener) {
581         removeListenerObject(listener);
582         if (!isListenerAttached()) {
583             disposePreferenceStoreListener();
584         }
585     }
586
587     /*
588      * (non-Javadoc)
589      *
590      * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String,
591      * double)
592      */

593     public void setDefault(String JavaDoc name, double value) {
594         getDefaultPreferences().putDouble(name, value);
595     }
596
597     /*
598      * (non-Javadoc)
599      *
600      * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String,
601      * float)
602      */

603     public void setDefault(String JavaDoc name, float value) {
604         getDefaultPreferences().putFloat(name, value);
605     }
606
607     /*
608      * (non-Javadoc)
609      *
610      * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String,
611      * int)
612      */

613     public void setDefault(String JavaDoc name, int value) {
614         getDefaultPreferences().putInt(name, value);
615     }
616
617     /*
618      * (non-Javadoc)
619      *
620      * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String,
621      * long)
622      */

623     public void setDefault(String JavaDoc name, long value) {
624         getDefaultPreferences().putLong(name, value);
625     }
626
627     /*
628      * (non-Javadoc)
629      *
630      * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String,
631      * java.lang.String)
632      */

633     public void setDefault(String JavaDoc name, String JavaDoc defaultObject) {
634         getDefaultPreferences().put(name, defaultObject);
635     }
636
637     /*
638      * (non-Javadoc)
639      *
640      * @see org.eclipse.jface.preference.IPreferenceStore#setDefault(java.lang.String,
641      * boolean)
642      */

643     public void setDefault(String JavaDoc name, boolean value) {
644         getDefaultPreferences().putBoolean(name, value);
645     }
646
647     /*
648      * (non-Javadoc)
649      *
650      * @see org.eclipse.jface.preference.IPreferenceStore#setToDefault(java.lang.String)
651      */

652     public void setToDefault(String JavaDoc name) {
653
654         String JavaDoc oldValue = getString(name);
655         String JavaDoc defaultValue = getDefaultString(name);
656         try {
657             silentRunning = true;// Turn off updates from the store
658
// removing a non-existing preference is a no-op so call the Core
659
// API directly
660
getStorePreferences().remove(name);
661             dirty = true;
662             firePropertyChangeEvent(name, oldValue, defaultValue);
663         } finally {
664             silentRunning = false;// Restart listening to preferences
665
}
666
667     }
668
669     /*
670      * (non-Javadoc)
671      *
672      * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String,
673      * double)
674      */

675     public void setValue(String JavaDoc name, double value) {
676         double oldValue = getDouble(name);
677         if (oldValue == value) {
678             return;
679         }
680         try {
681             silentRunning = true;// Turn off updates from the store
682
if (getDefaultDouble(name) == value) {
683                 getStorePreferences().remove(name);
684             } else {
685                 getStorePreferences().putDouble(name, value);
686             }
687             dirty = true;
688             firePropertyChangeEvent(name, new Double JavaDoc(oldValue), new Double JavaDoc(
689                     value));
690         } finally {
691             silentRunning = false;// Restart listening to preferences
692
}
693     }
694
695     /*
696      * (non-Javadoc)
697      *
698      * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String,
699      * float)
700      */

701     public void setValue(String JavaDoc name, float value) {
702         float oldValue = getFloat(name);
703         if (oldValue == value) {
704             return;
705         }
706         try {
707             silentRunning = true;// Turn off updates from the store
708
if (getDefaultFloat(name) == value) {
709                 getStorePreferences().remove(name);
710             } else {
711                 getStorePreferences().putFloat(name, value);
712             }
713             dirty = true;
714             firePropertyChangeEvent(name, new Float JavaDoc(oldValue), new Float JavaDoc(value));
715         } finally {
716             silentRunning = false;// Restart listening to preferences
717
}
718     }
719
720     /*
721      * (non-Javadoc)
722      *
723      * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String,
724      * int)
725      */

726     public void setValue(String JavaDoc name, int value) {
727         int oldValue = getInt(name);
728         if (oldValue == value) {
729             return;
730         }
731         try {
732             silentRunning = true;// Turn off updates from the store
733
if (getDefaultInt(name) == value) {
734                 getStorePreferences().remove(name);
735             } else {
736                 getStorePreferences().putInt(name, value);
737             }
738             dirty = true;
739             firePropertyChangeEvent(name, new Integer JavaDoc(oldValue), new Integer JavaDoc(
740                     value));
741         } finally {
742             silentRunning = false;// Restart listening to preferences
743
}
744     }
745
746     /*
747      * (non-Javadoc)
748      *
749      * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String,
750      * long)
751      */

752     public void setValue(String JavaDoc name, long value) {
753         long oldValue = getLong(name);
754         if (oldValue == value) {
755             return;
756         }
757         try {
758             silentRunning = true;// Turn off updates from the store
759
if (getDefaultLong(name) == value) {
760                 getStorePreferences().remove(name);
761             } else {
762                 getStorePreferences().putLong(name, value);
763             }
764             dirty = true;
765             firePropertyChangeEvent(name, new Long JavaDoc(oldValue), new Long JavaDoc(value));
766         } finally {
767             silentRunning = false;// Restart listening to preferences
768
}
769     }
770
771     /*
772      * (non-Javadoc)
773      *
774      * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String,
775      * java.lang.String)
776      */

777     public void setValue(String JavaDoc name, String JavaDoc value) {
778         // Do not turn on silent running here as Strings are propagated
779
if (getDefaultString(name).equals(value)) {
780             getStorePreferences().remove(name);
781         } else {
782             getStorePreferences().put(name, value);
783         }
784         dirty = true;
785     }
786
787     /*
788      * (non-Javadoc)
789      *
790      * @see org.eclipse.jface.preference.IPreferenceStore#setValue(java.lang.String,
791      * boolean)
792      */

793     public void setValue(String JavaDoc name, boolean value) {
794         boolean oldValue = getBoolean(name);
795         if (oldValue == value) {
796             return;
797         }
798         try {
799             silentRunning = true;// Turn off updates from the store
800
if (getDefaultBoolean(name) == value) {
801                 getStorePreferences().remove(name);
802             } else {
803                 getStorePreferences().putBoolean(name, value);
804             }
805             dirty = true;
806             firePropertyChangeEvent(name, oldValue ? Boolean.TRUE : Boolean.FALSE,
807                 value ? Boolean.TRUE : Boolean.FALSE);
808         } finally {
809             silentRunning = false;// Restart listening to preferences
810
}
811     }
812
813     /*
814      * (non-Javadoc)
815      *
816      * @see org.eclipse.jface.preference.IPersistentPreferenceStore#save()
817      */

818     public void save() throws IOException JavaDoc {
819         try {
820             getStorePreferences().flush();
821             dirty = false;
822         } catch (BackingStoreException e) {
823             throw new IOException JavaDoc(e.getMessage());
824         }
825
826     }
827
828     /**
829      * Dispose the receiver.
830      */

831     private void disposePreferenceStoreListener() {
832
833         IEclipsePreferences root = (IEclipsePreferences) Platform
834                 .getPreferencesService().getRootNode().node(
835                         Plugin.PLUGIN_PREFERENCE_SCOPE);
836         try {
837             if (!(root.nodeExists(nodeQualifier))) {
838                 return;
839             }
840         } catch (BackingStoreException e) {
841             return;// No need to report here as the node won't have the
842
// listener
843
}
844
845         IEclipsePreferences preferences = getStorePreferences();
846         if (preferences == null) {
847             return;
848         }
849         if (preferencesListener != null) {
850             preferences.removePreferenceChangeListener(preferencesListener);
851             preferencesListener = null;
852         }
853     }
854
855 }
856
Popular Tags