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.jface.resource.FontRegistry;
016: import org.eclipse.jface.util.IPropertyChangeListener;
017: import org.eclipse.jface.util.PropertyChangeEvent;
018: import org.eclipse.swt.graphics.Color;
019: import org.eclipse.swt.graphics.Font;
020: import org.eclipse.ui.themes.ITheme;
021:
022: /**
023: * @since 3.1
024: */
025: public class ThemeAdapter extends PropertyMapAdapter {
026:
027: private ITheme targetTheme;
028:
029: private IPropertyChangeListener listener = new IPropertyChangeListener() {
030: public void propertyChange(PropertyChangeEvent event) {
031: firePropertyChange(event.getProperty());
032: }
033: };
034:
035: public ThemeAdapter(ITheme targetTheme) {
036: this .targetTheme = targetTheme;
037: }
038:
039: /* (non-Javadoc)
040: * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#attachListener()
041: */
042: protected void attachListener() {
043: targetTheme.addPropertyChangeListener(listener);
044: }
045:
046: /* (non-Javadoc)
047: * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#detachListener()
048: */
049: protected void detachListener() {
050: targetTheme.removePropertyChangeListener(listener);
051: }
052:
053: /* (non-Javadoc)
054: * @see org.eclipse.ui.internal.preferences.IPropertyMap#getKeySet()
055: */
056: public Set keySet() {
057: return getKeySet(targetTheme);
058: }
059:
060: /* (non-Javadoc)
061: * @see org.eclipse.ui.internal.preferences.IPropertyMap#getValue(java.lang.String, java.lang.Class)
062: */
063: public Object getValue(String propertyId, Class propertyType) {
064: return getValue(targetTheme, propertyId, propertyType);
065: }
066:
067: public static Set getKeySet(ITheme targetTheme) {
068: Set result = new HashSet();
069:
070: result.addAll(targetTheme.keySet());
071: result.addAll(targetTheme.getColorRegistry().getKeySet());
072: result.addAll(targetTheme.getFontRegistry().getKeySet());
073:
074: return result;
075: }
076:
077: public static Object getValue(ITheme targetTheme,
078: String propertyId, Class propertyType) {
079:
080: if (propertyType.isAssignableFrom(String.class)) {
081: return targetTheme.getString(propertyId);
082: }
083:
084: if (propertyType.isAssignableFrom(Color.class)) {
085: Color result = targetTheme.getColorRegistry().get(
086: propertyId);
087: if (result != null) {
088: return result;
089: }
090: }
091:
092: if (propertyType.isAssignableFrom(Font.class)) {
093: FontRegistry fonts = targetTheme.getFontRegistry();
094:
095: if (fonts.hasValueFor(propertyId)) {
096: return fonts.get(propertyId);
097: }
098: }
099:
100: if (propertyType == Integer.class) {
101: return new Integer(targetTheme.getInt(propertyId));
102: }
103:
104: if (propertyType == Boolean.class) {
105: return targetTheme.getBoolean(propertyId) ? Boolean.TRUE
106: : Boolean.FALSE;
107: }
108:
109: return null;
110: }
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.ui.internal.preferences.IPropertyMap#propertyExists(java.lang.String)
114: */
115: public boolean propertyExists(String propertyId) {
116: return keySet().contains(propertyId);
117: }
118:
119: /* (non-Javadoc)
120: * @see org.eclipse.ui.internal.preferences.IPropertyMap#setValue(java.lang.String)
121: */
122: public void setValue(String propertyId, Object newValue) {
123: throw new UnsupportedOperationException();
124: }
125:
126: }
|