KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.runtime.Preferences;
17
18 /**
19  * @since 3.1
20  */

21 public final class PreferencesAdapter extends PropertyMapAdapter {
22     private Preferences store;
23     
24     private Preferences.IPropertyChangeListener listener = new Preferences.IPropertyChangeListener() {
25         public void propertyChange(Preferences.PropertyChangeEvent event) {
26             firePropertyChange(event.getProperty());
27         }
28     };
29     
30     public PreferencesAdapter(Preferences toConvert) {
31         this.store = toConvert;
32     }
33     
34     /* (non-Javadoc)
35      * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#attachListener()
36      */

37     protected void attachListener() {
38         store.addPropertyChangeListener(listener);
39     }
40
41     /* (non-Javadoc)
42      * @see org.eclipse.ui.internal.preferences.PropertyMapAdapter#detachListener()
43      */

44     protected void detachListener() {
45         store.removePropertyChangeListener(listener);
46     }
47
48     /* (non-Javadoc)
49      * @see org.eclipse.ui.internal.preferences.IPropertyMap#keySet()
50      */

51     public Set JavaDoc keySet() {
52         Set JavaDoc result = new HashSet JavaDoc();
53         
54         String JavaDoc[] names = store.propertyNames();
55         
56         for (int i = 0; i < names.length; i++) {
57             String JavaDoc string = names[i];
58             
59             result.add(string);
60         }
61         
62         return result;
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.ui.internal.preferences.IPropertyMap#getValue(java.lang.String, java.lang.Class)
67      */

68     public Object JavaDoc getValue(String JavaDoc propertyId, Class JavaDoc propertyType) {
69         if (propertyType.isAssignableFrom(String JavaDoc.class)) {
70             return store.getString(propertyId);
71         }
72         
73         if (propertyType == Boolean JavaDoc.class) {
74             return store.getBoolean(propertyId) ? Boolean.TRUE : Boolean.FALSE;
75         }
76         
77         if (propertyType == Double JavaDoc.class) {
78             return new Double JavaDoc(store.getDouble(propertyId));
79         }
80         
81         if (propertyType == Float JavaDoc.class) {
82             return new Float JavaDoc(store.getFloat(propertyId));
83         }
84         
85         if (propertyType == Integer JavaDoc.class) {
86             return new Integer JavaDoc(store.getInt(propertyId));
87         }
88         
89         if (propertyType == Long JavaDoc.class) {
90             return new Long JavaDoc(store.getLong(propertyId));
91         }
92         
93         return null;
94     }
95
96     /* (non-Javadoc)
97      * @see org.eclipse.ui.internal.preferences.IPropertyMap#propertyExists(java.lang.String)
98      */

99     public boolean propertyExists(String JavaDoc 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 JavaDoc propertyId, Object JavaDoc newValue) {
107         if (newValue instanceof String JavaDoc) {
108             store.setValue(propertyId, (String JavaDoc)newValue);
109         } else if (newValue instanceof Integer JavaDoc) {
110             store.setValue(propertyId, ((Integer JavaDoc)newValue).intValue());
111         } else if (newValue instanceof Boolean JavaDoc) {
112             store.setValue(propertyId, ((Boolean JavaDoc)newValue).booleanValue());
113         } else if (newValue instanceof Double JavaDoc) {
114             store.setValue(propertyId, ((Double JavaDoc)newValue).doubleValue());
115         } else if (newValue instanceof Float JavaDoc) {
116             store.setValue(propertyId, ((Float JavaDoc)newValue).floatValue());
117         } else if (newValue instanceof Integer JavaDoc) {
118             store.setValue(propertyId, ((Integer JavaDoc)newValue).intValue());
119         } else if (newValue instanceof Long JavaDoc) {
120             store.setValue(propertyId, ((Long JavaDoc)newValue).longValue());
121         }
122     }
123
124 }
125
Popular Tags