KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > preferences > OverlayPreferenceStore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ant.internal.ui.preferences;
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 class OverlayPreferenceStore implements IPreferenceStore {
22     
23     
24     public static final class TypeDescriptor {
25         private TypeDescriptor() {
26         }
27     }
28     
29     public static final TypeDescriptor BOOLEAN= new TypeDescriptor();
30     public static final TypeDescriptor DOUBLE= new TypeDescriptor();
31     public static final TypeDescriptor FLOAT= new TypeDescriptor();
32     public static final TypeDescriptor INT= new TypeDescriptor();
33     public static final TypeDescriptor LONG= new TypeDescriptor();
34     public static final TypeDescriptor STRING= new TypeDescriptor();
35     
36     public static class OverlayKey {
37         
38         TypeDescriptor fDescriptor;
39         String JavaDoc fKey;
40         
41         public OverlayKey(TypeDescriptor descriptor, String JavaDoc key) {
42             fDescriptor= descriptor;
43             fKey= key;
44         }
45     }
46     
47     private class PropertyListener implements IPropertyChangeListener {
48                 
49         /*
50          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
51          */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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