a8d3a8a453c26dad22cf32b253a01eb754c4f7c0
[phpeclipse.git] /
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5  
6 package net.sourceforge.phpdt.internal.ui.preferences;
7
8 import org.eclipse.jface.preference.IPreferenceStore;
9 import org.eclipse.jface.preference.PreferenceStore;
10 import org.eclipse.jface.util.IPropertyChangeListener;
11 import org.eclipse.jface.util.PropertyChangeEvent;
12
13 /**
14  * An overlaying preference store.
15  */
16 public class OverlayPreferenceStore  implements IPreferenceStore {
17         
18         
19         public static final class TypeDescriptor {
20                 private TypeDescriptor() {
21                 }
22         };
23         
24         public static final TypeDescriptor BOOLEAN= new TypeDescriptor();
25         public static final TypeDescriptor DOUBLE= new TypeDescriptor();
26         public static final TypeDescriptor FLOAT= new TypeDescriptor();
27         public static final TypeDescriptor INT= new TypeDescriptor();
28         public static final TypeDescriptor LONG= new TypeDescriptor();
29         public static final TypeDescriptor STRING= new TypeDescriptor();
30         
31         public static class OverlayKey {
32                 
33                 TypeDescriptor fDescriptor;
34                 String fKey;
35                 
36                 public OverlayKey(TypeDescriptor descriptor, String key) {
37                         fDescriptor= descriptor;
38                         fKey= key;
39                 }
40         };
41         
42         private class PropertyListener implements IPropertyChangeListener {
43                                 
44                 /*
45                  * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
46                  */
47                 public void propertyChange(PropertyChangeEvent event) {
48                         OverlayKey key= findOverlayKey(event.getProperty());
49                         if (key != null)
50                                 propagateProperty(fParent, key, fStore); 
51                 }
52         };
53         
54         
55         private IPreferenceStore fParent;
56         private IPreferenceStore fStore;
57         private OverlayKey[] fOverlayKeys;
58         
59         private PropertyListener fPropertyListener;
60         
61         
62         public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) {
63                 fParent= parent;
64                 fOverlayKeys= overlayKeys;
65                 fStore= new PreferenceStore();
66         }
67         
68         private OverlayKey findOverlayKey(String key) {
69                 for (int i= 0; i < fOverlayKeys.length; i++) {
70                         if (fOverlayKeys[i].fKey.equals(key))
71                                 return fOverlayKeys[i];
72                 }
73                 return null;
74         }
75         
76         private boolean covers(String key) {
77                 return (findOverlayKey(key) != null);
78         }
79         
80         private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) {
81                 
82                 if (orgin.isDefault(key.fKey)) {
83                         if (!target.isDefault(key.fKey))
84                                 target.setToDefault(key.fKey);
85                         return;
86                 }
87                 
88                 TypeDescriptor d= key.fDescriptor;
89                 if (BOOLEAN == d) {
90                         
91                         boolean originValue= orgin.getBoolean(key.fKey);
92                         boolean targetValue= target.getBoolean(key.fKey);
93                         if (targetValue != originValue)
94                                 target.setValue(key.fKey, originValue);
95                                 
96                 } else if (DOUBLE == d) {
97                         
98                         double originValue= orgin.getDouble(key.fKey);
99                         double targetValue= target.getDouble(key.fKey);
100                         if (targetValue != originValue)
101                                 target.setValue(key.fKey, originValue);
102                 
103                 } else if (FLOAT == d) {
104                         
105                         float originValue= orgin.getFloat(key.fKey);
106                         float targetValue= target.getFloat(key.fKey);
107                         if (targetValue != originValue)
108                                 target.setValue(key.fKey, originValue);
109                                 
110                 } else if (INT == d) {
111
112                         int originValue= orgin.getInt(key.fKey);
113                         int targetValue= target.getInt(key.fKey);
114                         if (targetValue != originValue)
115                                 target.setValue(key.fKey, originValue);
116
117                 } else if (LONG == d) {
118
119                         long originValue= orgin.getLong(key.fKey);
120                         long targetValue= target.getLong(key.fKey);
121                         if (targetValue != originValue)
122                                 target.setValue(key.fKey, originValue);
123
124                 } else if (STRING == d) {
125
126                         String originValue= orgin.getString(key.fKey);
127                         String targetValue= target.getString(key.fKey);
128                         if (targetValue != null && originValue != null && !targetValue.equals(originValue))
129                                 target.setValue(key.fKey, originValue);
130
131                 }
132         }
133         
134         public void propagate() {
135                 for (int i= 0; i < fOverlayKeys.length; i++)
136                         propagateProperty(fStore, fOverlayKeys[i], fParent);
137         }
138         
139         private void loadProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target, boolean forceInitialization) {
140                 TypeDescriptor d= key.fDescriptor;
141                 if (BOOLEAN == d) {
142                         
143                         if (forceInitialization)
144                                 target.setValue(key.fKey, true);
145                         target.setValue(key.fKey, orgin.getBoolean(key.fKey));
146                         target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
147                         
148                 } else if (DOUBLE == d) {
149                         
150                         if (forceInitialization)
151                                 target.setValue(key.fKey, 1.0D);
152                         target.setValue(key.fKey, orgin.getDouble(key.fKey));
153                         target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
154                         
155                 } else if (FLOAT == d) {
156                         
157                         if (forceInitialization)
158                                 target.setValue(key.fKey, 1.0F);
159                         target.setValue(key.fKey, orgin.getFloat(key.fKey));
160                         target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
161                         
162                 } else if (INT == d) {
163                         
164                         if (forceInitialization)
165                                 target.setValue(key.fKey, 1);
166                         target.setValue(key.fKey, orgin.getInt(key.fKey));
167                         target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
168                         
169                 } else if (LONG == d) {
170                         
171                         if (forceInitialization)
172                                 target.setValue(key.fKey, 1L);
173                         target.setValue(key.fKey, orgin.getLong(key.fKey));
174                         target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
175                         
176                 } else if (STRING == d) {
177                         
178                         if (forceInitialization)
179                                 target.setValue(key.fKey, "1"); //$NON-NLS-1$
180                         target.setValue(key.fKey, orgin.getString(key.fKey));
181                         target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
182                         
183                 }
184         }
185         
186         public void load() {
187                 for (int i= 0; i < fOverlayKeys.length; i++)
188                         loadProperty(fParent, fOverlayKeys[i], fStore, true);
189         }
190         
191         public void loadDefaults() {
192                 for (int i= 0; i < fOverlayKeys.length; i++)
193                         setToDefault(fOverlayKeys[i].fKey);
194         }
195         
196         public void start() {
197                 if (fPropertyListener == null) {
198                         fPropertyListener= new PropertyListener();
199                         fParent.addPropertyChangeListener(fPropertyListener);
200                 }
201         }
202         
203         public void stop() {
204                 if (fPropertyListener != null)  {
205                         fParent.removePropertyChangeListener(fPropertyListener);
206                         fPropertyListener= null;
207                 }
208         }
209         
210         /*
211          * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
212          */
213         public void addPropertyChangeListener(IPropertyChangeListener listener) {
214                 fStore.addPropertyChangeListener(listener);
215         }
216         
217         /*
218          * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
219          */
220         public void removePropertyChangeListener(IPropertyChangeListener listener) {
221                 fStore.removePropertyChangeListener(listener);
222         }
223         
224         /*
225          * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
226          */
227         public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) {
228                 fStore.firePropertyChangeEvent(name, oldValue, newValue);
229         }
230
231         /*
232          * @see IPreferenceStore#contains(String)
233          */
234         public boolean contains(String name) {
235                 return fStore.contains(name);
236         }
237         
238         /*
239          * @see IPreferenceStore#getBoolean(String)
240          */
241         public boolean getBoolean(String name) {
242                 return fStore.getBoolean(name);
243         }
244
245         /*
246          * @see IPreferenceStore#getDefaultBoolean(String)
247          */
248         public boolean getDefaultBoolean(String name) {
249                 return fStore.getDefaultBoolean(name);
250         }
251
252         /*
253          * @see IPreferenceStore#getDefaultDouble(String)
254          */
255         public double getDefaultDouble(String name) {
256                 return fStore.getDefaultDouble(name);
257         }
258
259         /*
260          * @see IPreferenceStore#getDefaultFloat(String)
261          */
262         public float getDefaultFloat(String name) {
263                 return fStore.getDefaultFloat(name);
264         }
265
266         /*
267          * @see IPreferenceStore#getDefaultInt(String)
268          */
269         public int getDefaultInt(String name) {
270                 return fStore.getDefaultInt(name);
271         }
272
273         /*
274          * @see IPreferenceStore#getDefaultLong(String)
275          */
276         public long getDefaultLong(String name) {
277                 return fStore.getDefaultLong(name);
278         }
279
280         /*
281          * @see IPreferenceStore#getDefaultString(String)
282          */
283         public String getDefaultString(String name) {
284                 return fStore.getDefaultString(name);
285         }
286
287         /*
288          * @see IPreferenceStore#getDouble(String)
289          */
290         public double getDouble(String name) {
291                 return fStore.getDouble(name);
292         }
293
294         /*
295          * @see IPreferenceStore#getFloat(String)
296          */
297         public float getFloat(String name) {
298                 return fStore.getFloat(name);
299         }
300
301         /*
302          * @see IPreferenceStore#getInt(String)
303          */
304         public int getInt(String name) {
305                 return fStore.getInt(name);
306         }
307
308         /*
309          * @see IPreferenceStore#getLong(String)
310          */
311         public long getLong(String name) {
312                 return fStore.getLong(name);
313         }
314
315         /*
316          * @see IPreferenceStore#getString(String)
317          */
318         public String getString(String name) {
319                 return fStore.getString(name);
320         }
321
322         /*
323          * @see IPreferenceStore#isDefault(String)
324          */
325         public boolean isDefault(String name) {
326                 return fStore.isDefault(name);
327         }
328
329         /*
330          * @see IPreferenceStore#needsSaving()
331          */
332         public boolean needsSaving() {
333                 return fStore.needsSaving();
334         }
335
336         /*
337          * @see IPreferenceStore#putValue(String, String)
338          */
339         public void putValue(String name, String value) {
340                 if (covers(name))
341                         fStore.putValue(name, value);
342         }
343
344         /*
345          * @see IPreferenceStore#setDefault(String, double)
346          */
347         public void setDefault(String name, double value) {
348                 if (covers(name))
349                         fStore.setDefault(name, value);
350         }
351
352         /*
353          * @see IPreferenceStore#setDefault(String, float)
354          */
355         public void setDefault(String name, float value) {
356                 if (covers(name))
357                         fStore.setDefault(name, value);
358         }
359
360         /*
361          * @see IPreferenceStore#setDefault(String, int)
362          */
363         public void setDefault(String name, int value) {
364                 if (covers(name))
365                         fStore.setDefault(name, value);
366         }
367
368         /*
369          * @see IPreferenceStore#setDefault(String, long)
370          */
371         public void setDefault(String name, long value) {
372                 if (covers(name))
373                         fStore.setDefault(name, value);
374         }
375
376         /*
377          * @see IPreferenceStore#setDefault(String, String)
378          */
379         public void setDefault(String name, String value) {
380                 if (covers(name))
381                         fStore.setDefault(name, value);
382         }
383
384         /*
385          * @see IPreferenceStore#setDefault(String, boolean)
386          */
387         public void setDefault(String name, boolean value) {
388                 if (covers(name))
389                         fStore.setDefault(name, value);
390         }
391
392         /*
393          * @see IPreferenceStore#setToDefault(String)
394          */
395         public void setToDefault(String name) {
396                 fStore.setToDefault(name);
397         }
398
399         /*
400          * @see IPreferenceStore#setValue(String, double)
401          */
402         public void setValue(String name, double value) {
403                 if (covers(name))
404                         fStore.setValue(name, value);
405         }
406
407         /*
408          * @see IPreferenceStore#setValue(String, float)
409          */
410         public void setValue(String name, float value) {
411                 if (covers(name))
412                         fStore.setValue(name, value);
413         }
414
415         /*
416          * @see IPreferenceStore#setValue(String, int)
417          */
418         public void setValue(String name, int value) {
419                 if (covers(name))
420                         fStore.setValue(name, value);
421         }
422
423         /*
424          * @see IPreferenceStore#setValue(String, long)
425          */
426         public void setValue(String name, long value) {
427                 if (covers(name))
428                         fStore.setValue(name, value);
429         }
430
431         /*
432          * @see IPreferenceStore#setValue(String, String)
433          */
434         public void setValue(String name, String value) {
435                 if (covers(name))
436                         fStore.setValue(name, value);
437         }
438
439         /*
440          * @see IPreferenceStore#setValue(String, boolean)
441          */
442         public void setValue(String name, boolean value) {
443                 if (covers(name))
444                         fStore.setValue(name, value);
445         }
446 }