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.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.jface.text.Assert;
22 * An overlaying preference store.
24 public class OverlayPreferenceStore implements IPreferenceStore {
27 public static final class TypeDescriptor {
28 private TypeDescriptor() {
32 public static final TypeDescriptor BOOLEAN= new TypeDescriptor();
33 public static final TypeDescriptor DOUBLE= new TypeDescriptor();
34 public static final TypeDescriptor FLOAT= new TypeDescriptor();
35 public static final TypeDescriptor INT= new TypeDescriptor();
36 public static final TypeDescriptor LONG= new TypeDescriptor();
37 public static final TypeDescriptor STRING= new TypeDescriptor();
39 public static class OverlayKey {
41 TypeDescriptor fDescriptor;
44 public OverlayKey(TypeDescriptor descriptor, String key) {
45 fDescriptor= descriptor;
50 private class PropertyListener implements IPropertyChangeListener {
53 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
55 public void propertyChange(PropertyChangeEvent event) {
56 OverlayKey key= findOverlayKey(event.getProperty());
58 propagateProperty(fParent, key, fStore);
63 private IPreferenceStore fParent;
64 private IPreferenceStore fStore;
65 private OverlayKey[] fOverlayKeys;
67 private PropertyListener fPropertyListener;
68 private boolean fLoaded;
71 public OverlayPreferenceStore(IPreferenceStore parent, OverlayKey[] overlayKeys) {
73 fOverlayKeys= overlayKeys;
74 fStore= new PreferenceStore();
77 private OverlayKey findOverlayKey(String key) {
78 for (int i= 0; i < fOverlayKeys.length; i++) {
79 if (fOverlayKeys[i].fKey.equals(key))
80 return fOverlayKeys[i];
85 private boolean covers(String key) {
86 return (findOverlayKey(key) != null);
89 private void propagateProperty(IPreferenceStore orgin, OverlayKey key, IPreferenceStore target) {
91 if (orgin.isDefault(key.fKey)) {
92 if (!target.isDefault(key.fKey))
93 target.setToDefault(key.fKey);
97 TypeDescriptor d= key.fDescriptor;
100 boolean originValue= orgin.getBoolean(key.fKey);
101 boolean targetValue= target.getBoolean(key.fKey);
102 if (targetValue != originValue)
103 target.setValue(key.fKey, originValue);
105 } else if (DOUBLE == d) {
107 double originValue= orgin.getDouble(key.fKey);
108 double targetValue= target.getDouble(key.fKey);
109 if (targetValue != originValue)
110 target.setValue(key.fKey, originValue);
112 } else if (FLOAT == d) {
114 float originValue= orgin.getFloat(key.fKey);
115 float targetValue= target.getFloat(key.fKey);
116 if (targetValue != originValue)
117 target.setValue(key.fKey, originValue);
119 } else if (INT == d) {
121 int originValue= orgin.getInt(key.fKey);
122 int targetValue= target.getInt(key.fKey);
123 if (targetValue != originValue)
124 target.setValue(key.fKey, originValue);
126 } else if (LONG == d) {
128 long originValue= orgin.getLong(key.fKey);
129 long targetValue= target.getLong(key.fKey);
130 if (targetValue != originValue)
131 target.setValue(key.fKey, originValue);
133 } else if (STRING == d) {
135 String originValue= orgin.getString(key.fKey);
136 String targetValue= target.getString(key.fKey);
137 if (targetValue != null && originValue != null && !targetValue.equals(originValue))
138 target.setValue(key.fKey, originValue);
143 public void propagate() {
144 for (int i= 0; i < fOverlayKeys.length; i++)
145 propagateProperty(fStore, fOverlayKeys[i], fParent);
148 private void loadProperty(IPreferenceStore orgin, OverlayKey key, 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);
203 public void loadDefaults() {
204 for (int i= 0; i < fOverlayKeys.length; i++)
205 setToDefault(fOverlayKeys[i].fKey);
208 public void start() {
209 if (fPropertyListener == null) {
210 fPropertyListener= new PropertyListener();
211 fParent.addPropertyChangeListener(fPropertyListener);
216 if (fPropertyListener != null) {
217 fParent.removePropertyChangeListener(fPropertyListener);
218 fPropertyListener= null;
223 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
225 public void addPropertyChangeListener(IPropertyChangeListener listener) {
226 fStore.addPropertyChangeListener(listener);
230 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
232 public void removePropertyChangeListener(IPropertyChangeListener listener) {
233 fStore.removePropertyChangeListener(listener);
237 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
239 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) {
240 fStore.firePropertyChangeEvent(name, oldValue, newValue);
244 * @see IPreferenceStore#contains(String)
246 public boolean contains(String name) {
247 return fStore.contains(name);
251 * @see IPreferenceStore#getBoolean(String)
253 public boolean getBoolean(String name) {
254 return fStore.getBoolean(name);
258 * @see IPreferenceStore#getDefaultBoolean(String)
260 public boolean getDefaultBoolean(String name) {
261 return fStore.getDefaultBoolean(name);
265 * @see IPreferenceStore#getDefaultDouble(String)
267 public double getDefaultDouble(String name) {
268 return fStore.getDefaultDouble(name);
272 * @see IPreferenceStore#getDefaultFloat(String)
274 public float getDefaultFloat(String name) {
275 return fStore.getDefaultFloat(name);
279 * @see IPreferenceStore#getDefaultInt(String)
281 public int getDefaultInt(String name) {
282 return fStore.getDefaultInt(name);
286 * @see IPreferenceStore#getDefaultLong(String)
288 public long getDefaultLong(String name) {
289 return fStore.getDefaultLong(name);
293 * @see IPreferenceStore#getDefaultString(String)
295 public String getDefaultString(String name) {
296 return fStore.getDefaultString(name);
300 * @see IPreferenceStore#getDouble(String)
302 public double getDouble(String name) {
303 return fStore.getDouble(name);
307 * @see IPreferenceStore#getFloat(String)
309 public float getFloat(String name) {
310 return fStore.getFloat(name);
314 * @see IPreferenceStore#getInt(String)
316 public int getInt(String name) {
317 return fStore.getInt(name);
321 * @see IPreferenceStore#getLong(String)
323 public long getLong(String name) {
324 return fStore.getLong(name);
328 * @see IPreferenceStore#getString(String)
330 public String getString(String name) {
331 return fStore.getString(name);
335 * @see IPreferenceStore#isDefault(String)
337 public boolean isDefault(String name) {
338 return fStore.isDefault(name);
342 * @see IPreferenceStore#needsSaving()
344 public boolean needsSaving() {
345 return fStore.needsSaving();
349 * @see IPreferenceStore#putValue(String, String)
351 public void putValue(String name, String value) {
353 fStore.putValue(name, value);
357 * @see IPreferenceStore#setDefault(String, double)
359 public void setDefault(String name, double value) {
361 fStore.setDefault(name, value);
365 * @see IPreferenceStore#setDefault(String, float)
367 public void setDefault(String name, float value) {
369 fStore.setDefault(name, value);
373 * @see IPreferenceStore#setDefault(String, int)
375 public void setDefault(String name, int value) {
377 fStore.setDefault(name, value);
381 * @see IPreferenceStore#setDefault(String, long)
383 public void setDefault(String name, long value) {
385 fStore.setDefault(name, value);
389 * @see IPreferenceStore#setDefault(String, String)
391 public void setDefault(String name, String value) {
393 fStore.setDefault(name, value);
397 * @see IPreferenceStore#setDefault(String, boolean)
399 public void setDefault(String name, boolean value) {
401 fStore.setDefault(name, value);
405 * @see IPreferenceStore#setToDefault(String)
407 public void setToDefault(String name) {
408 fStore.setToDefault(name);
412 * @see IPreferenceStore#setValue(String, double)
414 public void setValue(String name, double value) {
416 fStore.setValue(name, value);
420 * @see IPreferenceStore#setValue(String, float)
422 public void setValue(String name, float value) {
424 fStore.setValue(name, value);
428 * @see IPreferenceStore#setValue(String, int)
430 public void setValue(String name, int value) {
432 fStore.setValue(name, value);
436 * @see IPreferenceStore#setValue(String, long)
438 public void setValue(String name, long value) {
440 fStore.setValue(name, value);
444 * @see IPreferenceStore#setValue(String, String)
446 public void setValue(String name, String value) {
448 fStore.setValue(name, value);
452 * @see IPreferenceStore#setValue(String, boolean)
454 public void setValue(String name, boolean value) {
456 fStore.setValue(name, value);
460 * The keys to add to the list of overlay keys.
462 * Note: This method must be called before {@link #load()} is called.
468 public void addKeys(OverlayKey[] keys) {
469 Assert.isTrue(!fLoaded);
470 Assert.isNotNull(keys);
472 int overlayKeysLength= fOverlayKeys.length;
473 OverlayKey[] result= new OverlayKey[keys.length + overlayKeysLength];
475 for (int i= 0, length= overlayKeysLength; i < length; i++)
476 result[i]= fOverlayKeys[i];
478 for (int i= 0, length= keys.length; i < length; i++)
479 result[overlayKeysLength + i]= keys[i];
481 fOverlayKeys= result;