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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpdt.internal.ui.text;
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;
21 * Adapts {@link org.eclipse.core.runtime.Preferences} to
22 * {@link org.eclipse.jface.preference.IPreferenceStore}
26 public class PreferencesAdapter implements IPreferenceStore {
29 * Property change listener. Listens for events of type
30 * {@link org.eclipse.core.runtime.Preferences.PropertyChangeEvent} and fires
31 * a {@link org.eclipse.jface.util.PropertyChangeEvent} on the
32 * adapter with arguments from the received event.
34 private class PropertyChangeListener implements Preferences.IPropertyChangeListener {
37 * @see org.eclipse.core.runtime.Preferences.IPropertyChangeListener#propertyChange(org.eclipse.core.runtime.Preferences.PropertyChangeEvent)
39 public void propertyChange(Preferences.PropertyChangeEvent event) {
40 firePropertyChangeEvent(event.getProperty(), event.getOldValue(), event.getNewValue());
44 /** Listeners on the adapter */
45 private ListenerList fListeners= new ListenerList();
47 /** Listener on the adapted Preferences */
48 private PropertyChangeListener fListener= new PropertyChangeListener();
50 /** Adapted Preferences */
51 private Preferences fPreferences;
53 /** True iff no events should be forwarded */
54 private boolean fSilent;
57 * Initialize with empty Preferences.
59 public PreferencesAdapter() {
60 this(new Preferences());
63 * Initialize with the given Preferences.
65 * @param preferences The preferences to wrap.
67 public PreferencesAdapter(Preferences preferences) {
68 fPreferences= preferences;
74 public void addPropertyChangeListener(IPropertyChangeListener listener) {
75 if (fListeners.size() == 0)
76 fPreferences.addPropertyChangeListener(fListener);
77 fListeners.add(listener);
83 public void removePropertyChangeListener(IPropertyChangeListener listener) {
84 fListeners.remove(listener);
85 if (fListeners.size() == 0)
86 fPreferences.removePropertyChangeListener(fListener);
92 public boolean contains(String name) {
93 return fPreferences.contains(name);
99 public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) {
101 PropertyChangeEvent event= new PropertyChangeEvent(this, name, oldValue, newValue);
102 Object[] listeners= fListeners.getListeners();
103 for (int i= 0; i < listeners.length; i++)
104 ((IPropertyChangeListener) listeners[i]).propertyChange(event);
111 public boolean getBoolean(String name) {
112 return fPreferences.getBoolean(name);
118 public boolean getDefaultBoolean(String name) {
119 return fPreferences.getDefaultBoolean(name);
125 public double getDefaultDouble(String name) {
126 return fPreferences.getDefaultDouble(name);
132 public float getDefaultFloat(String name) {
133 return fPreferences.getDefaultFloat(name);
139 public int getDefaultInt(String name) {
140 return fPreferences.getDefaultInt(name);
146 public long getDefaultLong(String name) {
147 return fPreferences.getDefaultLong(name);
153 public String getDefaultString(String name) {
154 return fPreferences.getDefaultString(name);
160 public double getDouble(String name) {
161 return fPreferences.getDouble(name);
167 public float getFloat(String name) {
168 return fPreferences.getFloat(name);
174 public int getInt(String name) {
175 return fPreferences.getInt(name);
181 public long getLong(String name) {
182 return fPreferences.getLong(name);
188 public String getString(String name) {
189 return fPreferences.getString(name);
195 public boolean isDefault(String name) {
196 return fPreferences.isDefault(name);
202 public boolean needsSaving() {
203 return fPreferences.needsSaving();
209 public void putValue(String name, String value) {
212 fPreferences.setValue(name, value);
221 public void setDefault(String name, double value) {
222 fPreferences.setDefault(name, value);
228 public void setDefault(String name, float value) {
229 fPreferences.setDefault(name, value);
235 public void setDefault(String name, int value) {
236 fPreferences.setDefault(name, value);
242 public void setDefault(String name, long value) {
243 fPreferences.setDefault(name, value);
249 public void setDefault(String name, String defaultObject) {
250 fPreferences.setDefault(name, defaultObject);
256 public void setDefault(String name, boolean value) {
257 fPreferences.setDefault(name, value);
263 public void setToDefault(String name) {
264 fPreferences.setToDefault(name);
270 public void setValue(String name, double value) {
271 fPreferences.setValue(name, value);
277 public void setValue(String name, float value) {
278 fPreferences.setValue(name, value);
284 public void setValue(String name, int value) {
285 fPreferences.setValue(name, value);
291 public void setValue(String name, long value) {
292 fPreferences.setValue(name, value);
298 public void setValue(String name, String value) {
299 fPreferences.setValue(name, value);
305 public void setValue(String name, boolean value) {
306 fPreferences.setValue(name, value);