KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > 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.compare.internal;
12
13 import org.eclipse.jface.preference.IPreferenceStore;
14 import org.eclipse.jface.preference.PreferenceStore;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17
18 /**
19  * An overlaying preference store.
20  */

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

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

219     public void addPropertyChangeListener(IPropertyChangeListener listener) {
220         fStore.addPropertyChangeListener(listener);
221     }
222     
223     /*
224      * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
225      */

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

233     public void firePropertyChangeEvent(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
234         fStore.firePropertyChangeEvent(name, oldValue, newValue);
235     }
236
237     /*
238      * @see IPreferenceStore#contains(String)
239      */

240     public boolean contains(String JavaDoc name) {
241         return fStore.contains(name);
242     }
243     
244     /*
245      * @see IPreferenceStore#getBoolean(String)
246      */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

345     public void putValue(String JavaDoc name, String JavaDoc value) {
346         if (covers(name))
347             fStore.putValue(name, value);
348     }
349
350     /*
351      * @see IPreferenceStore#setDefault(String, double)
352      */

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

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

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

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

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

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

401     public void setToDefault(String JavaDoc name) {
402         fStore.setToDefault(name);
403     }
404
405     /*
406      * @see IPreferenceStore#setValue(String, double)
407      */

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

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

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

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

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

448     public void setValue(String JavaDoc name, boolean value) {
449         if (covers(name))
450             fStore.setValue(name, value);
451     }
452 }
453
Popular Tags