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