2 * Copyright (c) 2002-2004 Widespace, OU 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://solareclipse.sourceforge.net/legal/cpl-v10.html
9 * Igor Malinin - initial contribution
11 * $Id: OverlayPreferenceStore.java,v 1.2 2006-10-21 23:13:54 pombredanne Exp $
14 package net.sourceforge.phpeclipse.ui.preferences;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.preference.PreferenceStore;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
22 * An overlaying preference store.
24 public class OverlayPreferenceStore implements IPreferenceStore {
25 private class PropertyListener implements IPropertyChangeListener {
26 public void propertyChange(PropertyChangeEvent event) {
27 PreferenceDescriptor key = findOverlayKey(event.getProperty());
29 propagateProperty(parent, key, store);
34 IPreferenceStore parent;
36 IPreferenceStore store;
38 private PreferenceDescriptor[] keys;
40 private PropertyListener fPropertyListener;
42 public OverlayPreferenceStore(IPreferenceStore parent,
43 PreferenceDescriptor[] overlayKeys) {
45 this.keys = overlayKeys;
47 store = new PreferenceStore();
50 PreferenceDescriptor findOverlayKey(String key) {
51 for (int i = 0; i < keys.length; i++) {
52 if (keys[i].key.equals(key)) {
60 private boolean covers(String key) {
61 return (findOverlayKey(key) != null);
64 void propagateProperty(IPreferenceStore orgin, PreferenceDescriptor key,
65 IPreferenceStore target) {
66 if (orgin.isDefault(key.key)) {
67 if (!target.isDefault(key.key)) {
68 target.setToDefault(key.key);
74 PreferenceDescriptor.Type d = key.type;
75 if (PreferenceDescriptor.BOOLEAN == d) {
76 boolean originValue = orgin.getBoolean(key.key);
77 boolean targetValue = target.getBoolean(key.key);
78 if (targetValue != originValue) {
79 target.setValue(key.key, originValue);
81 } else if (PreferenceDescriptor.DOUBLE == d) {
82 double originValue = orgin.getDouble(key.key);
83 double targetValue = target.getDouble(key.key);
84 if (targetValue != originValue) {
85 target.setValue(key.key, originValue);
87 } else if (PreferenceDescriptor.FLOAT == d) {
88 float originValue = orgin.getFloat(key.key);
89 float targetValue = target.getFloat(key.key);
90 if (targetValue != originValue) {
91 target.setValue(key.key, originValue);
93 } else if (PreferenceDescriptor.INT == d) {
94 int originValue = orgin.getInt(key.key);
95 int targetValue = target.getInt(key.key);
96 if (targetValue != originValue) {
97 target.setValue(key.key, originValue);
99 } else if (PreferenceDescriptor.LONG == d) {
100 long originValue = orgin.getLong(key.key);
101 long targetValue = target.getLong(key.key);
102 if (targetValue != originValue) {
103 target.setValue(key.key, originValue);
105 } else if (PreferenceDescriptor.STRING == d) {
106 String originValue = orgin.getString(key.key);
107 String targetValue = target.getString(key.key);
108 if (targetValue != null && originValue != null
109 && !targetValue.equals(originValue)) {
110 target.setValue(key.key, originValue);
115 public void propagate() {
116 for (int i = 0; i < keys.length; i++) {
117 propagateProperty(store, keys[i], parent);
121 private void loadProperty(IPreferenceStore orgin, PreferenceDescriptor key,
122 IPreferenceStore target, boolean forceInitialization) {
123 PreferenceDescriptor.Type d = key.type;
124 if (PreferenceDescriptor.BOOLEAN == d) {
125 if (forceInitialization) {
126 target.setValue(key.key, true);
128 target.setValue(key.key, orgin.getBoolean(key.key));
129 target.setDefault(key.key, orgin.getDefaultBoolean(key.key));
130 } else if (PreferenceDescriptor.DOUBLE == d) {
131 if (forceInitialization) {
132 target.setValue(key.key, 1.0D);
134 target.setValue(key.key, orgin.getDouble(key.key));
135 target.setDefault(key.key, orgin.getDefaultDouble(key.key));
136 } else if (PreferenceDescriptor.FLOAT == d) {
137 if (forceInitialization) {
138 target.setValue(key.key, 1.0F);
140 target.setValue(key.key, orgin.getFloat(key.key));
141 target.setDefault(key.key, orgin.getDefaultFloat(key.key));
142 } else if (PreferenceDescriptor.INT == d) {
143 if (forceInitialization) {
144 target.setValue(key.key, 1);
146 target.setValue(key.key, orgin.getInt(key.key));
147 target.setDefault(key.key, orgin.getDefaultInt(key.key));
148 } else if (PreferenceDescriptor.LONG == d) {
149 if (forceInitialization) {
150 target.setValue(key.key, 1L);
152 target.setValue(key.key, orgin.getLong(key.key));
153 target.setDefault(key.key, orgin.getDefaultLong(key.key));
154 } else if (PreferenceDescriptor.STRING == d) {
155 if (forceInitialization) {
156 target.setValue(key.key, "1"); //$NON-NLS-1$
158 target.setValue(key.key, orgin.getString(key.key));
159 target.setDefault(key.key, orgin.getDefaultString(key.key));
164 for (int i = 0; i < keys.length; i++) {
165 loadProperty(parent, keys[i], store, true);
169 public void loadDefaults() {
170 for (int i = 0; i < keys.length; i++) {
171 setToDefault(keys[i].key);
175 public void start() {
176 if (fPropertyListener == null) {
177 fPropertyListener = new PropertyListener();
178 parent.addPropertyChangeListener(fPropertyListener);
183 if (fPropertyListener != null) {
184 parent.removePropertyChangeListener(fPropertyListener);
185 fPropertyListener = null;
190 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
192 public void addPropertyChangeListener(IPropertyChangeListener listener) {
193 store.addPropertyChangeListener(listener);
197 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
199 public void removePropertyChangeListener(IPropertyChangeListener listener) {
200 store.removePropertyChangeListener(listener);
204 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
206 public void firePropertyChangeEvent(String name, Object oldValue,
208 store.firePropertyChangeEvent(name, oldValue, newValue);
212 * @see IPreferenceStore#contains(String)
214 public boolean contains(String name) {
215 return store.contains(name);
219 * @see IPreferenceStore#getBoolean(String)
221 public boolean getBoolean(String name) {
222 return store.getBoolean(name);
226 * @see IPreferenceStore#getDefaultBoolean(String)
228 public boolean getDefaultBoolean(String name) {
229 return store.getDefaultBoolean(name);
233 * @see IPreferenceStore#getDefaultDouble(String)
235 public double getDefaultDouble(String name) {
236 return store.getDefaultDouble(name);
240 * @see IPreferenceStore#getDefaultFloat(String)
242 public float getDefaultFloat(String name) {
243 return store.getDefaultFloat(name);
247 * @see IPreferenceStore#getDefaultInt(String)
249 public int getDefaultInt(String name) {
250 return store.getDefaultInt(name);
254 * @see IPreferenceStore#getDefaultLong(String)
256 public long getDefaultLong(String name) {
257 return store.getDefaultLong(name);
261 * @see IPreferenceStore#getDefaultString(String)
263 public String getDefaultString(String name) {
264 return store.getDefaultString(name);
268 * @see IPreferenceStore#getDouble(String)
270 public double getDouble(String name) {
271 return store.getDouble(name);
275 * @see IPreferenceStore#getFloat(String)
277 public float getFloat(String name) {
278 return store.getFloat(name);
282 * @see IPreferenceStore#getInt(String)
284 public int getInt(String name) {
285 return store.getInt(name);
289 * @see IPreferenceStore#getLong(String)
291 public long getLong(String name) {
292 return store.getLong(name);
296 * @see IPreferenceStore#getString(String)
298 public String getString(String name) {
299 return store.getString(name);
303 * @see IPreferenceStore#isDefault(String)
305 public boolean isDefault(String name) {
306 return store.isDefault(name);
310 * @see IPreferenceStore#needsSaving()
312 public boolean needsSaving() {
313 return store.needsSaving();
317 * @see IPreferenceStore#putValue(String, String)
319 public void putValue(String name, String value) {
321 store.putValue(name, value);
326 * @see IPreferenceStore#setDefault(String, double)
328 public void setDefault(String name, double value) {
330 store.setDefault(name, value);
335 * @see IPreferenceStore#setDefault(String, float)
337 public void setDefault(String name, float value) {
339 store.setDefault(name, value);
344 * @see IPreferenceStore#setDefault(String, int)
346 public void setDefault(String name, int value) {
348 store.setDefault(name, value);
353 * @see IPreferenceStore#setDefault(String, long)
355 public void setDefault(String name, long value) {
357 store.setDefault(name, value);
362 * @see IPreferenceStore#setDefault(String, String)
364 public void setDefault(String name, String value) {
366 store.setDefault(name, value);
371 * @see IPreferenceStore#setDefault(String, boolean)
373 public void setDefault(String name, boolean value) {
375 store.setDefault(name, value);
380 * @see IPreferenceStore#setToDefault(String)
382 public void setToDefault(String name) {
383 store.setToDefault(name);
387 * @see IPreferenceStore#setValue(String, double)
389 public void setValue(String name, double value) {
391 store.setValue(name, value);
396 * @see IPreferenceStore#setValue(String, float)
398 public void setValue(String name, float value) {
400 store.setValue(name, value);
405 * @see IPreferenceStore#setValue(String, int)
407 public void setValue(String name, int value) {
409 store.setValue(name, value);
414 * @see IPreferenceStore#setValue(String, long)
416 public void setValue(String name, long value) {
418 store.setValue(name, value);
423 * @see IPreferenceStore#setValue(String, String)
425 public void setValue(String name, String value) {
427 store.setValue(name, value);
432 * @see IPreferenceStore#setValue(String, boolean)
434 public void setValue(String name, boolean value) {
436 store.setValue(name, value);