KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > 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  
12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.preference.PreferenceStore;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20
21
22 /**
23  * An overlaying preference store.
24  */

25 public class OverlayPreferenceStore implements IPreferenceStore {
26     
27     
28     public static final class TypeDescriptor {
29         private TypeDescriptor() {
30         }
31     }
32     
33     public static final TypeDescriptor BOOLEAN= new TypeDescriptor();
34     public static final TypeDescriptor DOUBLE= new TypeDescriptor();
35     public static final TypeDescriptor FLOAT= new TypeDescriptor();
36     public static final TypeDescriptor INT= new TypeDescriptor();
37     public static final TypeDescriptor LONG= new TypeDescriptor();
38     public static final TypeDescriptor STRING= new TypeDescriptor();
39     
40     public static class OverlayKey {
41         
42         TypeDescriptor fDescriptor;
43         String JavaDoc fKey;
44         
45         public OverlayKey(TypeDescriptor descriptor, String JavaDoc key) {
46             fDescriptor= descriptor;
47             fKey= key;
48         }
49     }
50     
51     private class PropertyListener implements IPropertyChangeListener {
52                 
53         /*
54          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
55          */

56         public void propertyChange(PropertyChangeEvent event) {
57             OverlayKey key= findOverlayKey(event.getProperty());
58             if (key != null)
59                 propagateProperty(fParent, key, fStore);
60         }
61     }
62     
63     
64     private IPreferenceStore fParent;
65     private IPreferenceStore fStore;
66     private OverlayKey[] fOverlayKeys;
67     
68     private PropertyListener fPropertyListener;
69     private boolean fLoaded;
70     
71     
72     public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) {
73         fParent= parent;
74         fOverlayKeys= overlayKeys;
75         fStore= new PreferenceStore();
76     }
77     
78     private OverlayKey findOverlayKey(String JavaDoc key) {
79         for (int i= 0; i < fOverlayKeys.length; i++) {
80             if (fOverlayKeys[i].fKey.equals(key))
81                 return fOverlayKeys[i];
82         }
83         return null;
84     }
85     
86     private boolean covers(String JavaDoc key) {
87         return (findOverlayKey(key) != null);
88     }
89     
90     private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) {
91         
92         if (orgin.isDefault(key.fKey)) {
93             if (!target.isDefault(key.fKey))
94                 target.setToDefault(key.fKey);
95             return;
96         }
97         
98         TypeDescriptor d= key.fDescriptor;
99         if (BOOLEAN == d) {
100             
101             boolean originValue= orgin.getBoolean(key.fKey);
102             boolean targetValue= target.getBoolean(key.fKey);
103             if (targetValue != originValue)
104                 target.setValue(key.fKey, originValue);
105                 
106         } else if (DOUBLE == d) {
107             
108             double originValue= orgin.getDouble(key.fKey);
109             double targetValue= target.getDouble(key.fKey);
110             if (targetValue != originValue)
111                 target.setValue(key.fKey, originValue);
112         
113         } else if (FLOAT == d) {
114             
115             float originValue= orgin.getFloat(key.fKey);
116             float targetValue= target.getFloat(key.fKey);
117             if (targetValue != originValue)
118                 target.setValue(key.fKey, originValue);
119                 
120         } else if (INT == d) {
121
122             int originValue= orgin.getInt(key.fKey);
123             int targetValue= target.getInt(key.fKey);
124             if (targetValue != originValue)
125                 target.setValue(key.fKey, originValue);
126
127         } else if (LONG == d) {
128
129             long originValue= orgin.getLong(key.fKey);
130             long targetValue= target.getLong(key.fKey);
131             if (targetValue != originValue)
132                 target.setValue(key.fKey, originValue);
133
134         } else if (STRING == d) {
135
136             String JavaDoc originValue= orgin.getString(key.fKey);
137             String JavaDoc targetValue= target.getString(key.fKey);
138             if (targetValue != null && originValue != null && !targetValue.equals(originValue))
139                 target.setValue(key.fKey, originValue);
140
141         }
142     }
143     
144     public void propagate() {
145         for (int i= 0; i < fOverlayKeys.length; i++)
146             propagateProperty(fStore, fOverlayKeys[i], fParent);
147     }
148     
149     private void loadProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target, boolean forceInitialization) {
150         TypeDescriptor d= key.fDescriptor;
151         if (BOOLEAN == d) {
152             
153             if (forceInitialization)
154                 target.setValue(key.fKey, true);
155             target.setValue(key.fKey, orgin.getBoolean(key.fKey));
156             target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
157             
158         } else if (DOUBLE == d) {
159             
160             if (forceInitialization)
161                 target.setValue(key.fKey, 1.0D);
162             target.setValue(key.fKey, orgin.getDouble(key.fKey));
163             target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
164             
165         } else if (FLOAT == d) {
166             
167             if (forceInitialization)
168                 target.setValue(key.fKey, 1.0F);
169             target.setValue(key.fKey, orgin.getFloat(key.fKey));
170             target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
171             
172         } else if (INT == d) {
173             
174             if (forceInitialization)
175                 target.setValue(key.fKey, 1);
176             target.setValue(key.fKey, orgin.getInt(key.fKey));
177             target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
178             
179         } else if (LONG == d) {
180             
181             if (forceInitialization)
182                 target.setValue(key.fKey, 1L);
183             target.setValue(key.fKey, orgin.getLong(key.fKey));
184             target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
185             
186         } else if (STRING == d) {
187             
188             if (forceInitialization)
189                 target.setValue(key.fKey, "1"); //$NON-NLS-1$
190
target.setValue(key.fKey, orgin.getString(key.fKey));
191             target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
192             
193         }
194     }
195     
196     public void load() {
197         for (int i= 0; i < fOverlayKeys.length; i++)
198             loadProperty(fParent, fOverlayKeys[i], fStore, true);
199         
200         fLoaded= true;
201         
202     }
203     
204     public void loadDefaults() {
205         for (int i= 0; i < fOverlayKeys.length; i++)
206             setToDefault(fOverlayKeys[i].fKey);
207     }
208     
209     public void start() {
210         if (fPropertyListener == null) {
211             fPropertyListener= new PropertyListener();
212             fParent.addPropertyChangeListener(fPropertyListener);
213         }
214     }
215     
216     public void stop() {
217         if (fPropertyListener != null) {
218             fParent.removePropertyChangeListener(fPropertyListener);
219             fPropertyListener= null;
220         }
221     }
222     
223     /*
224      * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
225      */

226     public void addPropertyChangeListener(IPropertyChangeListener listener) {
227         fStore.addPropertyChangeListener(listener);
228     }
229     
230     /*
231      * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
232      */

233     public void removePropertyChangeListener(IPropertyChangeListener listener) {
234         fStore.removePropertyChangeListener(listener);
235     }
236     
237     /*
238      * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
239      */

240     public void firePropertyChangeEvent(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
241         fStore.firePropertyChangeEvent(name, oldValue, newValue);
242     }
243
244     /*
245      * @see IPreferenceStore#contains(String)
246      */

247     public boolean contains(String JavaDoc name) {
248         return fStore.contains(name);
249     }
250     
251     /*
252      * @see IPreferenceStore#getBoolean(String)
253      */

254     public boolean getBoolean(String JavaDoc name) {
255         return fStore.getBoolean(name);
256     }
257
258     /*
259      * @see IPreferenceStore#getDefaultBoolean(String)
260      */

261     public boolean getDefaultBoolean(String JavaDoc name) {
262         return fStore.getDefaultBoolean(name);
263     }
264
265     /*
266      * @see IPreferenceStore#getDefaultDouble(String)
267      */

268     public double getDefaultDouble(String JavaDoc name) {
269         return fStore.getDefaultDouble(name);
270     }
271
272     /*
273      * @see IPreferenceStore#getDefaultFloat(String)
274      */

275     public float getDefaultFloat(String JavaDoc name) {
276         return fStore.getDefaultFloat(name);
277     }
278
279     /*
280      * @see IPreferenceStore#getDefaultInt(String)
281      */

282     public int getDefaultInt(String JavaDoc name) {
283         return fStore.getDefaultInt(name);
284     }
285
286     /*
287      * @see IPreferenceStore#getDefaultLong(String)
288      */

289     public long getDefaultLong(String JavaDoc name) {
290         return fStore.getDefaultLong(name);
291     }
292
293     /*
294      * @see IPreferenceStore#getDefaultString(String)
295      */

296     public String JavaDoc getDefaultString(String JavaDoc name) {
297         return fStore.getDefaultString(name);
298     }
299
300     /*
301      * @see IPreferenceStore#getDouble(String)
302      */

303     public double getDouble(String JavaDoc name) {
304         return fStore.getDouble(name);
305     }
306
307     /*
308      * @see IPreferenceStore#getFloat(String)
309      */

310     public float getFloat(String JavaDoc name) {
311         return fStore.getFloat(name);
312     }
313
314     /*
315      * @see IPreferenceStore#getInt(String)
316      */

317     public int getInt(String JavaDoc name) {
318         return fStore.getInt(name);
319     }
320
321     /*
322      * @see IPreferenceStore#getLong(String)
323      */

324     public long getLong(String JavaDoc name) {
325         return fStore.getLong(name);
326     }
327
328     /*
329      * @see IPreferenceStore#getString(String)
330      */

331     public String JavaDoc getString(String JavaDoc name) {
332         return fStore.getString(name);
333     }
334
335     /*
336      * @see IPreferenceStore#isDefault(String)
337      */

338     public boolean isDefault(String JavaDoc name) {
339         return fStore.isDefault(name);
340     }
341
342     /*
343      * @see IPreferenceStore#needsSaving()
344      */

345     public boolean needsSaving() {
346         return fStore.needsSaving();
347     }
348
349     /*
350      * @see IPreferenceStore#putValue(String, String)
351      */

352     public void putValue(String JavaDoc name, String JavaDoc value) {
353         if (covers(name))
354             fStore.putValue(name, value);
355     }
356
357     /*
358      * @see IPreferenceStore#setDefault(String, double)
359      */

360     public void setDefault(String JavaDoc name, double value) {
361         if (covers(name))
362             fStore.setDefault(name, value);
363     }
364
365     /*
366      * @see IPreferenceStore#setDefault(String, float)
367      */

368     public void setDefault(String JavaDoc name, float value) {
369         if (covers(name))
370             fStore.setDefault(name, value);
371     }
372
373     /*
374      * @see IPreferenceStore#setDefault(String, int)
375      */

376     public void setDefault(String JavaDoc name, int value) {
377         if (covers(name))
378             fStore.setDefault(name, value);
379     }
380
381     /*
382      * @see IPreferenceStore#setDefault(String, long)
383      */

384     public void setDefault(String JavaDoc name, long value) {
385         if (covers(name))
386             fStore.setDefault(name, value);
387     }
388
389     /*
390      * @see IPreferenceStore#setDefault(String, String)
391      */

392     public void setDefault(String JavaDoc name, String JavaDoc value) {
393         if (covers(name))
394             fStore.setDefault(name, value);
395     }
396
397     /*
398      * @see IPreferenceStore#setDefault(String, boolean)
399      */

400     public void setDefault(String JavaDoc name, boolean value) {
401         if (covers(name))
402             fStore.setDefault(name, value);
403     }
404
405     /*
406      * @see IPreferenceStore#setToDefault(String)
407      */

408     public void setToDefault(String JavaDoc name) {
409         fStore.setToDefault(name);
410     }
411
412     /*
413      * @see IPreferenceStore#setValue(String, double)
414      */

415     public void setValue(String JavaDoc name, double value) {
416         if (covers(name))
417             fStore.setValue(name, value);
418     }
419
420     /*
421      * @see IPreferenceStore#setValue(String, float)
422      */

423     public void setValue(String JavaDoc name, float value) {
424         if (covers(name))
425             fStore.setValue(name, value);
426     }
427
428     /*
429      * @see IPreferenceStore#setValue(String, int)
430      */

431     public void setValue(String JavaDoc name, int value) {
432         if (covers(name))
433             fStore.setValue(name, value);
434     }
435
436     /*
437      * @see IPreferenceStore#setValue(String, long)
438      */

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

447     public void setValue(String JavaDoc name, String JavaDoc value) {
448         if (covers(name))
449             fStore.setValue(name, value);
450     }
451
452     /*
453      * @see IPreferenceStore#setValue(String, boolean)
454      */

455     public void setValue(String JavaDoc name, boolean value) {
456         if (covers(name))
457             fStore.setValue(name, value);
458     }
459
460     /**
461      * The keys to add to the list of overlay keys.
462      * <p>
463      * Note: This method must be called before {@link #load()} is called.
464      * </p>
465      *
466      * @param keys
467      * @since 3.0
468      */

469     public void addKeys(OverlayKey[] keys) {
470         Assert.isTrue(!fLoaded);
471         Assert.isNotNull(keys);
472         
473         int overlayKeysLength= fOverlayKeys.length;
474         OverlayKey[] result= new OverlayKey[keys.length + overlayKeysLength];
475
476         for (int i= 0, length= overlayKeysLength; i < length; i++)
477             result[i]= fOverlayKeys[i];
478         
479         for (int i= 0, length= keys.length; i < length; i++)
480             result[overlayKeysLength + i]= keys[i];
481         
482         fOverlayKeys= result;
483         
484         if (fLoaded)
485             load();
486     }
487 }
488
Popular Tags