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