2 * (c) Copyright IBM Corp. 2000, 2001.
6 package net.sourceforge.phpdt.internal.ui.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 {
19 public static final class TypeDescriptor {
20 private TypeDescriptor() {
24 public static final TypeDescriptor BOOLEAN= new TypeDescriptor();
25 public static final TypeDescriptor DOUBLE= new TypeDescriptor();
26 public static final TypeDescriptor FLOAT= new TypeDescriptor();
27 public static final TypeDescriptor INT= new TypeDescriptor();
28 public static final TypeDescriptor LONG= new TypeDescriptor();
29 public static final TypeDescriptor STRING= new TypeDescriptor();
31 public static class OverlayKey {
33 TypeDescriptor fDescriptor;
36 public OverlayKey(TypeDescriptor descriptor, String key) {
37 fDescriptor= descriptor;
42 private class PropertyListener implements IPropertyChangeListener {
45 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
47 public void propertyChange(PropertyChangeEvent event) {
48 OverlayKey key= findOverlayKey(event.getProperty());
50 propagateProperty(fParent, key, fStore);
55 private IPreferenceStore fParent;
56 private IPreferenceStore fStore;
57 private OverlayKey[] fOverlayKeys;
59 private PropertyListener fPropertyListener;
62 public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) {
64 fOverlayKeys= overlayKeys;
65 fStore= new PreferenceStore();
68 private OverlayKey findOverlayKey(String key) {
69 for (int i= 0; i < fOverlayKeys.length; i++) {
70 if (fOverlayKeys[i].fKey.equals(key))
71 return fOverlayKeys[i];
76 private boolean covers(String key) {
77 return (findOverlayKey(key) != null);
80 private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) {
82 if (orgin.isDefault(key.fKey)) {
83 if (!target.isDefault(key.fKey))
84 target.setToDefault(key.fKey);
88 TypeDescriptor d= key.fDescriptor;
91 boolean originValue= orgin.getBoolean(key.fKey);
92 boolean targetValue= target.getBoolean(key.fKey);
93 if (targetValue != originValue)
94 target.setValue(key.fKey, originValue);
96 } else if (DOUBLE == d) {
98 double originValue= orgin.getDouble(key.fKey);
99 double targetValue= target.getDouble(key.fKey);
100 if (targetValue != originValue)
101 target.setValue(key.fKey, originValue);
103 } else if (FLOAT == d) {
105 float originValue= orgin.getFloat(key.fKey);
106 float targetValue= target.getFloat(key.fKey);
107 if (targetValue != originValue)
108 target.setValue(key.fKey, originValue);
110 } else if (INT == d) {
112 int originValue= orgin.getInt(key.fKey);
113 int targetValue= target.getInt(key.fKey);
114 if (targetValue != originValue)
115 target.setValue(key.fKey, originValue);
117 } else if (LONG == d) {
119 long originValue= orgin.getLong(key.fKey);
120 long targetValue= target.getLong(key.fKey);
121 if (targetValue != originValue)
122 target.setValue(key.fKey, originValue);
124 } else if (STRING == d) {
126 String originValue= orgin.getString(key.fKey);
127 String targetValue= target.getString(key.fKey);
128 if (targetValue != null && originValue != null && !targetValue.equals(originValue))
129 target.setValue(key.fKey, originValue);
134 public void propagate() {
135 for (int i= 0; i < fOverlayKeys.length; i++)
136 propagateProperty(fStore, fOverlayKeys[i], fParent);
139 private void loadProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target, boolean forceInitialization) {
140 TypeDescriptor d= key.fDescriptor;
143 if (forceInitialization)
144 target.setValue(key.fKey, true);
145 target.setValue(key.fKey, orgin.getBoolean(key.fKey));
146 target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
148 } else if (DOUBLE == d) {
150 if (forceInitialization)
151 target.setValue(key.fKey, 1.0D);
152 target.setValue(key.fKey, orgin.getDouble(key.fKey));
153 target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
155 } else if (FLOAT == d) {
157 if (forceInitialization)
158 target.setValue(key.fKey, 1.0F);
159 target.setValue(key.fKey, orgin.getFloat(key.fKey));
160 target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
162 } else if (INT == d) {
164 if (forceInitialization)
165 target.setValue(key.fKey, 1);
166 target.setValue(key.fKey, orgin.getInt(key.fKey));
167 target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
169 } else if (LONG == d) {
171 if (forceInitialization)
172 target.setValue(key.fKey, 1L);
173 target.setValue(key.fKey, orgin.getLong(key.fKey));
174 target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
176 } else if (STRING == d) {
178 if (forceInitialization)
179 target.setValue(key.fKey, "1"); //$NON-NLS-1$
180 target.setValue(key.fKey, orgin.getString(key.fKey));
181 target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
187 for (int i= 0; i < fOverlayKeys.length; i++)
188 loadProperty(fParent, fOverlayKeys[i], fStore, true);
191 public void loadDefaults() {
192 for (int i= 0; i < fOverlayKeys.length; i++)
193 setToDefault(fOverlayKeys[i].fKey);
196 public void start() {
197 if (fPropertyListener == null) {
198 fPropertyListener= new PropertyListener();
199 fParent.addPropertyChangeListener(fPropertyListener);
204 if (fPropertyListener != null) {
205 fParent.removePropertyChangeListener(fPropertyListener);
206 fPropertyListener= null;
211 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
213 public void addPropertyChangeListener(IPropertyChangeListener listener) {
214 fStore.addPropertyChangeListener(listener);
218 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
220 public void removePropertyChangeListener(IPropertyChangeListener listener) {
221 fStore.removePropertyChangeListener(listener);
225 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
227 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) {
228 fStore.firePropertyChangeEvent(name, oldValue, newValue);
232 * @see IPreferenceStore#contains(String)
234 public boolean contains(String name) {
235 return fStore.contains(name);
239 * @see IPreferenceStore#getBoolean(String)
241 public boolean getBoolean(String name) {
242 return fStore.getBoolean(name);
246 * @see IPreferenceStore#getDefaultBoolean(String)
248 public boolean getDefaultBoolean(String name) {
249 return fStore.getDefaultBoolean(name);
253 * @see IPreferenceStore#getDefaultDouble(String)
255 public double getDefaultDouble(String name) {
256 return fStore.getDefaultDouble(name);
260 * @see IPreferenceStore#getDefaultFloat(String)
262 public float getDefaultFloat(String name) {
263 return fStore.getDefaultFloat(name);
267 * @see IPreferenceStore#getDefaultInt(String)
269 public int getDefaultInt(String name) {
270 return fStore.getDefaultInt(name);
274 * @see IPreferenceStore#getDefaultLong(String)
276 public long getDefaultLong(String name) {
277 return fStore.getDefaultLong(name);
281 * @see IPreferenceStore#getDefaultString(String)
283 public String getDefaultString(String name) {
284 return fStore.getDefaultString(name);
288 * @see IPreferenceStore#getDouble(String)
290 public double getDouble(String name) {
291 return fStore.getDouble(name);
295 * @see IPreferenceStore#getFloat(String)
297 public float getFloat(String name) {
298 return fStore.getFloat(name);
302 * @see IPreferenceStore#getInt(String)
304 public int getInt(String name) {
305 return fStore.getInt(name);
309 * @see IPreferenceStore#getLong(String)
311 public long getLong(String name) {
312 return fStore.getLong(name);
316 * @see IPreferenceStore#getString(String)
318 public String getString(String name) {
319 return fStore.getString(name);
323 * @see IPreferenceStore#isDefault(String)
325 public boolean isDefault(String name) {
326 return fStore.isDefault(name);
330 * @see IPreferenceStore#needsSaving()
332 public boolean needsSaving() {
333 return fStore.needsSaving();
337 * @see IPreferenceStore#putValue(String, String)
339 public void putValue(String name, String value) {
341 fStore.putValue(name, value);
345 * @see IPreferenceStore#setDefault(String, double)
347 public void setDefault(String name, double value) {
349 fStore.setDefault(name, value);
353 * @see IPreferenceStore#setDefault(String, float)
355 public void setDefault(String name, float value) {
357 fStore.setDefault(name, value);
361 * @see IPreferenceStore#setDefault(String, int)
363 public void setDefault(String name, int value) {
365 fStore.setDefault(name, value);
369 * @see IPreferenceStore#setDefault(String, long)
371 public void setDefault(String name, long value) {
373 fStore.setDefault(name, value);
377 * @see IPreferenceStore#setDefault(String, String)
379 public void setDefault(String name, String value) {
381 fStore.setDefault(name, value);
385 * @see IPreferenceStore#setDefault(String, boolean)
387 public void setDefault(String name, boolean value) {
389 fStore.setDefault(name, value);
393 * @see IPreferenceStore#setToDefault(String)
395 public void setToDefault(String name) {
396 fStore.setToDefault(name);
400 * @see IPreferenceStore#setValue(String, double)
402 public void setValue(String name, double value) {
404 fStore.setValue(name, value);
408 * @see IPreferenceStore#setValue(String, float)
410 public void setValue(String name, float value) {
412 fStore.setValue(name, value);
416 * @see IPreferenceStore#setValue(String, int)
418 public void setValue(String name, int value) {
420 fStore.setValue(name, value);
424 * @see IPreferenceStore#setValue(String, long)
426 public void setValue(String name, long value) {
428 fStore.setValue(name, value);
432 * @see IPreferenceStore#setValue(String, String)
434 public void setValue(String name, String value) {
436 fStore.setValue(name, value);
440 * @see IPreferenceStore#setValue(String, boolean)
442 public void setValue(String name, boolean value) {
444 fStore.setValue(name, value);