KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > ThemeAdapter


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.ui.internal.preferences;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.jface.resource.FontRegistry;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.swt.graphics.Color;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.ui.themes.ITheme;
22
23 /**
24  * @since 3.1
25  */

26 public class ThemeAdapter extends PropertyMapAdapter {
27
28     private ITheme targetTheme;
29     
30     private IPropertyChangeListener listener = new IPropertyChangeListener() {
31         public void propertyChange(PropertyChangeEvent event) {
32             firePropertyChange(event.getProperty());
33         }
34     };
35     
36     public ThemeAdapter(ITheme targetTheme) {
37         this.targetTheme = targetTheme;
38     }
39     
40     /* (non-Javadoc)
41      * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#attachListener()
42      */

43     protected void attachListener() {
44         targetTheme.addPropertyChangeListener(listener);
45     }
46
47     /* (non-Javadoc)
48      * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#detachListener()
49      */

50     protected void detachListener() {
51         targetTheme.removePropertyChangeListener(listener);
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.ui.internal.preferences.IPropertyMap#getKeySet()
56      */

57     public Set JavaDoc keySet() {
58         return getKeySet(targetTheme);
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.ui.internal.preferences.IPropertyMap#getValue(java.lang.String, java.lang.Class)
63      */

64     public Object JavaDoc getValue(String JavaDoc propertyId, Class JavaDoc propertyType) {
65         return getValue(targetTheme, propertyId, propertyType);
66     }
67
68     public static Set JavaDoc getKeySet(ITheme targetTheme) {
69         Set JavaDoc result = new HashSet JavaDoc();
70         
71         result.addAll(targetTheme.keySet());
72         result.addAll(targetTheme.getColorRegistry().getKeySet());
73         result.addAll(targetTheme.getFontRegistry().getKeySet());
74         
75         return result;
76     }
77     
78     public static Object JavaDoc getValue(ITheme targetTheme, String JavaDoc propertyId, Class JavaDoc propertyType) {
79
80         if (propertyType.isAssignableFrom(String JavaDoc.class)) {
81             return targetTheme.getString(propertyId);
82         }
83         
84         if (propertyType.isAssignableFrom(Color.class)) {
85             Color result = targetTheme.getColorRegistry().get(propertyId);
86             if (result != null) {
87                 return result;
88             }
89         }
90         
91         if (propertyType.isAssignableFrom(Font.class)) {
92             FontRegistry fonts = targetTheme.getFontRegistry();
93             
94             if (fonts.hasValueFor(propertyId)) {
95                 return fonts.get(propertyId);
96             }
97         }
98         
99         if (propertyType == Integer JavaDoc.class) {
100             return new Integer JavaDoc(targetTheme.getInt(propertyId));
101         }
102
103         if (propertyType == Boolean JavaDoc.class) {
104             return targetTheme.getBoolean(propertyId) ? Boolean.TRUE : Boolean.FALSE;
105         }
106         
107         return null;
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.ui.internal.preferences.IPropertyMap#propertyExists(java.lang.String)
112      */

113     public boolean propertyExists(String JavaDoc propertyId) {
114         return keySet().contains(propertyId);
115     }
116
117     /* (non-Javadoc)
118      * @see org.eclipse.ui.internal.preferences.IPropertyMap#setValue(java.lang.String)
119      */

120     public void setValue(String JavaDoc propertyId, Object JavaDoc newValue) {
121         throw new UnsupportedOperationException JavaDoc();
122     }
123
124 }
125
Popular Tags