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;
16 import org.eclipse.jface.text.Assert;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
21 * An overlaying preference store.
23 public class OverlayPreferenceStore implements IPreferenceStore {
25 public static final class TypeDescriptor {
26 private TypeDescriptor() {
30 public static final TypeDescriptor BOOLEAN = new TypeDescriptor();
32 public static final TypeDescriptor DOUBLE = new TypeDescriptor();
34 public static final TypeDescriptor FLOAT = new TypeDescriptor();
36 public static final TypeDescriptor INT = new TypeDescriptor();
38 public static final TypeDescriptor LONG = new TypeDescriptor();
40 public static final TypeDescriptor STRING = new TypeDescriptor();
42 public static class OverlayKey {
44 TypeDescriptor fDescriptor;
48 public OverlayKey(TypeDescriptor descriptor, String key) {
49 fDescriptor = descriptor;
54 private class PropertyListener implements IPropertyChangeListener {
57 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
59 public void propertyChange(PropertyChangeEvent event) {
60 OverlayKey key = findOverlayKey(event.getProperty());
62 propagateProperty(fParent, key, fStore);
66 private IPreferenceStore fParent;
68 private IPreferenceStore fStore;
70 private OverlayKey[] fOverlayKeys;
72 private PropertyListener fPropertyListener;
74 private boolean fLoaded;
76 public OverlayPreferenceStore(IPreferenceStore parent,
77 OverlayKey[] overlayKeys) {
79 fOverlayKeys = overlayKeys;
80 fStore = new PreferenceStore();
83 private OverlayKey findOverlayKey(String key) {
84 for (int i = 0; i < fOverlayKeys.length; i++) {
85 if (fOverlayKeys[i].fKey.equals(key))
86 return fOverlayKeys[i];
91 private boolean covers(String key) {
92 return (findOverlayKey(key) != null);
95 private void propagateProperty(IPreferenceStore orgin, OverlayKey key,
96 IPreferenceStore target) {
98 if (orgin.isDefault(key.fKey)) {
99 if (!target.isDefault(key.fKey))
100 target.setToDefault(key.fKey);
104 TypeDescriptor d = key.fDescriptor;
107 boolean originValue = orgin.getBoolean(key.fKey);
108 boolean targetValue = target.getBoolean(key.fKey);
109 if (targetValue != originValue)
110 target.setValue(key.fKey, originValue);
112 } else if (DOUBLE == d) {
114 double originValue = orgin.getDouble(key.fKey);
115 double targetValue = target.getDouble(key.fKey);
116 if (targetValue != originValue)
117 target.setValue(key.fKey, originValue);
119 } else if (FLOAT == d) {
121 float originValue = orgin.getFloat(key.fKey);
122 float targetValue = target.getFloat(key.fKey);
123 if (targetValue != originValue)
124 target.setValue(key.fKey, originValue);
126 } else if (INT == d) {
128 int originValue = orgin.getInt(key.fKey);
129 int targetValue = target.getInt(key.fKey);
130 if (targetValue != originValue)
131 target.setValue(key.fKey, originValue);
133 } else if (LONG == d) {
135 long originValue = orgin.getLong(key.fKey);
136 long targetValue = target.getLong(key.fKey);
137 if (targetValue != originValue)
138 target.setValue(key.fKey, originValue);
140 } else if (STRING == d) {
142 String originValue = orgin.getString(key.fKey);
143 String targetValue = target.getString(key.fKey);
144 if (targetValue != null && originValue != null
145 && !targetValue.equals(originValue))
146 target.setValue(key.fKey, originValue);
151 public void propagate() {
152 for (int i = 0; i < fOverlayKeys.length; i++)
153 propagateProperty(fStore, fOverlayKeys[i], fParent);
156 private void loadProperty(IPreferenceStore orgin, OverlayKey key,
157 IPreferenceStore target, boolean forceInitialization) {
158 TypeDescriptor d = key.fDescriptor;
161 if (forceInitialization)
162 target.setValue(key.fKey, true);
163 target.setValue(key.fKey, orgin.getBoolean(key.fKey));
164 target.setDefault(key.fKey, orgin.getDefaultBoolean(key.fKey));
166 } else if (DOUBLE == d) {
168 if (forceInitialization)
169 target.setValue(key.fKey, 1.0D);
170 target.setValue(key.fKey, orgin.getDouble(key.fKey));
171 target.setDefault(key.fKey, orgin.getDefaultDouble(key.fKey));
173 } else if (FLOAT == d) {
175 if (forceInitialization)
176 target.setValue(key.fKey, 1.0F);
177 target.setValue(key.fKey, orgin.getFloat(key.fKey));
178 target.setDefault(key.fKey, orgin.getDefaultFloat(key.fKey));
180 } else if (INT == d) {
182 if (forceInitialization)
183 target.setValue(key.fKey, 1);
184 target.setValue(key.fKey, orgin.getInt(key.fKey));
185 target.setDefault(key.fKey, orgin.getDefaultInt(key.fKey));
187 } else if (LONG == d) {
189 if (forceInitialization)
190 target.setValue(key.fKey, 1L);
191 target.setValue(key.fKey, orgin.getLong(key.fKey));
192 target.setDefault(key.fKey, orgin.getDefaultLong(key.fKey));
194 } else if (STRING == d) {
196 if (forceInitialization)
197 target.setValue(key.fKey, "1"); //$NON-NLS-1$
198 target.setValue(key.fKey, orgin.getString(key.fKey));
199 target.setDefault(key.fKey, orgin.getDefaultString(key.fKey));
205 for (int i = 0; i < fOverlayKeys.length; i++)
206 loadProperty(fParent, fOverlayKeys[i], fStore, true);
212 public void loadDefaults() {
213 for (int i = 0; i < fOverlayKeys.length; i++)
214 setToDefault(fOverlayKeys[i].fKey);
217 public void start() {
218 if (fPropertyListener == null) {
219 fPropertyListener = new PropertyListener();
220 fParent.addPropertyChangeListener(fPropertyListener);
225 if (fPropertyListener != null) {
226 fParent.removePropertyChangeListener(fPropertyListener);
227 fPropertyListener = null;
232 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
234 public void addPropertyChangeListener(IPropertyChangeListener listener) {
235 fStore.addPropertyChangeListener(listener);
239 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
241 public void removePropertyChangeListener(IPropertyChangeListener listener) {
242 fStore.removePropertyChangeListener(listener);
246 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
248 public void firePropertyChangeEvent(String name, Object oldValue,
250 fStore.firePropertyChangeEvent(name, oldValue, newValue);
254 * @see IPreferenceStore#contains(String)
256 public boolean contains(String name) {
257 return fStore.contains(name);
261 * @see IPreferenceStore#getBoolean(String)
263 public boolean getBoolean(String name) {
264 return fStore.getBoolean(name);
268 * @see IPreferenceStore#getDefaultBoolean(String)
270 public boolean getDefaultBoolean(String name) {
271 return fStore.getDefaultBoolean(name);
275 * @see IPreferenceStore#getDefaultDouble(String)
277 public double getDefaultDouble(String name) {
278 return fStore.getDefaultDouble(name);
282 * @see IPreferenceStore#getDefaultFloat(String)
284 public float getDefaultFloat(String name) {
285 return fStore.getDefaultFloat(name);
289 * @see IPreferenceStore#getDefaultInt(String)
291 public int getDefaultInt(String name) {
292 return fStore.getDefaultInt(name);
296 * @see IPreferenceStore#getDefaultLong(String)
298 public long getDefaultLong(String name) {
299 return fStore.getDefaultLong(name);
303 * @see IPreferenceStore#getDefaultString(String)
305 public String getDefaultString(String name) {
306 return fStore.getDefaultString(name);
310 * @see IPreferenceStore#getDouble(String)
312 public double getDouble(String name) {
313 return fStore.getDouble(name);
317 * @see IPreferenceStore#getFloat(String)
319 public float getFloat(String name) {
320 return fStore.getFloat(name);
324 * @see IPreferenceStore#getInt(String)
326 public int getInt(String name) {
327 return fStore.getInt(name);
331 * @see IPreferenceStore#getLong(String)
333 public long getLong(String name) {
334 return fStore.getLong(name);
338 * @see IPreferenceStore#getString(String)
340 public String getString(String name) {
341 return fStore.getString(name);
345 * @see IPreferenceStore#isDefault(String)
347 public boolean isDefault(String name) {
348 return fStore.isDefault(name);
352 * @see IPreferenceStore#needsSaving()
354 public boolean needsSaving() {
355 return fStore.needsSaving();
359 * @see IPreferenceStore#putValue(String, String)
361 public void putValue(String name, String value) {
363 fStore.putValue(name, value);
367 * @see IPreferenceStore#setDefault(String, double)
369 public void setDefault(String name, double value) {
371 fStore.setDefault(name, value);
375 * @see IPreferenceStore#setDefault(String, float)
377 public void setDefault(String name, float value) {
379 fStore.setDefault(name, value);
383 * @see IPreferenceStore#setDefault(String, int)
385 public void setDefault(String name, int value) {
387 fStore.setDefault(name, value);
391 * @see IPreferenceStore#setDefault(String, long)
393 public void setDefault(String name, long value) {
395 fStore.setDefault(name, value);
399 * @see IPreferenceStore#setDefault(String, String)
401 public void setDefault(String name, String value) {
403 fStore.setDefault(name, value);
407 * @see IPreferenceStore#setDefault(String, boolean)
409 public void setDefault(String name, boolean value) {
411 fStore.setDefault(name, value);
415 * @see IPreferenceStore#setToDefault(String)
417 public void setToDefault(String name) {
418 fStore.setToDefault(name);
422 * @see IPreferenceStore#setValue(String, double)
424 public void setValue(String name, double value) {
426 fStore.setValue(name, value);
430 * @see IPreferenceStore#setValue(String, float)
432 public void setValue(String name, float value) {
434 fStore.setValue(name, value);
438 * @see IPreferenceStore#setValue(String, int)
440 public void setValue(String name, int value) {
442 fStore.setValue(name, value);
446 * @see IPreferenceStore#setValue(String, long)
448 public void setValue(String name, long value) {
450 fStore.setValue(name, value);
454 * @see IPreferenceStore#setValue(String, String)
456 public void setValue(String name, String value) {
458 fStore.setValue(name, value);
462 * @see IPreferenceStore#setValue(String, boolean)
464 public void setValue(String name, boolean value) {
466 fStore.setValue(name, value);
470 * The keys to add to the list of overlay keys.
472 * Note: This method must be called before {@link #load()} is called.
478 public void addKeys(OverlayKey[] keys) {
479 Assert.isTrue(!fLoaded);
480 Assert.isNotNull(keys);
482 int overlayKeysLength = fOverlayKeys.length;
483 OverlayKey[] result = new OverlayKey[keys.length + overlayKeysLength];
485 for (int i = 0, length = overlayKeysLength; i < length; i++)
486 result[i] = fOverlayKeys[i];
488 for (int i = 0, length = keys.length; i < length; i++)
489 result[overlayKeysLength + i] = keys[i];
491 fOverlayKeys = result;