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