KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > OverlayPreferenceStore


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.ui.internal.editors.text;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.preference.PreferenceStore;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19
20
21 /**
22  * An overlaying preference store.
23  *
24  * @since 2.1
25  */

26 class OverlayPreferenceStore implements IPreferenceStore {
27
28
29     /**
30      * Descriptor used to denote data types.
31      */

32     public static final class TypeDescriptor {
33         private TypeDescriptor() {
34         }
35     }
36
37     public static final TypeDescriptor BOOLEAN= new TypeDescriptor();
38     public static final TypeDescriptor DOUBLE= new TypeDescriptor();
39     public static final TypeDescriptor FLOAT= new TypeDescriptor();
40     public static final TypeDescriptor INT= new TypeDescriptor();
41     public static final TypeDescriptor LONG= new TypeDescriptor();
42     public static final TypeDescriptor STRING= new TypeDescriptor();
43
44     /**
45      * Data structure for the overlay key.
46      */

47     public static class OverlayKey {
48
49         TypeDescriptor fDescriptor;
50         String JavaDoc fKey;
51
52         public OverlayKey(TypeDescriptor descriptor, String JavaDoc key) {
53             fDescriptor= descriptor;
54             fKey= key;
55         }
56     }
57
58     /*
59      * @see IPropertyChangeListener
60      */

61     private class PropertyListener implements IPropertyChangeListener {
62
63         /*
64          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
65          */

66         public void propertyChange(PropertyChangeEvent event) {
67             OverlayKey key= findOverlayKey(event.getProperty());
68             if (key != null)
69                 propagateProperty(fParent, key, fStore);
70         }
71     }
72
73
74     /** The parent preference store. */
75     private IPreferenceStore fParent;
76     /** This store. */
77     private IPreferenceStore fStore;
78     /** The keys of this store. */
79     private OverlayKey[] fOverlayKeys;
80     /** The property listener. */
81     private PropertyListener fPropertyListener;
82     private boolean fLoaded;
83
84
85     /**
86      * Creates and returns a new overlay preference store.
87      *
88      * @param parent the parent preference store
89      * @param overlayKeys the overlay keys
90      */

91     public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) {
92         fParent= parent;
93         fOverlayKeys= overlayKeys;
94         fStore= new PreferenceStore();
95     }
96
97     /**
98      * Tries to find and return the overlay key for the given preference key string.
99      *
100      * @param key the preference key string
101      * @return the overlay key or <code>null</code> if none can be found
102      */

103     private OverlayKey findOverlayKey(String JavaDoc key) {
104         for (int i= 0; i < fOverlayKeys.length; i++) {
105             if (fOverlayKeys[i].fKey.equals(key))
106                 return fOverlayKeys[i];
107         }
108         return null;
109     }
110
111     /**
112      * Tells whether the given preference key string is
113      * covered by this overlay store.
114      *
115      * @param key the preference key string
116      * @return <code>true</code> if this overlay store covers the given key
117      */

118     private boolean covers(String JavaDoc key) {
119         return (findOverlayKey(key) != null);
120     }
121
122     /**
123      * Propagates the given overlay key from the orgin to the target preference store.
124      *
125      * @param orgin the source preference store
126      * @param key the overlay key
127      * @param target the preference store to which the key is propagated
128      */

129     private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) {
130
131         if (orgin.isDefault(key.fKey)) {
132             if (!target.isDefault(key.fKey))
133                 target.setToDefault(key.fKey);
134             return;
135         }
136
137         TypeDescriptor d= key.fDescriptor;
138         if (BOOLEAN == d) {
139
140             boolean originValue= orgin.getBoolean(key.fKey);
141             boolean targetValue= target.getBoolean(key.fKey);
142             if (targetValue != originValue)
143                 target.setValue(key.fKey, originValue);
144
145         } else if (DOUBLE == d) {
146
147             double originValue= orgin.getDouble(key.fKey);
148             double targetValue= target.getDouble(key.fKey);
149             if (targetValue != originValue)
150                 target.setValue(key.fKey, originValue);
151
152         } else if (FLOAT == d) {
153
154             float originValue= orgin.getFloat(key.fKey);
155             float targetValue= target.getFloat(key.fKey);
156             if (targetValue != originValue)
157                 target.setValue(key.fKey, originValue);
158
159         } else if (INT == d) {
160
161             int originValue= orgin.getInt(key.fKey);
162             int targetValue= target.getInt(key.fKey);
163             if (targetValue != originValue)
164                 target.setValue(key.fKey, originValue);
165
166         } else if (LONG == d) {
167
168             long originValue= orgin.getLong(key.fKey);
169             long targetValue= target.getLong(key.fKey);
170             if (targetValue != originValue)
171                 target.setValue(key.fKey, originValue);
172
173         } else if (STRING == d) {
174
175             String JavaDoc originValue= orgin.getString(key.fKey);
176             String JavaDoc targetValue= target.getString(key.fKey);
177             if (targetValue != null && originValue != null && !targetValue.equals(originValue))
178                 target.setValue(key.fKey, originValue);
179
180         }
181     }
182
183     /**
184      * Propagates all overlay keys from this store to the parent store.
185      */

186     public void propagate() {
187         for (int i= 0; i < fOverlayKeys.length; i++)
188             propagateProperty(fStore, fOverlayKeys[i], fParent);
189     }
190
191     /**
192      * Loads the given key from the orgin into the target.
193      *
194      * @param orgin the source preference store
195      * @param key the overlay key
196      * @param target the preference store to which the key is propagated
197      * @param forceInitialization if <code>true</code> the value in the target gets initialized before loading
198      */

199     private void loadProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target, boolean forceInitialization) {
200         TypeDescriptor d= key.fDescriptor;
201         if (BOOLEAN == d) {
202
203             if (forceInitialization)
204                 target.setValue(key.fKey, true);
205             target.setValue(key.fKey, orgin.getBoolean(key.fKey));
206             target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
207
208         } else if (DOUBLE == d) {
209
210             if (forceInitialization)
211                 target.setValue(key.fKey, 1.0D);
212             target.setValue(key.fKey, orgin.getDouble(key.fKey));
213             target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
214
215         } else if (FLOAT == d) {
216
217             if (forceInitialization)
218                 target.setValue(key.fKey, 1.0F);
219             target.setValue(key.fKey, orgin.getFloat(key.fKey));
220             target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
221
222         } else if (INT == d) {
223
224             if (forceInitialization)
225                 target.setValue(key.fKey, 1);
226             target.setValue(key.fKey, orgin.getInt(key.fKey));
227             target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
228
229         } else if (LONG == d) {
230
231             if (forceInitialization)
232                 target.setValue(key.fKey, 1L);
233             target.setValue(key.fKey, orgin.getLong(key.fKey));
234             target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
235
236         } else if (STRING == d) {
237
238             if (forceInitialization)
239                 target.setValue(key.fKey, "1"); //$NON-NLS-1$
240
target.setValue(key.fKey, orgin.getString(key.fKey));
241             target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
242
243         }
244     }
245
246     /**
247      * Loads the values from the parent into this store.
248      */

249     public void load() {
250         for (int i= 0; i < fOverlayKeys.length; i++)
251             loadProperty(fParent, fOverlayKeys[i], fStore, true);
252
253         fLoaded= true;
254     }
255
256     /**
257      * Loads the default values.
258      */

259     public void loadDefaults() {
260         for (int i= 0; i < fOverlayKeys.length; i++)
261             setToDefault(fOverlayKeys[i].fKey);
262     }
263
264     /**
265      * Starts to listen for changes.
266      */

267     public void start() {
268         if (fPropertyListener == null) {
269             fPropertyListener= new PropertyListener();
270             fParent.addPropertyChangeListener(fPropertyListener);
271         }
272     }
273
274     /**
275      * Stops to listen for changes.
276      */

277     public void stop() {
278         if (fPropertyListener != null) {
279             fParent.removePropertyChangeListener(fPropertyListener);
280             fPropertyListener= null;
281         }
282     }
283
284     /*
285      * @see IPreferenceStore#addPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
286      */

287     public void addPropertyChangeListener(IPropertyChangeListener listener) {
288         fStore.addPropertyChangeListener(listener);
289     }
290
291     /*
292      * @see IPreferenceStore#removePropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener)
293      */

294     public void removePropertyChangeListener(IPropertyChangeListener listener) {
295         fStore.removePropertyChangeListener(listener);
296     }
297
298     /*
299      * @see IPreferenceStore#firePropertyChangeEvent(java.lang.String, java.lang.Object, java.lang.Object)
300      */

301     public void firePropertyChangeEvent(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
302         fStore.firePropertyChangeEvent(name, oldValue, newValue);
303     }
304
305     /*
306      * @see IPreferenceStore#contains(java.lang.String)
307      */

308     public boolean contains(String JavaDoc name) {
309         return fStore.contains(name);
310     }
311
312     /*
313      * @see IPreferenceStore#getBoolean(java.lang.String)
314      */

315     public boolean getBoolean(String JavaDoc name) {
316         return fStore.getBoolean(name);
317     }
318
319     /*
320      * @see IPreferenceStore#getDefaultBoolean(java.lang.String)
321      */

322     public boolean getDefaultBoolean(String JavaDoc name) {
323         return fStore.getDefaultBoolean(name);
324     }
325
326     /*
327      * @see IPreferenceStore#getDefaultDouble(java.lang.String)
328      */

329     public double getDefaultDouble(String JavaDoc name) {
330         return fStore.getDefaultDouble(name);
331     }
332
333     /*
334      * @see IPreferenceStore#getDefaultFloat(String)
335      */

336     public float getDefaultFloat(String JavaDoc name) {
337         return fStore.getDefaultFloat(name);
338     }
339
340     /*
341      * @see IPreferenceStore#getDefaultInt(String)
342      */

343     public int getDefaultInt(String JavaDoc name) {
344         return fStore.getDefaultInt(name);
345     }
346
347     /*
348      * @see IPreferenceStore#getDefaultLong(String)
349      */

350     public long getDefaultLong(String JavaDoc name) {
351         return fStore.getDefaultLong(name);
352     }
353
354     /*
355      * @see IPreferenceStore#getDefaultString(String)
356      */

357     public String JavaDoc getDefaultString(String JavaDoc name) {
358         return fStore.getDefaultString(name);
359     }
360
361     /*
362      * @see IPreferenceStore#getDouble(String)
363      */

364     public double getDouble(String JavaDoc name) {
365         return fStore.getDouble(name);
366     }
367
368     /*
369      * @see IPreferenceStore#getFloat(String)
370      */

371     public float getFloat(String JavaDoc name) {
372         return fStore.getFloat(name);
373     }
374
375     /*
376      * @see IPreferenceStore#getInt(String)
377      */

378     public int getInt(String JavaDoc name) {
379         return fStore.getInt(name);
380     }
381
382     /*
383      * @see IPreferenceStore#getLong(String)
384      */

385     public long getLong(String JavaDoc name) {
386         return fStore.getLong(name);
387     }
388
389     /*
390      * @see IPreferenceStore#getString(String)
391      */

392     public String JavaDoc getString(String JavaDoc name) {
393         return fStore.getString(name);
394     }
395
396     /*
397      * @see IPreferenceStore#isDefault(String)
398      */

399     public boolean isDefault(String JavaDoc name) {
400         return fStore.isDefault(name);
401     }
402
403     /*
404      * @see IPreferenceStore#needsSaving()
405      */

406     public boolean needsSaving() {
407         return fStore.needsSaving();
408     }
409
410     /*
411      * @see IPreferenceStore#putValue(String, String)
412      */

413     public void putValue(String JavaDoc name, String JavaDoc value) {
414         if (covers(name))
415             fStore.putValue(name, value);
416     }
417
418     /*
419      * @see IPreferenceStore#setDefault(String, double)
420      */

421     public void setDefault(String JavaDoc name, double value) {
422         if (covers(name))
423             fStore.setDefault(name, value);
424     }
425
426     /*
427      * @see IPreferenceStore#setDefault(String, float)
428      */

429     public void setDefault(String JavaDoc name, float value) {
430         if (covers(name))
431             fStore.setDefault(name, value);
432     }
433
434     /*
435      * @see IPreferenceStore#setDefault(String, int)
436      */

437     public void setDefault(String JavaDoc name, int value) {
438         if (covers(name))
439             fStore.setDefault(name, value);
440     }
441
442     /*
443      * @see IPreferenceStore#setDefault(String, long)
444      */

445     public void setDefault(String JavaDoc name, long value) {
446         if (covers(name))
447             fStore.setDefault(name, value);
448     }
449
450     /*
451      * @see IPreferenceStore#setDefault(String, String)
452      */

453     public void setDefault(String JavaDoc name, String JavaDoc value) {
454         if (covers(name))
455             fStore.setDefault(name, value);
456     }
457
458     /*
459      * @see IPreferenceStore#setDefault(String, boolean)
460      */

461     public void setDefault(String JavaDoc name, boolean value) {
462         if (covers(name))
463             fStore.setDefault(name, value);
464     }
465
466     /*
467      * @see IPreferenceStore#setToDefault(String)
468      */

469     public void setToDefault(String JavaDoc name) {
470         fStore.setToDefault(name);
471     }
472
473     /*
474      * @see IPreferenceStore#setValue(String, double)
475      */

476     public void setValue(String JavaDoc name, double value) {
477         if (covers(name))
478             fStore.setValue(name, value);
479     }
480
481     /*
482      * @see IPreferenceStore#setValue(String, float)
483      */

484     public void setValue(String JavaDoc name, float value) {
485         if (covers(name))
486             fStore.setValue(name, value);
487     }
488
489     /*
490      * @see IPreferenceStore#setValue(String, int)
491      */

492     public void setValue(String JavaDoc name, int value) {
493         if (covers(name))
494             fStore.setValue(name, value);
495     }
496
497     /*
498      * @see IPreferenceStore#setValue(String, long)
499      */

500     public void setValue(String JavaDoc name, long value) {
501         if (covers(name))
502             fStore.setValue(name, value);
503     }
504
505     /*
506      * @see IPreferenceStore#setValue(String, String)
507      */

508     public void setValue(String JavaDoc name, String JavaDoc value) {
509         if (covers(name))
510             fStore.setValue(name, value);
511     }
512
513     /*
514      * @see IPreferenceStore#setValue(String, boolean)
515      */

516     public void setValue(String JavaDoc name, boolean value) {
517         if (covers(name))
518             fStore.setValue(name, value);
519     }
520
521     /**
522      * The keys to add to the list of overlay keys.
523      * <p>
524      * Note: This method must be called before {@link #load()} is called.
525      * </p>
526      *
527      * @param keys
528      * @since 3.0
529      */

530     public void addKeys(OverlayKey[] keys) {
531         Assert.isTrue(!fLoaded);
532         Assert.isNotNull(keys);
533
534         int overlayKeysLength= fOverlayKeys.length;
535         OverlayKey[] result= new OverlayKey[keys.length + overlayKeysLength];
536
537         for (int i= 0, length= overlayKeysLength; i < length; i++)
538             result[i]= fOverlayKeys[i];
539
540         for (int i= 0, length= keys.length; i < length; i++)
541             result[overlayKeysLength + i]= keys[i];
542
543         fOverlayKeys= result;
544
545         if (fLoaded)
546             load();
547     }
548 }
549
Popular Tags