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.1 2004-09-02 18:26:30 jsurfer 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;
23 * An overlaying preference store.
25 public class OverlayPreferenceStore implements IPreferenceStore {
26 private class PropertyListener implements IPropertyChangeListener {
27 public void propertyChange(PropertyChangeEvent event) {
28 PreferenceDescriptor key = findOverlayKey(event.getProperty());
30 propagateProperty(parent, key, store);
35 IPreferenceStore parent;
36 IPreferenceStore store;
38 private PreferenceDescriptor[] keys;
40 private PropertyListener fPropertyListener;
42 public OverlayPreferenceStore(
43 IPreferenceStore parent, PreferenceDescriptor[] overlayKeys
46 this.keys = overlayKeys;
48 store = new PreferenceStore();
51 PreferenceDescriptor findOverlayKey(String key) {
52 for (int i = 0; i < keys.length; i++) {
53 if (keys[i].key.equals(key)) {
61 private boolean covers(String key) {
62 return (findOverlayKey(key) != null);
65 void propagateProperty(
66 IPreferenceStore orgin, PreferenceDescriptor key,
67 IPreferenceStore target
69 if (orgin.isDefault(key.key)) {
70 if (!target.isDefault(key.key)) {
71 target.setToDefault(key.key);
77 PreferenceDescriptor.Type d = key.type;
78 if (PreferenceDescriptor.BOOLEAN == d) {
79 boolean originValue = orgin.getBoolean(key.key);
80 boolean targetValue = target.getBoolean(key.key);
81 if (targetValue != originValue) {
82 target.setValue(key.key, originValue);
84 } else if (PreferenceDescriptor.DOUBLE == d) {
85 double originValue = orgin.getDouble(key.key);
86 double targetValue = target.getDouble(key.key);
87 if (targetValue != originValue) {
88 target.setValue(key.key, originValue);
90 } else if (PreferenceDescriptor.FLOAT == d) {
91 float originValue = orgin.getFloat(key.key);
92 float targetValue = target.getFloat(key.key);
93 if (targetValue != originValue) {
94 target.setValue(key.key, originValue);
96 } else if (PreferenceDescriptor.INT == d) {
97 int originValue = orgin.getInt(key.key);
98 int targetValue = target.getInt(key.key);
99 if (targetValue != originValue) {
100 target.setValue(key.key, originValue);
102 } else if (PreferenceDescriptor.LONG == d) {
103 long originValue = orgin.getLong(key.key);
104 long targetValue = target.getLong(key.key);
105 if (targetValue != originValue) {
106 target.setValue(key.key, originValue);
108 } else if (PreferenceDescriptor.STRING == d) {
109 String originValue = orgin.getString(key.key);
110 String targetValue = target.getString(key.key);
111 if (targetValue != null && originValue != null
112 && !targetValue.equals(originValue)) {
113 target.setValue(key.key, originValue);
118 public void propagate() {
119 for (int i = 0; i < keys.length; i++) {
120 propagateProperty(store, keys[i], parent);
124 private void loadProperty(
125 IPreferenceStore orgin, PreferenceDescriptor key,
126 IPreferenceStore target, boolean forceInitialization
128 PreferenceDescriptor.Type d = key.type;
129 if (PreferenceDescriptor.BOOLEAN == d) {
130 if (forceInitialization) {
131 target.setValue(key.key, true);
133 target.setValue(key.key, orgin.getBoolean(key.key));
134 target.setDefault(key.key, orgin.getDefaultBoolean(key.key));
135 } else if (PreferenceDescriptor.DOUBLE == d) {
136 if (forceInitialization) {
137 target.setValue(key.key, 1.0D);
139 target.setValue(key.key, orgin.getDouble(key.key));
140 target.setDefault(key.key, orgin.getDefaultDouble(key.key));
141 } else if (PreferenceDescriptor.FLOAT == d) {
142 if (forceInitialization) {
143 target.setValue(key.key, 1.0F);
145 target.setValue(key.key, orgin.getFloat(key.key));
146 target.setDefault(key.key, orgin.getDefaultFloat(key.key));
147 } else if (PreferenceDescriptor.INT == d) {
148 if (forceInitialization) {
149 target.setValue(key.key, 1);
151 target.setValue(key.key, orgin.getInt(key.key));
152 target.setDefault(key.key, orgin.getDefaultInt(key.key));
153 } else if (PreferenceDescriptor.LONG == d) {
154 if (forceInitialization) {
155 target.setValue(key.key, 1L);
157 target.setValue(key.key, orgin.getLong(key.key));
158 target.setDefault(key.key, orgin.getDefaultLong(key.key));
159 } else if (PreferenceDescriptor.STRING == d) {
160 if (forceInitialization) {
161 target.setValue(key.key, "1"); //$NON-NLS-1$
163 target.setValue(key.key, orgin.getString(key.key));
164 target.setDefault(key.key, orgin.getDefaultString(key.key));
169 for (int i = 0; i < keys.length; i++) {
170 loadProperty(parent, keys[i], store, true);
174 public void loadDefaults() {
175 for (int i = 0; i < keys.length; i++) {
176 setToDefault(keys[i].key);
180 public void start() {
181 if (fPropertyListener == null) {
182 fPropertyListener = new PropertyListener();
183 parent.addPropertyChangeListener(fPropertyListener);
188 if (fPropertyListener != null) {
189 parent.removePropertyChangeListener(fPropertyListener);
190 fPropertyListener = null;
195 * @see IPreferenceStore#addPropertyChangeListener(IPropertyChangeListener)
197 public void addPropertyChangeListener(IPropertyChangeListener listener) {
198 store.addPropertyChangeListener(listener);
202 * @see IPreferenceStore#removePropertyChangeListener(IPropertyChangeListener)
204 public void removePropertyChangeListener(IPropertyChangeListener listener) {
205 store.removePropertyChangeListener(listener);
209 * @see IPreferenceStore#firePropertyChangeEvent(String, Object, Object)
211 public void firePropertyChangeEvent(
212 String name, Object oldValue, Object newValue
214 store.firePropertyChangeEvent(name, oldValue, newValue);
218 * @see IPreferenceStore#contains(String)
220 public boolean contains(String name) {
221 return store.contains(name);
225 * @see IPreferenceStore#getBoolean(String)
227 public boolean getBoolean(String name) {
228 return store.getBoolean(name);
232 * @see IPreferenceStore#getDefaultBoolean(String)
234 public boolean getDefaultBoolean(String name) {
235 return store.getDefaultBoolean(name);
239 * @see IPreferenceStore#getDefaultDouble(String)
241 public double getDefaultDouble(String name) {
242 return store.getDefaultDouble(name);
246 * @see IPreferenceStore#getDefaultFloat(String)
248 public float getDefaultFloat(String name) {
249 return store.getDefaultFloat(name);
253 * @see IPreferenceStore#getDefaultInt(String)
255 public int getDefaultInt(String name) {
256 return store.getDefaultInt(name);
260 * @see IPreferenceStore#getDefaultLong(String)
262 public long getDefaultLong(String name) {
263 return store.getDefaultLong(name);
267 * @see IPreferenceStore#getDefaultString(String)
269 public String getDefaultString(String name) {
270 return store.getDefaultString(name);
274 * @see IPreferenceStore#getDouble(String)
276 public double getDouble(String name) {
277 return store.getDouble(name);
281 * @see IPreferenceStore#getFloat(String)
283 public float getFloat(String name) {
284 return store.getFloat(name);
288 * @see IPreferenceStore#getInt(String)
290 public int getInt(String name) {
291 return store.getInt(name);
295 * @see IPreferenceStore#getLong(String)
297 public long getLong(String name) {
298 return store.getLong(name);
302 * @see IPreferenceStore#getString(String)
304 public String getString(String name) {
305 return store.getString(name);
309 * @see IPreferenceStore#isDefault(String)
311 public boolean isDefault(String name) {
312 return store.isDefault(name);
316 * @see IPreferenceStore#needsSaving()
318 public boolean needsSaving() {
319 return store.needsSaving();
323 * @see IPreferenceStore#putValue(String, String)
325 public void putValue(String name, String value) {
327 store.putValue(name, value);
332 * @see IPreferenceStore#setDefault(String, double)
334 public void setDefault(String name, double value) {
336 store.setDefault(name, value);
341 * @see IPreferenceStore#setDefault(String, float)
343 public void setDefault(String name, float value) {
345 store.setDefault(name, value);
350 * @see IPreferenceStore#setDefault(String, int)
352 public void setDefault(String name, int value) {
354 store.setDefault(name, value);
359 * @see IPreferenceStore#setDefault(String, long)
361 public void setDefault(String name, long value) {
363 store.setDefault(name, value);
368 * @see IPreferenceStore#setDefault(String, String)
370 public void setDefault(String name, String value) {
372 store.setDefault(name, value);
377 * @see IPreferenceStore#setDefault(String, boolean)
379 public void setDefault(String name, boolean value) {
381 store.setDefault(name, value);
386 * @see IPreferenceStore#setToDefault(String)
388 public void setToDefault(String name) {
389 store.setToDefault(name);
393 * @see IPreferenceStore#setValue(String, double)
395 public void setValue(String name, double value) {
397 store.setValue(name, value);
402 * @see IPreferenceStore#setValue(String, float)
404 public void setValue(String name, float value) {
406 store.setValue(name, value);
411 * @see IPreferenceStore#setValue(String, int)
413 public void setValue(String name, int value) {
415 store.setValue(name, value);
420 * @see IPreferenceStore#setValue(String, long)
422 public void setValue(String name, long value) {
424 store.setValue(name, value);
429 * @see IPreferenceStore#setValue(String, String)
431 public void setValue(String name, String value) {
433 store.setValue(name, value);
438 * @see IPreferenceStore#setValue(String, boolean)
440 public void setValue(String name, boolean value) {
442 store.setValue(name, value);