1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation 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://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.preferences;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.preference.PreferenceStore;
17 //import org.eclipse.jface.text.Assert;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
23 * An overlaying preference store.
25 public class OverlayPreferenceStore implements IPreferenceStore {
27 public static final class TypeDescriptor {
28 private TypeDescriptor() {
32 public static final TypeDescriptor BOOLEAN = new TypeDescriptor();
34 public static final TypeDescriptor DOUBLE = new TypeDescriptor();
36 public static final TypeDescriptor FLOAT = new TypeDescriptor();
38 public static final TypeDescriptor INT = new TypeDescriptor();
40 public static final TypeDescriptor LONG = new TypeDescriptor();
42 public static final TypeDescriptor STRING = new TypeDescriptor();
44 public static class OverlayKey {
46 TypeDescriptor fDescriptor;
50 public OverlayKey(TypeDescriptor descriptor, String key) {
51 fDescriptor = descriptor;
56 private class PropertyListener implements IPropertyChangeListener {
59 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
61 public void propertyChange(PropertyChangeEvent event) {
62 OverlayKey key = findOverlayKey(event.getProperty());
64 propagateProperty(fParent, key, fStore);
68 private IPreferenceStore fParent;
70 private IPreferenceStore fStore;
72 private OverlayKey[] fOverlayKeys;
74 private PropertyListener fPropertyListener;
76 private boolean fLoaded;
78 public OverlayPreferenceStore(IPreferenceStore parent,
79 OverlayKey[] overlayKeys) {
81 fOverlayKeys = overlayKeys;
82 fStore = new PreferenceStore();
85 private OverlayKey findOverlayKey(String key) {
86 for (int i = 0; i < fOverlayKeys.length; i++) {
87 if (fOverlayKeys[i].fKey.equals(key))
88 return fOverlayKeys[i];
93 private boolean covers(String key) {
94 return (findOverlayKey(key) != null);
97 private void propagateProperty(IPreferenceStore orgin, OverlayKey key,
98 IPreferenceStore target) {
100 if (orgin.isDefault(key.fKey)) {
101 if (!target.isDefault(key.fKey))
102 target.setToDefault(key.fKey);
106 TypeDescriptor d = key.fDescriptor;
109 boolean originValue = orgin.getBoolean(key.fKey);
110 boolean targetValue = target.getBoolean(key.fKey);
111 if (targetValue != originValue)
112 target.setValue(key.fKey, originValue);
114 } else if (DOUBLE == d) {
116 double originValue = orgin.getDouble(key.fKey);
117 double targetValue = target.getDouble(key.fKey);
118 if (targetValue != originValue)
119 target.setValue(key.fKey, originValue);
121 } else if (FLOAT == d) {
123 float originValue = orgin.getFloat(key.fKey);
124 float targetValue = target.getFloat(key.fKey);
125 if (targetValue != originValue)
126 target.setValue(key.fKey, originValue);
128 } else if (INT == d) {
130 int originValue = orgin.getInt(key.fKey);
131 int targetValue = target.getInt(key.fKey);
132 if (targetValue != originValue)
133 target.setValue(key.fKey, originValue);
135 } else if (LONG == d) {
137 long originValue = orgin.getLong(key.fKey);
138 long targetValue = target.getLong(key.fKey);
139 if (targetValue != originValue)
140 target.setValue(key.fKey, originValue);
142 } else if (STRING == d) {
144 String originValue = orgin.getString(key.fKey);
145 String targetValue = target.getString(key.fKey);
146 if (targetValue != null && originValue != null
147 && !targetValue.equals(originValue))
148 target.setValue(key.fKey, originValue);
153 public void propagate() {
154 for (int i = 0; i < fOverlayKeys.length; i++)
155 propagateProperty(fStore, fOverlayKeys[i], fParent);
158 private void loadProperty(IPreferenceStore orgin, OverlayKey key,
159 IPreferenceStore target, boolean forceInitialization) {
160 TypeDescriptor d = key.fDescriptor;
163 if (forceInitialization)
164 target.setValue(key.fKey, true);
165 target.setValue(key.fKey, orgin.getBoolean(key.fKey));
166 target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
168 } else if (DOUBLE == d) {
170 if (forceInitialization)
171 target.setValue(key.fKey, 1.0D);
172 target.setValue(key.fKey, orgin.getDouble(key.fKey));
173 target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
175 } else if (FLOAT == d) {
177 if (forceInitialization)
178 target.setValue(key.fKey, 1.0F);
179 target.setValue(key.fKey, orgin.getFloat(key.fKey));
180 target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
182 } else if (INT == d) {
184 if (forceInitialization)
185 target.setValue(key.fKey, 1);
186 target.setValue(key.fKey, orgin.getInt(key.fKey));
187 target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
189 } else if (LONG == d) {
191 if (forceInitialization)
192 target.setValue(key.fKey, 1L);
193 target.setValue(key.fKey, orgin.getLong(key.fKey));
194 target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
196 } else if (STRING == d) {
198 if (forceInitialization)
199 target.setValue(key.fKey, "1"); //$NON-NLS-1$
200 target.setValue(key.fKey, orgin.getString(key.fKey));
201 target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
207 for (int i = 0; i < fOverlayKeys.length; i++)
208 loadProperty(fParent, fOverlayKeys[i], fStore, true);
214 public void loadDefaults() {
215 for (int i = 0; i < fOverlayKeys.length; i++)
216 setToDefault(fOverlayKeys[i].fKey);
219 public void start() {
220 if (fPropertyListener == null) {
221 fPropertyListener = new PropertyListener();
222 fParent.addPropertyChangeListener(fPropertyListener);
227 if (fPropertyListener != null) {
228 fParent.removePropertyChangeListener(fPropertyListener);
229 fPropertyListener = null;
234 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
236 public void addPropertyChangeListener(IPropertyChangeListener listener) {
237 fStore.addPropertyChangeListener(listener);
241 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
243 public void removePropertyChangeListener(IPropertyChangeListener listener) {
244 fStore.removePropertyChangeListener(listener);
248 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
250 public void firePropertyChangeEvent(String name, Object oldValue,
252 fStore.firePropertyChangeEvent(name, oldValue, newValue);
256 * @see IPreferenceStore#contains(String)
258 public boolean contains(String name) {
259 return fStore.contains(name);
263 * @see IPreferenceStore#getBoolean(String)
265 public boolean getBoolean(String name) {
266 return fStore.getBoolean(name);
270 * @see IPreferenceStore#getDefaultBoolean(String)
272 public boolean getDefaultBoolean(String name) {
273 return fStore.getDefaultBoolean(name);
277 * @see IPreferenceStore#getDefaultDouble(String)
279 public double getDefaultDouble(String name) {
280 return fStore.getDefaultDouble(name);
284 * @see IPreferenceStore#getDefaultFloat(String)
286 public float getDefaultFloat(String name) {
287 return fStore.getDefaultFloat(name);
291 * @see IPreferenceStore#getDefaultInt(String)
293 public int getDefaultInt(String name) {
294 return fStore.getDefaultInt(name);
298 * @see IPreferenceStore#getDefaultLong(String)
300 public long getDefaultLong(String name) {
301 return fStore.getDefaultLong(name);
305 * @see IPreferenceStore#getDefaultString(String)
307 public String getDefaultString(String name) {
308 return fStore.getDefaultString(name);
312 * @see IPreferenceStore#getDouble(String)
314 public double getDouble(String name) {
315 return fStore.getDouble(name);
319 * @see IPreferenceStore#getFloat(String)
321 public float getFloat(String name) {
322 return fStore.getFloat(name);
326 * @see IPreferenceStore#getInt(String)
328 public int getInt(String name) {
329 return fStore.getInt(name);
333 * @see IPreferenceStore#getLong(String)
335 public long getLong(String name) {
336 return fStore.getLong(name);
340 * @see IPreferenceStore#getString(String)
342 public String getString(String name) {
343 return fStore.getString(name);
347 * @see IPreferenceStore#isDefault(String)
349 public boolean isDefault(String name) {
350 return fStore.isDefault(name);
354 * @see IPreferenceStore#needsSaving()
356 public boolean needsSaving() {
357 return fStore.needsSaving();
361 * @see IPreferenceStore#putValue(String, String)
363 public void putValue(String name, String value) {
365 fStore.putValue(name, value);
369 * @see IPreferenceStore#setDefault(String, double)
371 public void setDefault(String name, double value) {
373 fStore.setDefault(name, value);
377 * @see IPreferenceStore#setDefault(String, float)
379 public void setDefault(String name, float value) {
381 fStore.setDefault(name, value);
385 * @see IPreferenceStore#setDefault(String, int)
387 public void setDefault(String name, int value) {
389 fStore.setDefault(name, value);
393 * @see IPreferenceStore#setDefault(String, long)
395 public void setDefault(String name, long value) {
397 fStore.setDefault(name, value);
401 * @see IPreferenceStore#setDefault(String, String)
403 public void setDefault(String name, String value) {
405 fStore.setDefault(name, value);
409 * @see IPreferenceStore#setDefault(String, boolean)
411 public void setDefault(String name, boolean value) {
413 fStore.setDefault(name, value);
417 * @see IPreferenceStore#setToDefault(String)
419 public void setToDefault(String name) {
420 fStore.setToDefault(name);
424 * @see IPreferenceStore#setValue(String, double)
426 public void setValue(String name, double value) {
428 fStore.setValue(name, value);
432 * @see IPreferenceStore#setValue(String, float)
434 public void setValue(String name, float value) {
436 fStore.setValue(name, value);
440 * @see IPreferenceStore#setValue(String, int)
442 public void setValue(String name, int value) {
444 fStore.setValue(name, value);
448 * @see IPreferenceStore#setValue(String, long)
450 public void setValue(String name, long value) {
452 fStore.setValue(name, value);
456 * @see IPreferenceStore#setValue(String, String)
458 public void setValue(String name, String value) {
460 fStore.setValue(name, value);
464 * @see IPreferenceStore#setValue(String, boolean)
466 public void setValue(String name, boolean value) {
468 fStore.setValue(name, value);
472 * The keys to add to the list of overlay keys.
474 * Note: This method must be called before {@link #load()} is called.
480 public void addKeys(OverlayKey[] keys) {
481 Assert.isTrue(!fLoaded);
482 Assert.isNotNull(keys);
484 int overlayKeysLength = fOverlayKeys.length;
485 OverlayKey[] result = new OverlayKey[keys.length + overlayKeysLength];
487 for (int i = 0, length = overlayKeysLength; i < length; i++)
488 result[i] = fOverlayKeys[i];
490 for (int i = 0, length = keys.length; i < length; i++)
491 result[overlayKeysLength + i] = keys[i];
493 fOverlayKeys = result;