Fix nasty bug #706. See trac.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / PreferencesAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpdt.internal.ui.text;
13
14 import org.eclipse.core.runtime.Preferences;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.ListenerList;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19
20 /**
21  * Adapts {@link org.eclipse.core.runtime.Preferences} to
22  * {@link org.eclipse.jface.preference.IPreferenceStore}
23  * 
24  * @since 3.0
25  */
26 public class PreferencesAdapter implements IPreferenceStore {
27
28         /**
29          * Property change listener. Listens for events of type
30          * {@link org.eclipse.core.runtime.Preferences.PropertyChangeEvent} and
31          * fires a {@link org.eclipse.jface.util.PropertyChangeEvent} on the adapter
32          * with arguments from the received event.
33          */
34         private class PropertyChangeListener implements
35                         Preferences.IPropertyChangeListener {
36
37                 /*
38                  * @see org.eclipse.core.runtime.Preferences.IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
39                  */
40                 public void propertyChange(Preferences.PropertyChangeEvent event) {
41                         firePropertyChangeEvent(event.getProperty(), event.getOldValue(),
42                                         event.getNewValue());
43                 }
44         }
45
46         /** Listeners on the adapter */
47         private ListenerList fListeners = new ListenerList();
48
49         /** Listener on the adapted Preferences */
50         private PropertyChangeListener fListener = new PropertyChangeListener();
51
52         /** Adapted Preferences */
53         private Preferences fPreferences;
54
55         /** True iff no events should be forwarded */
56         private boolean fSilent;
57
58         /**
59          * Initialize with empty Preferences.
60          */
61         public PreferencesAdapter() {
62                 this(new Preferences());
63         }
64
65         /**
66          * Initialize with the given Preferences.
67          * 
68          * @param preferences
69          *            The preferences to wrap.
70          */
71         public PreferencesAdapter(Preferences preferences) {
72                 fPreferences = preferences;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         public void addPropertyChangeListener(IPropertyChangeListener listener) {
79                 if (fListeners.size() == 0)
80                         fPreferences.addPropertyChangeListener(fListener);
81                 fListeners.add(listener);
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         public void removePropertyChangeListener(IPropertyChangeListener listener) {
88                 fListeners.remove(listener);
89                 if (fListeners.size() == 0)
90                         fPreferences.removePropertyChangeListener(fListener);
91         }
92
93         /**
94          * {@inheritDoc}
95          */
96         public boolean contains(String name) {
97                 return fPreferences.contains(name);
98         }
99
100         /**
101          * {@inheritDoc}
102          */
103         public void firePropertyChangeEvent(String name, Object oldValue,
104                         Object newValue) {
105                 if (!fSilent) {
106                         PropertyChangeEvent event = new PropertyChangeEvent(this, name,
107                                         oldValue, newValue);
108                         Object[] listeners = fListeners.getListeners();
109                         for (int i = 0; i < listeners.length; i++)
110                                 ((IPropertyChangeListener) listeners[i]).propertyChange(event);
111                 }
112         }
113
114         /**
115          * {@inheritDoc}
116          */
117         public boolean getBoolean(String name) {
118                 return fPreferences.getBoolean(name);
119         }
120
121         /**
122          * {@inheritDoc}
123          */
124         public boolean getDefaultBoolean(String name) {
125                 return fPreferences.getDefaultBoolean(name);
126         }
127
128         /**
129          * {@inheritDoc}
130          */
131         public double getDefaultDouble(String name) {
132                 return fPreferences.getDefaultDouble(name);
133         }
134
135         /**
136          * {@inheritDoc}
137          */
138         public float getDefaultFloat(String name) {
139                 return fPreferences.getDefaultFloat(name);
140         }
141
142         /**
143          * {@inheritDoc}
144          */
145         public int getDefaultInt(String name) {
146                 return fPreferences.getDefaultInt(name);
147         }
148
149         /**
150          * {@inheritDoc}
151          */
152         public long getDefaultLong(String name) {
153                 return fPreferences.getDefaultLong(name);
154         }
155
156         /**
157          * {@inheritDoc}
158          */
159         public String getDefaultString(String name) {
160                 return fPreferences.getDefaultString(name);
161         }
162
163         /**
164          * {@inheritDoc}
165          */
166         public double getDouble(String name) {
167                 return fPreferences.getDouble(name);
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         public float getFloat(String name) {
174                 return fPreferences.getFloat(name);
175         }
176
177         /**
178          * {@inheritDoc}
179          */
180         public int getInt(String name) {
181                 return fPreferences.getInt(name);
182         }
183
184         /**
185          * {@inheritDoc}
186          */
187         public long getLong(String name) {
188                 return fPreferences.getLong(name);
189         }
190
191         /**
192          * {@inheritDoc}
193          */
194         public String getString(String name) {
195                 return fPreferences.getString(name);
196         }
197
198         /**
199          * {@inheritDoc}
200          */
201         public boolean isDefault(String name) {
202                 return fPreferences.isDefault(name);
203         }
204
205         /**
206          * {@inheritDoc}
207          */
208         public boolean needsSaving() {
209                 return fPreferences.needsSaving();
210         }
211
212         /**
213          * {@inheritDoc}
214          */
215         public void putValue(String name, String value) {
216                 try {
217                         fSilent = true;
218                         fPreferences.setValue(name, value);
219                 } finally {
220                         fSilent = false;
221                 }
222         }
223
224         /**
225          * {@inheritDoc}
226          */
227         public void setDefault(String name, double value) {
228                 fPreferences.setDefault(name, value);
229         }
230
231         /**
232          * {@inheritDoc}
233          */
234         public void setDefault(String name, float value) {
235                 fPreferences.setDefault(name, value);
236         }
237
238         /**
239          * {@inheritDoc}
240          */
241         public void setDefault(String name, int value) {
242                 fPreferences.setDefault(name, value);
243         }
244
245         /**
246          * {@inheritDoc}
247          */
248         public void setDefault(String name, long value) {
249                 fPreferences.setDefault(name, value);
250         }
251
252         /**
253          * {@inheritDoc}
254          */
255         public void setDefault(String name, String defaultObject) {
256                 fPreferences.setDefault(name, defaultObject);
257         }
258
259         /**
260          * {@inheritDoc}
261          */
262         public void setDefault(String name, boolean value) {
263                 fPreferences.setDefault(name, value);
264         }
265
266         /**
267          * {@inheritDoc}
268          */
269         public void setToDefault(String name) {
270                 fPreferences.setToDefault(name);
271         }
272
273         /**
274          * {@inheritDoc}
275          */
276         public void setValue(String name, double value) {
277                 fPreferences.setValue(name, value);
278         }
279
280         /**
281          * {@inheritDoc}
282          */
283         public void setValue(String name, float value) {
284                 fPreferences.setValue(name, value);
285         }
286
287         /**
288          * {@inheritDoc}
289          */
290         public void setValue(String name, int value) {
291                 fPreferences.setValue(name, value);
292         }
293
294         /**
295          * {@inheritDoc}
296          */
297         public void setValue(String name, long value) {
298                 fPreferences.setValue(name, value);
299         }
300
301         /**
302          * {@inheritDoc}
303          */
304         public void setValue(String name, String value) {
305                 fPreferences.setValue(name, value);
306         }
307
308         /**
309          * {@inheritDoc}
310          */
311         public void setValue(String name, boolean value) {
312                 fPreferences.setValue(name, value);
313         }
314 }