KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > EclipsePreferencesAdapter


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.compare;
12
13 import org.eclipse.core.runtime.ListenerList;
14 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
15 import org.eclipse.core.runtime.preferences.IScopeContext;
16
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.swt.widgets.Display;
20 import org.osgi.service.prefs.BackingStoreException;
21
22 import org.eclipse.jface.preference.IPreferenceStore;
23
24 /**
25  * Adapts an options {@link IEclipsePreferences} to {@link org.eclipse.jface.preference.IPreferenceStore}.
26  * <p>
27  * This preference store is read-only i.e. write access
28  * throws an {@link java.lang.UnsupportedOperationException}.
29  * </p>
30  *
31  * @since 3.1
32  */

33 class EclipsePreferencesAdapter implements IPreferenceStore {
34
35     /**
36      * Preference change listener. Listens for events preferences
37      * fires a {@link org.eclipse.jface.util.PropertyChangeEvent}
38      * on this adapter with arguments from the received event.
39      */

40     private class PreferenceChangeListener implements IEclipsePreferences.IPreferenceChangeListener {
41
42         /**
43          * {@inheritDoc}
44          */

45         public void preferenceChange(final IEclipsePreferences.PreferenceChangeEvent event) {
46             if (Display.getCurrent() == null) {
47                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
48                     public void run() {
49                         firePropertyChangeEvent(event.getKey(), event.getOldValue(), event.getNewValue());
50                     }
51                 });
52             } else {
53                 firePropertyChangeEvent(event.getKey(), event.getOldValue(), event.getNewValue());
54             }
55         }
56     }
57
58     /** Listeners on on this adapter */
59     private ListenerList fListeners= new ListenerList(ListenerList.IDENTITY);
60
61     /** Listener on the node */
62     private IEclipsePreferences.IPreferenceChangeListener fListener= new PreferenceChangeListener();
63
64     /** wrapped node */
65     private final IScopeContext fContext;
66     private final String JavaDoc fQualifier;
67
68     /**
69      * Initialize with the node to wrap
70      *
71      * @param context The context to access
72      */

73     public EclipsePreferencesAdapter(IScopeContext context, String JavaDoc qualifier) {
74         fContext= context;
75         fQualifier= qualifier;
76     }
77
78     private IEclipsePreferences getNode() {
79         return fContext.getNode(fQualifier);
80     }
81
82     /**
83      * {@inheritDoc}
84      */

85     public void addPropertyChangeListener(IPropertyChangeListener listener) {
86         if (fListeners.size() == 0)
87             getNode().addPreferenceChangeListener(fListener);
88         fListeners.add(listener);
89     }
90
91     /**
92      * {@inheritDoc}
93      */

94     public void removePropertyChangeListener(IPropertyChangeListener listener) {
95         fListeners.remove(listener);
96         if (fListeners.size() == 0) {
97             getNode().removePreferenceChangeListener(fListener);
98         }
99     }
100
101     /**
102      * {@inheritDoc}
103      */

104     public boolean contains(String JavaDoc name) {
105         return getNode().get(name, null) != null;
106     }
107
108     /**
109      * {@inheritDoc}
110      */

111     public void firePropertyChangeEvent(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
112         PropertyChangeEvent event= new PropertyChangeEvent(this, name, oldValue, newValue);
113         Object JavaDoc[] listeners= fListeners.getListeners();
114         for (int i= 0; i < listeners.length; i++)
115             ((IPropertyChangeListener) listeners[i]).propertyChange(event);
116     }
117
118     /**
119      * {@inheritDoc}
120      */

121     public boolean getBoolean(String JavaDoc name) {
122         return getNode().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT);
123     }
124
125     /**
126      * {@inheritDoc}
127      */

128     public boolean getDefaultBoolean(String JavaDoc name) {
129         return BOOLEAN_DEFAULT_DEFAULT;
130     }
131
132     /**
133      * {@inheritDoc}
134      */

135     public double getDefaultDouble(String JavaDoc name) {
136         return DOUBLE_DEFAULT_DEFAULT;
137     }
138
139     /**
140      * {@inheritDoc}
141      */

142     public float getDefaultFloat(String JavaDoc name) {
143         return FLOAT_DEFAULT_DEFAULT;
144     }
145
146     /**
147      * {@inheritDoc}
148      */

149     public int getDefaultInt(String JavaDoc name) {
150         return INT_DEFAULT_DEFAULT;
151     }
152
153     /**
154      * {@inheritDoc}
155      */

156     public long getDefaultLong(String JavaDoc name) {
157         return LONG_DEFAULT_DEFAULT;
158     }
159
160     /**
161      * {@inheritDoc}
162      */

163     public String JavaDoc getDefaultString(String JavaDoc name) {
164         return STRING_DEFAULT_DEFAULT;
165     }
166
167     /**
168      * {@inheritDoc}
169      */

170     public double getDouble(String JavaDoc name) {
171         return getNode().getDouble(name, DOUBLE_DEFAULT_DEFAULT);
172     }
173
174     /**
175      * {@inheritDoc}
176      */

177     public float getFloat(String JavaDoc name) {
178         return getNode().getFloat(name, FLOAT_DEFAULT_DEFAULT);
179     }
180
181     /**
182      * {@inheritDoc}
183      */

184     public int getInt(String JavaDoc name) {
185         return getNode().getInt(name, INT_DEFAULT_DEFAULT);
186     }
187
188     /**
189      * {@inheritDoc}
190      */

191     public long getLong(String JavaDoc name) {
192         return getNode().getLong(name, LONG_DEFAULT_DEFAULT);
193     }
194
195     /**
196      * {@inheritDoc}
197      */

198     public String JavaDoc getString(String JavaDoc name) {
199         return getNode().get(name, STRING_DEFAULT_DEFAULT);
200     }
201
202     /**
203      * {@inheritDoc}
204      */

205     public boolean isDefault(String JavaDoc name) {
206         return false;
207     }
208
209     /**
210      * {@inheritDoc}
211      */

212     public boolean needsSaving() {
213         try {
214             return getNode().keys().length > 0;
215         } catch (BackingStoreException e) {
216             // ignore
217
}
218         return true;
219     }
220
221     /**
222      * {@inheritDoc}
223      */

224     public void putValue(String JavaDoc name, String JavaDoc value) {
225         throw new UnsupportedOperationException JavaDoc();
226     }
227
228     /**
229      * {@inheritDoc}
230      */

231     public void setDefault(String JavaDoc name, double value) {
232         throw new UnsupportedOperationException JavaDoc();
233     }
234
235     /**
236      * {@inheritDoc}
237      */

238     public void setDefault(String JavaDoc name, float value) {
239         throw new UnsupportedOperationException JavaDoc();
240     }
241
242     /**
243      * {@inheritDoc}
244      */

245     public void setDefault(String JavaDoc name, int value) {
246         throw new UnsupportedOperationException JavaDoc();
247     }
248
249     /**
250      * {@inheritDoc}
251      */

252     public void setDefault(String JavaDoc name, long value) {
253         throw new UnsupportedOperationException JavaDoc();
254     }
255
256     /**
257      * {@inheritDoc}
258      */

259     public void setDefault(String JavaDoc name, String JavaDoc defaultObject) {
260         throw new UnsupportedOperationException JavaDoc();
261     }
262
263     /**
264      * {@inheritDoc}
265      */

266     public void setDefault(String JavaDoc name, boolean value) {
267         throw new UnsupportedOperationException JavaDoc();
268     }
269
270     /**
271      * {@inheritDoc}
272      */

273     public void setToDefault(String JavaDoc name) {
274         throw new UnsupportedOperationException JavaDoc();
275     }
276
277     /**
278      * {@inheritDoc}
279      */

280     public void setValue(String JavaDoc name, double value) {
281         throw new UnsupportedOperationException JavaDoc();
282     }
283
284     /**
285      * {@inheritDoc}
286      */

287     public void setValue(String JavaDoc name, float value) {
288         throw new UnsupportedOperationException JavaDoc();
289     }
290
291     /**
292      * {@inheritDoc}
293      */

294     public void setValue(String JavaDoc name, int value) {
295         throw new UnsupportedOperationException JavaDoc();
296     }
297
298     /**
299      * {@inheritDoc}
300      */

301     public void setValue(String JavaDoc name, long value) {
302         throw new UnsupportedOperationException JavaDoc();
303     }
304
305     /**
306      * {@inheritDoc}
307      */

308     public void setValue(String JavaDoc name, String JavaDoc value) {
309         throw new UnsupportedOperationException JavaDoc();
310     }
311
312     /**
313      * {@inheritDoc}
314      */

315     public void setValue(String JavaDoc name, boolean value) {
316         throw new UnsupportedOperationException JavaDoc();
317     }
318
319 }
320
Popular Tags