2 * (c) Copyright IBM Corp. 2000, 2001.
6 package net.sourceforge.phpeclipse.preferences;
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;
14 * An overlaying preference store.
16 public class OverlayPreferenceStore implements IPreferenceStore {
18 public static final class TypeDescriptor {
19 private TypeDescriptor() {
23 public static final TypeDescriptor BOOLEAN = new TypeDescriptor();
24 public static final TypeDescriptor DOUBLE = new TypeDescriptor();
25 public static final TypeDescriptor FLOAT = new TypeDescriptor();
26 public static final TypeDescriptor INT = new TypeDescriptor();
27 public static final TypeDescriptor LONG = new TypeDescriptor();
28 public static final TypeDescriptor STRING = new TypeDescriptor();
30 public static class OverlayKey {
32 TypeDescriptor fDescriptor;
35 public OverlayKey(TypeDescriptor descriptor, String key) {
36 fDescriptor = descriptor;
41 private class PropertyListener implements IPropertyChangeListener {
44 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
46 public void propertyChange(PropertyChangeEvent event) {
47 OverlayKey key = findOverlayKey(event.getProperty());
49 propagateProperty(fParent, key, fStore);
53 private IPreferenceStore fParent;
54 private IPreferenceStore fStore;
55 private OverlayKey[] fOverlayKeys;
57 private PropertyListener fPropertyListener;
59 public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) {
61 fOverlayKeys = overlayKeys;
62 fStore = new PreferenceStore();
65 private OverlayKey findOverlayKey(String key) {
66 for (int i = 0; i < fOverlayKeys.length; i++) {
67 if (fOverlayKeys[i].fKey.equals(key))
68 return fOverlayKeys[i];
73 private boolean covers(String key) {
74 return (findOverlayKey(key) != null);
77 private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) {
79 if (orgin.isDefault(key.fKey)) {
80 if (!target.isDefault(key.fKey))
81 target.setToDefault(key.fKey);
85 TypeDescriptor d = key.fDescriptor;
88 boolean originValue = orgin.getBoolean(key.fKey);
89 boolean targetValue = target.getBoolean(key.fKey);
90 if (targetValue != originValue)
91 target.setValue(key.fKey, originValue);
93 } else if (DOUBLE == d) {
95 double originValue = orgin.getDouble(key.fKey);
96 double targetValue = target.getDouble(key.fKey);
97 if (targetValue != originValue)
98 target.setValue(key.fKey, originValue);
100 } else if (FLOAT == d) {
102 float originValue = orgin.getFloat(key.fKey);
103 float targetValue = target.getFloat(key.fKey);
104 if (targetValue != originValue)
105 target.setValue(key.fKey, originValue);
107 } else if (INT == d) {
109 int originValue = orgin.getInt(key.fKey);
110 int targetValue = target.getInt(key.fKey);
111 if (targetValue != originValue)
112 target.setValue(key.fKey, originValue);
114 } else if (LONG == d) {
116 long originValue = orgin.getLong(key.fKey);
117 long targetValue = target.getLong(key.fKey);
118 if (targetValue != originValue)
119 target.setValue(key.fKey, originValue);
121 } else if (STRING == d) {
123 String originValue = orgin.getString(key.fKey);
124 String targetValue = target.getString(key.fKey);
125 if (targetValue != null && originValue != null && !targetValue.equals(originValue))
126 target.setValue(key.fKey, originValue);
131 public void propagate() {
132 for (int i = 0; i < fOverlayKeys.length; i++)
133 propagateProperty(fStore, fOverlayKeys[i], fParent);
136 private void loadProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target, boolean forceInitialization) {
137 TypeDescriptor d = key.fDescriptor;
140 if (forceInitialization)
141 target.setValue(key.fKey, true);
142 target.setValue(key.fKey, orgin.getBoolean(key.fKey));
143 target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
145 } else if (DOUBLE == d) {
147 if (forceInitialization)
148 target.setValue(key.fKey, 1.0D);
149 target.setValue(key.fKey, orgin.getDouble(key.fKey));
150 target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
152 } else if (FLOAT == d) {
154 if (forceInitialization)
155 target.setValue(key.fKey, 1.0F);
156 target.setValue(key.fKey, orgin.getFloat(key.fKey));
157 target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
159 } else if (INT == d) {
161 if (forceInitialization)
162 target.setValue(key.fKey, 1);
163 target.setValue(key.fKey, orgin.getInt(key.fKey));
164 target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
166 } else if (LONG == d) {
168 if (forceInitialization)
169 target.setValue(key.fKey, 1L);
170 target.setValue(key.fKey, orgin.getLong(key.fKey));
171 target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
173 } else if (STRING == d) {
175 if (forceInitialization)
176 target.setValue(key.fKey, "1"); //$NON-NLS-1$
177 target.setValue(key.fKey, orgin.getString(key.fKey));
178 target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
184 for (int i = 0; i < fOverlayKeys.length; i++)
185 loadProperty(fParent, fOverlayKeys[i], fStore, true);
188 public void loadDefaults() {
189 for (int i = 0; i < fOverlayKeys.length; i++)
190 setToDefault(fOverlayKeys[i].fKey);
193 public void start() {
194 if (fPropertyListener == null) {
195 fPropertyListener = new PropertyListener();
196 fParent.addPropertyChangeListener(fPropertyListener);
201 if (fPropertyListener != null) {
202 fParent.removePropertyChangeListener(fPropertyListener);
203 fPropertyListener = null;
208 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
210 public void addPropertyChangeListener(IPropertyChangeListener listener) {
211 fStore.addPropertyChangeListener(listener);
215 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
217 public void removePropertyChangeListener(IPropertyChangeListener listener) {
218 fStore.removePropertyChangeListener(listener);
222 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
224 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) {
225 fStore.firePropertyChangeEvent(name, oldValue, newValue);
229 * @see IPreferenceStore#contains(String)
231 public boolean contains(String name) {
232 return fStore.contains(name);
236 * @see IPreferenceStore#getBoolean(String)
238 public boolean getBoolean(String name) {
239 return fStore.getBoolean(name);
243 * @see IPreferenceStore#getDefaultBoolean(String)
245 public boolean getDefaultBoolean(String name) {
246 return fStore.getDefaultBoolean(name);
250 * @see IPreferenceStore#getDefaultDouble(String)
252 public double getDefaultDouble(String name) {
253 return fStore.getDefaultDouble(name);
257 * @see IPreferenceStore#getDefaultFloat(String)
259 public float getDefaultFloat(String name) {
260 return fStore.getDefaultFloat(name);
264 * @see IPreferenceStore#getDefaultInt(String)
266 public int getDefaultInt(String name) {
267 return fStore.getDefaultInt(name);
271 * @see IPreferenceStore#getDefaultLong(String)
273 public long getDefaultLong(String name) {
274 return fStore.getDefaultLong(name);
278 * @see IPreferenceStore#getDefaultString(String)
280 public String getDefaultString(String name) {
281 return fStore.getDefaultString(name);
285 * @see IPreferenceStore#getDouble(String)
287 public double getDouble(String name) {
288 return fStore.getDouble(name);
292 * @see IPreferenceStore#getFloat(String)
294 public float getFloat(String name) {
295 return fStore.getFloat(name);
299 * @see IPreferenceStore#getInt(String)
301 public int getInt(String name) {
302 return fStore.getInt(name);
306 * @see IPreferenceStore#getLong(String)
308 public long getLong(String name) {
309 return fStore.getLong(name);
313 * @see IPreferenceStore#getString(String)
315 public String getString(String name) {
316 return fStore.getString(name);
320 * @see IPreferenceStore#isDefault(String)
322 public boolean isDefault(String name) {
323 return fStore.isDefault(name);
327 * @see IPreferenceStore#needsSaving()
329 public boolean needsSaving() {
330 return fStore.needsSaving();
334 * @see IPreferenceStore#putValue(String, String)
336 public void putValue(String name, String value) {
338 fStore.putValue(name, value);
342 * @see IPreferenceStore#setDefault(String, double)
344 public void setDefault(String name, double value) {
346 fStore.setDefault(name, value);
350 * @see IPreferenceStore#setDefault(String, float)
352 public void setDefault(String name, float value) {
354 fStore.setDefault(name, value);
358 * @see IPreferenceStore#setDefault(String, int)
360 public void setDefault(String name, int value) {
362 fStore.setDefault(name, value);
366 * @see IPreferenceStore#setDefault(String, long)
368 public void setDefault(String name, long value) {
370 fStore.setDefault(name, value);
374 * @see IPreferenceStore#setDefault(String, String)
376 public void setDefault(String name, String value) {
378 fStore.setDefault(name, value);
382 * @see IPreferenceStore#setDefault(String, boolean)
384 public void setDefault(String name, boolean value) {
386 fStore.setDefault(name, value);
390 * @see IPreferenceStore#setToDefault(String)
392 public void setToDefault(String name) {
393 fStore.setToDefault(name);
397 * @see IPreferenceStore#setValue(String, double)
399 public void setValue(String name, double value) {
401 fStore.setValue(name, value);
405 * @see IPreferenceStore#setValue(String, float)
407 public void setValue(String name, float value) {
409 fStore.setValue(name, value);
413 * @see IPreferenceStore#setValue(String, int)
415 public void setValue(String name, int value) {
417 fStore.setValue(name, value);
421 * @see IPreferenceStore#setValue(String, long)
423 public void setValue(String name, long value) {
425 fStore.setValue(name, value);
429 * @see IPreferenceStore#setValue(String, String)
431 public void setValue(String name, String value) {
433 fStore.setValue(name, value);
437 * @see IPreferenceStore#setValue(String, boolean)
439 public void setValue(String name, boolean value) {
441 fStore.setValue(name, value);