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();
25 public static final TypeDescriptor DOUBLE = new TypeDescriptor();
27 public static final TypeDescriptor FLOAT = new TypeDescriptor();
29 public static final TypeDescriptor INT = new TypeDescriptor();
31 public static final TypeDescriptor LONG = new TypeDescriptor();
33 public static final TypeDescriptor STRING = new TypeDescriptor();
35 public static class OverlayKey {
37 TypeDescriptor fDescriptor;
41 public OverlayKey(TypeDescriptor descriptor, String key) {
42 fDescriptor = descriptor;
47 private class PropertyListener implements IPropertyChangeListener {
50 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
52 public void propertyChange(PropertyChangeEvent event) {
53 OverlayKey key = findOverlayKey(event.getProperty());
55 propagateProperty(fParent, key, fStore);
59 private IPreferenceStore fParent;
61 private IPreferenceStore fStore;
63 private OverlayKey[] fOverlayKeys;
65 private PropertyListener fPropertyListener;
67 public OverlayPreferenceStore(IPreferenceStore parent,
68 OverlayKey[] overlayKeys) {
70 fOverlayKeys = overlayKeys;
71 fStore = new PreferenceStore();
74 private OverlayKey findOverlayKey(String key) {
75 for (int i = 0; i < fOverlayKeys.length; i++) {
76 if (fOverlayKeys[i].fKey.equals(key))
77 return fOverlayKeys[i];
82 private boolean covers(String key) {
83 return (findOverlayKey(key) != null);
86 private void propagateProperty(IPreferenceStore orgin, OverlayKey key,
87 IPreferenceStore target) {
89 if (orgin.isDefault(key.fKey)) {
90 if (!target.isDefault(key.fKey))
91 target.setToDefault(key.fKey);
95 TypeDescriptor d = key.fDescriptor;
98 boolean originValue = orgin.getBoolean(key.fKey);
99 boolean targetValue = target.getBoolean(key.fKey);
100 if (targetValue != originValue)
101 target.setValue(key.fKey, originValue);
103 } else if (DOUBLE == d) {
105 double originValue = orgin.getDouble(key.fKey);
106 double targetValue = target.getDouble(key.fKey);
107 if (targetValue != originValue)
108 target.setValue(key.fKey, originValue);
110 } else if (FLOAT == d) {
112 float originValue = orgin.getFloat(key.fKey);
113 float targetValue = target.getFloat(key.fKey);
114 if (targetValue != originValue)
115 target.setValue(key.fKey, originValue);
117 } else if (INT == d) {
119 int originValue = orgin.getInt(key.fKey);
120 int targetValue = target.getInt(key.fKey);
121 if (targetValue != originValue)
122 target.setValue(key.fKey, originValue);
124 } else if (LONG == d) {
126 long originValue = orgin.getLong(key.fKey);
127 long targetValue = target.getLong(key.fKey);
128 if (targetValue != originValue)
129 target.setValue(key.fKey, originValue);
131 } else if (STRING == d) {
133 String originValue = orgin.getString(key.fKey);
134 String targetValue = target.getString(key.fKey);
135 if (targetValue != null && originValue != null
136 && !targetValue.equals(originValue))
137 target.setValue(key.fKey, originValue);
142 public void propagate() {
143 for (int i = 0; i < fOverlayKeys.length; i++)
144 propagateProperty(fStore, fOverlayKeys[i], fParent);
147 private void loadProperty(IPreferenceStore orgin, OverlayKey key,
148 IPreferenceStore target, boolean forceInitialization) {
149 TypeDescriptor d = key.fDescriptor;
152 if (forceInitialization)
153 target.setValue(key.fKey, true);
154 target.setValue(key.fKey, orgin.getBoolean(key.fKey));
155 target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
157 } else if (DOUBLE == d) {
159 if (forceInitialization)
160 target.setValue(key.fKey, 1.0D);
161 target.setValue(key.fKey, orgin.getDouble(key.fKey));
162 target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
164 } else if (FLOAT == d) {
166 if (forceInitialization)
167 target.setValue(key.fKey, 1.0F);
168 target.setValue(key.fKey, orgin.getFloat(key.fKey));
169 target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
171 } else if (INT == d) {
173 if (forceInitialization)
174 target.setValue(key.fKey, 1);
175 target.setValue(key.fKey, orgin.getInt(key.fKey));
176 target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
178 } else if (LONG == d) {
180 if (forceInitialization)
181 target.setValue(key.fKey, 1L);
182 target.setValue(key.fKey, orgin.getLong(key.fKey));
183 target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
185 } else if (STRING == d) {
187 if (forceInitialization)
188 target.setValue(key.fKey, "1"); //$NON-NLS-1$
189 target.setValue(key.fKey, orgin.getString(key.fKey));
190 target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
196 for (int i = 0; i < fOverlayKeys.length; i++)
197 loadProperty(fParent, fOverlayKeys[i], fStore, true);
200 public void loadDefaults() {
201 for (int i = 0; i < fOverlayKeys.length; i++)
202 setToDefault(fOverlayKeys[i].fKey);
205 public void start() {
206 if (fPropertyListener == null) {
207 fPropertyListener = new PropertyListener();
208 fParent.addPropertyChangeListener(fPropertyListener);
213 if (fPropertyListener != null) {
214 fParent.removePropertyChangeListener(fPropertyListener);
215 fPropertyListener = null;
220 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
222 public void addPropertyChangeListener(IPropertyChangeListener listener) {
223 fStore.addPropertyChangeListener(listener);
227 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
229 public void removePropertyChangeListener(IPropertyChangeListener listener) {
230 fStore.removePropertyChangeListener(listener);
234 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
236 public void firePropertyChangeEvent(String name, Object oldValue,
238 fStore.firePropertyChangeEvent(name, oldValue, newValue);
242 * @see IPreferenceStore#contains(String)
244 public boolean contains(String name) {
245 return fStore.contains(name);
249 * @see IPreferenceStore#getBoolean(String)
251 public boolean getBoolean(String name) {
252 return fStore.getBoolean(name);
256 * @see IPreferenceStore#getDefaultBoolean(String)
258 public boolean getDefaultBoolean(String name) {
259 return fStore.getDefaultBoolean(name);
263 * @see IPreferenceStore#getDefaultDouble(String)
265 public double getDefaultDouble(String name) {
266 return fStore.getDefaultDouble(name);
270 * @see IPreferenceStore#getDefaultFloat(String)
272 public float getDefaultFloat(String name) {
273 return fStore.getDefaultFloat(name);
277 * @see IPreferenceStore#getDefaultInt(String)
279 public int getDefaultInt(String name) {
280 return fStore.getDefaultInt(name);
284 * @see IPreferenceStore#getDefaultLong(String)
286 public long getDefaultLong(String name) {
287 return fStore.getDefaultLong(name);
291 * @see IPreferenceStore#getDefaultString(String)
293 public String getDefaultString(String name) {
294 return fStore.getDefaultString(name);
298 * @see IPreferenceStore#getDouble(String)
300 public double getDouble(String name) {
301 return fStore.getDouble(name);
305 * @see IPreferenceStore#getFloat(String)
307 public float getFloat(String name) {
308 return fStore.getFloat(name);
312 * @see IPreferenceStore#getInt(String)
314 public int getInt(String name) {
315 return fStore.getInt(name);
319 * @see IPreferenceStore#getLong(String)
321 public long getLong(String name) {
322 return fStore.getLong(name);
326 * @see IPreferenceStore#getString(String)
328 public String getString(String name) {
329 return fStore.getString(name);
333 * @see IPreferenceStore#isDefault(String)
335 public boolean isDefault(String name) {
336 return fStore.isDefault(name);
340 * @see IPreferenceStore#needsSaving()
342 public boolean needsSaving() {
343 return fStore.needsSaving();
347 * @see IPreferenceStore#putValue(String, String)
349 public void putValue(String name, String value) {
351 fStore.putValue(name, value);
355 * @see IPreferenceStore#setDefault(String, double)
357 public void setDefault(String name, double value) {
359 fStore.setDefault(name, value);
363 * @see IPreferenceStore#setDefault(String, float)
365 public void setDefault(String name, float value) {
367 fStore.setDefault(name, value);
371 * @see IPreferenceStore#setDefault(String, int)
373 public void setDefault(String name, int value) {
375 fStore.setDefault(name, value);
379 * @see IPreferenceStore#setDefault(String, long)
381 public void setDefault(String name, long value) {
383 fStore.setDefault(name, value);
387 * @see IPreferenceStore#setDefault(String, String)
389 public void setDefault(String name, String value) {
391 fStore.setDefault(name, value);
395 * @see IPreferenceStore#setDefault(String, boolean)
397 public void setDefault(String name, boolean value) {
399 fStore.setDefault(name, value);
403 * @see IPreferenceStore#setToDefault(String)
405 public void setToDefault(String name) {
406 fStore.setToDefault(name);
410 * @see IPreferenceStore#setValue(String, double)
412 public void setValue(String name, double value) {
414 fStore.setValue(name, value);
418 * @see IPreferenceStore#setValue(String, float)
420 public void setValue(String name, float value) {
422 fStore.setValue(name, value);
426 * @see IPreferenceStore#setValue(String, int)
428 public void setValue(String name, int value) {
430 fStore.setValue(name, value);
434 * @see IPreferenceStore#setValue(String, long)
436 public void setValue(String name, long value) {
438 fStore.setValue(name, value);
442 * @see IPreferenceStore#setValue(String, String)
444 public void setValue(String name, String value) {
446 fStore.setValue(name, value);
450 * @see IPreferenceStore#setValue(String, boolean)
452 public void setValue(String name, boolean value) {
454 fStore.setValue(name, value);