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