]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2005, 2008 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.osgi.service.prefs.BackingStoreException; | |
14 | ||
15 | import org.eclipse.swt.widgets.Display; | |
16 | ||
17 | import org.eclipse.core.runtime.ListenerList; | |
18 | import org.eclipse.core.runtime.preferences.IEclipsePreferences; | |
19 | import org.eclipse.core.runtime.preferences.IScopeContext; | |
20 | ||
21 | import org.eclipse.jface.preference.IPreferenceStore; | |
22 | import org.eclipse.jface.util.IPropertyChangeListener; | |
23 | import org.eclipse.jface.util.PropertyChangeEvent; | |
24 | ||
25 | /** | |
26 | * Adapts an options {@link IEclipsePreferences} to {@link org.eclipse.jface.preference.IPreferenceStore}. | |
27 | * <p> | |
28 | * This preference store is read-only i.e. write access | |
29 | * throws an {@link java.lang.UnsupportedOperationException}. | |
30 | * </p> | |
31 | * | |
32 | * @since 3.1 | |
33 | */ | |
34 | class EclipsePreferencesAdapter implements IPreferenceStore { | |
35 | ||
36 | /** | |
37 | * Preference change listener. Listens for events preferences | |
38 | * fires a {@link org.eclipse.jface.util.PropertyChangeEvent} | |
39 | * on this adapter with arguments from the received event. | |
40 | */ | |
41 | private class PreferenceChangeListener implements IEclipsePreferences.IPreferenceChangeListener { | |
42 | ||
43 | /** | |
44 | * {@inheritDoc} | |
45 | */ | |
46 | public void preferenceChange(final IEclipsePreferences.PreferenceChangeEvent event) { | |
47 | if (Display.getCurrent() == null) { | |
48 | Display.getDefault().asyncExec(new Runnable() { | |
49 | public void run() { | |
50 | firePropertyChangeEvent(event.getKey(), event.getOldValue(), event.getNewValue()); | |
51 | } | |
52 | }); | |
53 | } else { | |
54 | firePropertyChangeEvent(event.getKey(), event.getOldValue(), event.getNewValue()); | |
55 | } | |
56 | } | |
57 | } | |
58 | ||
59 | /** Listeners on on this adapter */ | |
60 | private ListenerList fListeners= new ListenerList(ListenerList.IDENTITY); | |
61 | ||
62 | /** Listener on the node */ | |
63 | private IEclipsePreferences.IPreferenceChangeListener fListener= new PreferenceChangeListener(); | |
64 | ||
65 | /** wrapped node */ | |
66 | private final IScopeContext fContext; | |
67 | private final String fQualifier; | |
68 | ||
69 | /** | |
70 | * Initialize with the node to wrap | |
71 | * | |
72 | * @param context The context to access | |
73 | */ | |
74 | public EclipsePreferencesAdapter(IScopeContext context, String qualifier) { | |
75 | fContext= context; | |
76 | fQualifier= qualifier; | |
77 | } | |
78 | ||
79 | private IEclipsePreferences getNode() { | |
80 | return fContext.getNode(fQualifier); | |
81 | } | |
82 | ||
83 | /** | |
84 | * {@inheritDoc} | |
85 | */ | |
86 | public void addPropertyChangeListener(IPropertyChangeListener listener) { | |
87 | if (fListeners.size() == 0) | |
88 | getNode().addPreferenceChangeListener(fListener); | |
89 | fListeners.add(listener); | |
90 | } | |
91 | ||
92 | /** | |
93 | * {@inheritDoc} | |
94 | */ | |
95 | public void removePropertyChangeListener(IPropertyChangeListener listener) { | |
96 | fListeners.remove(listener); | |
97 | if (fListeners.size() == 0) { | |
98 | getNode().removePreferenceChangeListener(fListener); | |
99 | } | |
100 | } | |
101 | ||
102 | /** | |
103 | * {@inheritDoc} | |
104 | */ | |
105 | public boolean contains(String name) { | |
106 | return getNode().get(name, null) != null; | |
107 | } | |
108 | ||
109 | /** | |
110 | * {@inheritDoc} | |
111 | */ | |
112 | public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { | |
113 | PropertyChangeEvent event= new PropertyChangeEvent(this, name, oldValue, newValue); | |
114 | Object[] listeners= fListeners.getListeners(); | |
115 | for (int i= 0; i < listeners.length; i++) | |
116 | ((IPropertyChangeListener) listeners[i]).propertyChange(event); | |
117 | } | |
118 | ||
119 | /** | |
120 | * {@inheritDoc} | |
121 | */ | |
122 | public boolean getBoolean(String name) { | |
123 | return getNode().getBoolean(name, BOOLEAN_DEFAULT_DEFAULT); | |
124 | } | |
125 | ||
126 | /** | |
127 | * {@inheritDoc} | |
128 | */ | |
129 | public boolean getDefaultBoolean(String name) { | |
130 | return BOOLEAN_DEFAULT_DEFAULT; | |
131 | } | |
132 | ||
133 | /** | |
134 | * {@inheritDoc} | |
135 | */ | |
136 | public double getDefaultDouble(String name) { | |
137 | return DOUBLE_DEFAULT_DEFAULT; | |
138 | } | |
139 | ||
140 | /** | |
141 | * {@inheritDoc} | |
142 | */ | |
143 | public float getDefaultFloat(String name) { | |
144 | return FLOAT_DEFAULT_DEFAULT; | |
145 | } | |
146 | ||
147 | /** | |
148 | * {@inheritDoc} | |
149 | */ | |
150 | public int getDefaultInt(String name) { | |
151 | return INT_DEFAULT_DEFAULT; | |
152 | } | |
153 | ||
154 | /** | |
155 | * {@inheritDoc} | |
156 | */ | |
157 | public long getDefaultLong(String name) { | |
158 | return LONG_DEFAULT_DEFAULT; | |
159 | } | |
160 | ||
161 | /** | |
162 | * {@inheritDoc} | |
163 | */ | |
164 | public String getDefaultString(String name) { | |
165 | return STRING_DEFAULT_DEFAULT; | |
166 | } | |
167 | ||
168 | /** | |
169 | * {@inheritDoc} | |
170 | */ | |
171 | public double getDouble(String name) { | |
172 | return getNode().getDouble(name, DOUBLE_DEFAULT_DEFAULT); | |
173 | } | |
174 | ||
175 | /** | |
176 | * {@inheritDoc} | |
177 | */ | |
178 | public float getFloat(String name) { | |
179 | return getNode().getFloat(name, FLOAT_DEFAULT_DEFAULT); | |
180 | } | |
181 | ||
182 | /** | |
183 | * {@inheritDoc} | |
184 | */ | |
185 | public int getInt(String name) { | |
186 | return getNode().getInt(name, INT_DEFAULT_DEFAULT); | |
187 | } | |
188 | ||
189 | /** | |
190 | * {@inheritDoc} | |
191 | */ | |
192 | public long getLong(String name) { | |
193 | return getNode().getLong(name, LONG_DEFAULT_DEFAULT); | |
194 | } | |
195 | ||
196 | /** | |
197 | * {@inheritDoc} | |
198 | */ | |
199 | public String getString(String name) { | |
200 | return getNode().get(name, STRING_DEFAULT_DEFAULT); | |
201 | } | |
202 | ||
203 | /** | |
204 | * {@inheritDoc} | |
205 | */ | |
206 | public boolean isDefault(String name) { | |
207 | return false; | |
208 | } | |
209 | ||
210 | /** | |
211 | * {@inheritDoc} | |
212 | */ | |
213 | public boolean needsSaving() { | |
214 | try { | |
215 | return getNode().keys().length > 0; | |
216 | } catch (BackingStoreException e) { | |
217 | // ignore | |
218 | } | |
219 | return true; | |
220 | } | |
221 | ||
222 | /** | |
223 | * {@inheritDoc} | |
224 | */ | |
225 | public void putValue(String name, String value) { | |
226 | throw new UnsupportedOperationException(); | |
227 | } | |
228 | ||
229 | /** | |
230 | * {@inheritDoc} | |
231 | */ | |
232 | public void setDefault(String name, double value) { | |
233 | throw new UnsupportedOperationException(); | |
234 | } | |
235 | ||
236 | /** | |
237 | * {@inheritDoc} | |
238 | */ | |
239 | public void setDefault(String name, float value) { | |
240 | throw new UnsupportedOperationException(); | |
241 | } | |
242 | ||
243 | /** | |
244 | * {@inheritDoc} | |
245 | */ | |
246 | public void setDefault(String name, int value) { | |
247 | throw new UnsupportedOperationException(); | |
248 | } | |
249 | ||
250 | /** | |
251 | * {@inheritDoc} | |
252 | */ | |
253 | public void setDefault(String name, long value) { | |
254 | throw new UnsupportedOperationException(); | |
255 | } | |
256 | ||
257 | /** | |
258 | * {@inheritDoc} | |
259 | */ | |
260 | public void setDefault(String name, String defaultObject) { | |
261 | throw new UnsupportedOperationException(); | |
262 | } | |
263 | ||
264 | /** | |
265 | * {@inheritDoc} | |
266 | */ | |
267 | public void setDefault(String name, boolean value) { | |
268 | throw new UnsupportedOperationException(); | |
269 | } | |
270 | ||
271 | /** | |
272 | * {@inheritDoc} | |
273 | */ | |
274 | public void setToDefault(String name) { | |
275 | throw new UnsupportedOperationException(); | |
276 | } | |
277 | ||
278 | /** | |
279 | * {@inheritDoc} | |
280 | */ | |
281 | public void setValue(String name, double value) { | |
282 | throw new UnsupportedOperationException(); | |
283 | } | |
284 | ||
285 | /** | |
286 | * {@inheritDoc} | |
287 | */ | |
288 | public void setValue(String name, float value) { | |
289 | throw new UnsupportedOperationException(); | |
290 | } | |
291 | ||
292 | /** | |
293 | * {@inheritDoc} | |
294 | */ | |
295 | public void setValue(String name, int value) { | |
296 | throw new UnsupportedOperationException(); | |
297 | } | |
298 | ||
299 | /** | |
300 | * {@inheritDoc} | |
301 | */ | |
302 | public void setValue(String name, long value) { | |
303 | throw new UnsupportedOperationException(); | |
304 | } | |
305 | ||
306 | /** | |
307 | * {@inheritDoc} | |
308 | */ | |
309 | public void setValue(String name, String value) { | |
310 | throw new UnsupportedOperationException(); | |
311 | } | |
312 | ||
313 | /** | |
314 | * {@inheritDoc} | |
315 | */ | |
316 | public void setValue(String name, boolean value) { | |
317 | throw new UnsupportedOperationException(); | |
318 | } | |
319 | ||
320 | } |