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