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