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.Set;
013:
014: import org.eclipse.jface.preference.IPreferenceStore;
015: import org.eclipse.jface.util.IPropertyChangeListener;
016: import org.eclipse.jface.util.PropertyChangeEvent;
017:
018: /**
019: * @since 3.1
020: */
021: public final class PreferenceStoreAdapter extends PropertyMapAdapter {
022:
023: private IPreferenceStore store;
024:
025: private IPropertyChangeListener listener = new IPropertyChangeListener() {
026: public void propertyChange(PropertyChangeEvent event) {
027: firePropertyChange(event.getProperty());
028: }
029: };
030:
031: public PreferenceStoreAdapter(IPreferenceStore toConvert) {
032: this .store = toConvert;
033: }
034:
035: /* (non-Javadoc)
036: * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#attachListener()
037: */
038: protected void attachListener() {
039: store.addPropertyChangeListener(listener);
040: }
041:
042: /* (non-Javadoc)
043: * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#detachListener()
044: */
045: protected void detachListener() {
046: store.removePropertyChangeListener(listener);
047: }
048:
049: /* (non-Javadoc)
050: * @see org.eclipse.ui.internal.preferences.IPropertyMap#keySet()
051: */
052: public Set keySet() {
053: throw new UnsupportedOperationException();
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.ui.internal.preferences.IPropertyMap#getValue(java.lang.String, java.lang.Class)
058: */
059: public Object getValue(String propertyId, Class propertyType) {
060: if (propertyType.isAssignableFrom(String.class)) {
061: return store.getString(propertyId);
062: }
063:
064: if (propertyType == Boolean.class) {
065: return store.getBoolean(propertyId) ? Boolean.TRUE
066: : Boolean.FALSE;
067: }
068:
069: if (propertyType == Double.class) {
070: return new Double(store.getDouble(propertyId));
071: }
072:
073: if (propertyType == Float.class) {
074: return new Float(store.getFloat(propertyId));
075: }
076:
077: if (propertyType == Integer.class) {
078: return new Integer(store.getInt(propertyId));
079: }
080:
081: if (propertyType == Long.class) {
082: return new Long(store.getLong(propertyId));
083: }
084:
085: return null;
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.ui.internal.preferences.IPropertyMap#propertyExists(java.lang.String)
090: */
091: public boolean propertyExists(String propertyId) {
092: return store.contains(propertyId);
093: }
094:
095: /* (non-Javadoc)
096: * @see org.eclipse.ui.internal.preferences.IPropertyMap#setValue(java.lang.String, java.lang.Object)
097: */
098: public void setValue(String propertyId, Object newValue) {
099: if (newValue instanceof String) {
100: store.setValue(propertyId, (String) newValue);
101: } else if (newValue instanceof Integer) {
102: store.setValue(propertyId, ((Integer) newValue).intValue());
103: } else if (newValue instanceof Boolean) {
104: store.setValue(propertyId, ((Boolean) newValue)
105: .booleanValue());
106: } else if (newValue instanceof Double) {
107: store.setValue(propertyId, ((Double) newValue)
108: .doubleValue());
109: } else if (newValue instanceof Float) {
110: store.setValue(propertyId, ((Float) newValue).floatValue());
111: } else if (newValue instanceof Integer) {
112: store.setValue(propertyId, ((Integer) newValue).intValue());
113: } else if (newValue instanceof Long) {
114: store.setValue(propertyId, ((Long) newValue).longValue());
115: }
116: }
117:
118: }
|