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