1 11 package org.eclipse.ui.internal.navigator.extensions; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.commands.common.EventManager; 17 import org.eclipse.jface.util.IPropertyChangeListener; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 import org.eclipse.ui.navigator.IExtensionStateModel; 20 21 27 public class ExtensionStateModel extends EventManager implements 28 IExtensionStateModel { 29 30 private final String id; 31 32 private final String viewerId; 33 34 private final Map values = new HashMap (); 35 36 45 public ExtensionStateModel(String anId, String aViewerId) { 46 id = anId; 47 viewerId = aViewerId; 48 } 49 50 public String getId() { 51 return id; 52 } 53 54 public String getViewerId() { 55 return viewerId; 56 } 57 58 public String getStringProperty(String aPropertyName) { 59 return (String ) values.get(aPropertyName); 60 } 61 62 public boolean getBooleanProperty(String aPropertyName) { 63 64 Boolean b = (Boolean ) values.get(aPropertyName); 65 return b != null ? b.booleanValue() : false; 66 } 67 68 public int getIntProperty(String aPropertyName) { 69 Integer i = (Integer ) values.get(aPropertyName); 70 return i != null ? i.intValue() : -1; 71 } 72 73 public void setStringProperty(String aPropertyName, String aPropertyValue) { 74 String oldValue = (String ) values.get(aPropertyName); 75 String newValue = aPropertyValue; 76 if (hasPropertyChanged(oldValue, newValue)) { 77 values.put(aPropertyName, newValue); 78 firePropertyChangeEvent(new PropertyChangeEvent(this, 79 aPropertyName, oldValue, newValue)); 80 } 81 } 82 83 public void setBooleanProperty(String aPropertyName, boolean aPropertyValue) { 84 Boolean oldValue = (Boolean ) values.get(aPropertyName); 85 Boolean newValue = aPropertyValue ? Boolean.TRUE : Boolean.FALSE; 86 if (hasPropertyChanged(oldValue, newValue)) { 87 88 values.put(aPropertyName, aPropertyValue ? Boolean.TRUE 89 : Boolean.FALSE); 90 firePropertyChangeEvent(new PropertyChangeEvent(this, 91 aPropertyName, oldValue, newValue)); 92 } 93 } 94 95 public void setIntProperty(String aPropertyName, int aPropertyValue) { 96 Integer oldValue = (Integer ) values.get(aPropertyName); 97 Integer newValue = new Integer (aPropertyValue); 98 if (hasPropertyChanged(oldValue, newValue)) { 99 values.put(aPropertyName, newValue); 100 firePropertyChangeEvent(new PropertyChangeEvent(this, 101 aPropertyName, oldValue, newValue)); 102 } 103 } 104 105 public void addPropertyChangeListener(IPropertyChangeListener aListener) { 106 addListenerObject(aListener); 107 } 108 109 public void removePropertyChangeListener(IPropertyChangeListener aListener) { 110 removeListenerObject(aListener); 111 } 112 113 public Object getProperty(String aPropertyName) { 114 return values.get(aPropertyName); 115 } 116 117 public void setProperty(String aPropertyName, Object aPropertyValue) { 118 119 Object oldValue = values.get(aPropertyName); 120 Object newValue = aPropertyValue; 121 if (hasPropertyChanged(oldValue, newValue)) { 122 values.put(aPropertyName, newValue); 123 firePropertyChangeEvent(new PropertyChangeEvent(this, 124 aPropertyName, oldValue, newValue)); 125 } 126 } 127 128 private boolean hasPropertyChanged(Object oldValue, Object newValue) { 129 return oldValue == null || !oldValue.equals(newValue); 130 } 131 132 protected void firePropertyChangeEvent(PropertyChangeEvent anEvent) { 133 Object [] listeners = getListeners(); 134 for (int i = 0; i < listeners.length; ++i) { 135 ((IPropertyChangeListener) listeners[i]).propertyChange(anEvent); 136 } 137 } 138 139 } 140 | Popular Tags |