1 11 package org.eclipse.ui.internal.preferences; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.core.runtime.Preferences; 17 18 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 37 protected void attachListener() { 38 store.addPropertyChangeListener(listener); 39 } 40 41 44 protected void detachListener() { 45 store.removePropertyChangeListener(listener); 46 } 47 48 51 public Set keySet() { 52 Set result = new HashSet (); 53 54 String [] names = store.propertyNames(); 55 56 for (int i = 0; i < names.length; i++) { 57 String string = names[i]; 58 59 result.add(string); 60 } 61 62 return result; 63 } 64 65 68 public Object getValue(String propertyId, Class propertyType) { 69 if (propertyType.isAssignableFrom(String .class)) { 70 return store.getString(propertyId); 71 } 72 73 if (propertyType == Boolean .class) { 74 return store.getBoolean(propertyId) ? Boolean.TRUE : Boolean.FALSE; 75 } 76 77 if (propertyType == Double .class) { 78 return new Double (store.getDouble(propertyId)); 79 } 80 81 if (propertyType == Float .class) { 82 return new Float (store.getFloat(propertyId)); 83 } 84 85 if (propertyType == Integer .class) { 86 return new Integer (store.getInt(propertyId)); 87 } 88 89 if (propertyType == Long .class) { 90 return new Long (store.getLong(propertyId)); 91 } 92 93 return null; 94 } 95 96 99 public boolean propertyExists(String propertyId) { 100 return store.contains(propertyId); 101 } 102 103 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).booleanValue()); 113 } else if (newValue instanceof Double ) { 114 store.setValue(propertyId, ((Double )newValue).doubleValue()); 115 } else if (newValue instanceof Float ) { 116 store.setValue(propertyId, ((Float )newValue).floatValue()); 117 } else if (newValue instanceof Integer ) { 118 store.setValue(propertyId, ((Integer )newValue).intValue()); 119 } else if (newValue instanceof Long ) { 120 store.setValue(propertyId, ((Long )newValue).longValue()); 121 } 122 } 123 124 } 125 | Popular Tags |