001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.preferences;
011:
012: import java.util.HashSet;
013: import java.util.Set;
014:
015: import org.eclipse.core.runtime.Preferences;
016:
017: /**
018: * @since 3.1
019: */
020: public final class PreferencesAdapter extends PropertyMapAdapter {
021: private Preferences store;
022:
023: private Preferences.IPropertyChangeListener listener = new Preferences.IPropertyChangeListener() {
024: public void propertyChange(Preferences.PropertyChangeEvent event) {
025: firePropertyChange(event.getProperty());
026: }
027: };
028:
029: public PreferencesAdapter(Preferences toConvert) {
030: this .store = toConvert;
031: }
032:
033: /* (non-Javadoc)
034: * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#attachListener()
035: */
036: protected void attachListener() {
037: store.addPropertyChangeListener(listener);
038: }
039:
040: /* (non-Javadoc)
041: * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#detachListener()
042: */
043: protected void detachListener() {
044: store.removePropertyChangeListener(listener);
045: }
046:
047: /* (non-Javadoc)
048: * @see org.eclipse.ui.internal.preferences.IPropertyMap#keySet()
049: */
050: public Set keySet() {
051: Set result = new HashSet();
052:
053: String[] names = store.propertyNames();
054:
055: for (int i = 0; i < names.length; i++) {
056: String string = names[i];
057:
058: result.add(string);
059: }
060:
061: return result;
062: }
063:
064: /* (non-Javadoc)
065: * @see org.eclipse.ui.internal.preferences.IPropertyMap#getValue(java.lang.String, java.lang.Class)
066: */
067: public Object getValue(String propertyId, Class propertyType) {
068: if (propertyType.isAssignableFrom(String.class)) {
069: return store.getString(propertyId);
070: }
071:
072: if (propertyType == Boolean.class) {
073: return store.getBoolean(propertyId) ? Boolean.TRUE
074: : Boolean.FALSE;
075: }
076:
077: if (propertyType == Double.class) {
078: return new Double(store.getDouble(propertyId));
079: }
080:
081: if (propertyType == Float.class) {
082: return new Float(store.getFloat(propertyId));
083: }
084:
085: if (propertyType == Integer.class) {
086: return new Integer(store.getInt(propertyId));
087: }
088:
089: if (propertyType == Long.class) {
090: return new Long(store.getLong(propertyId));
091: }
092:
093: return null;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.eclipse.ui.internal.preferences.IPropertyMap#propertyExists(java.lang.String)
098: */
099: public boolean propertyExists(String propertyId) {
100: return store.contains(propertyId);
101: }
102:
103: /* (non-Javadoc)
104: * @see org.eclipse.ui.internal.preferences.IPropertyMap#setValue(java.lang.String, java.lang.Object)
105: */
106: public void setValue(String propertyId, Object newValue) {
107: if (newValue instanceof String) {
108: store.setValue(propertyId, (String) newValue);
109: } else if (newValue instanceof Integer) {
110: store.setValue(propertyId, ((Integer) newValue).intValue());
111: } else if (newValue instanceof Boolean) {
112: store.setValue(propertyId, ((Boolean) newValue)
113: .booleanValue());
114: } else if (newValue instanceof Double) {
115: store.setValue(propertyId, ((Double) newValue)
116: .doubleValue());
117: } else if (newValue instanceof Float) {
118: store.setValue(propertyId, ((Float) newValue).floatValue());
119: } else if (newValue instanceof Integer) {
120: store.setValue(propertyId, ((Integer) newValue).intValue());
121: } else if (newValue instanceof Long) {
122: store.setValue(propertyId, ((Long) newValue).longValue());
123: }
124: }
125:
126: }
|