Permalink
Browse files

Add Map methods to BeanAdapter; update variable names that referenced…

… BeanDictionary.

git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@936555 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
Gregory K. Brown
Gregory K. Brown committed Apr 21, 2010
1 parent 7a909e6 commit d878243be2a96f7499e74025ffcbbb8fdb620322
@@ -20,22 +20,25 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
-import org.apache.pivot.collections.Dictionary;
+import org.apache.pivot.collections.Map;
+import org.apache.pivot.collections.MapListener;
+import org.apache.pivot.util.ListenerList;
/**
- * Exposes Java bean properties of an object via the {@link Dictionary}
- * interface. A call to {@link Dictionary#get(Object)} invokes the getter for
+ * Exposes Java bean properties of an object via the {@link Map}
+ * interface. A call to {@link Map#get(Object)} invokes the getter for
* the corresponding property, and a call to
- * {@link Dictionary#put(Object, Object)} invokes the property's setter.
+ * {@link Map#put(Object, Object)} invokes the property's setter.
* <p>
* Properties may provide multiple setters; the appropriate setter to invoke
* is determined by the type of the value being set. If the value is
* <tt>null</tt>, the return type of the getter method is used.
*/
-public class BeanAdapter implements Dictionary<String, Object>, Iterable<String> {
+public class BeanAdapter implements Map<String, Object> {
/**
* Property iterator. Walks the list of methods defined by the bean and
* returns a value for each getter method.
@@ -133,6 +136,8 @@ public void remove() {
private Object bean;
private boolean ignoreReadOnlyProperties;
+ private MapListenerList<String, Object> mapListeners = new MapListenerList<String, Object>();
+
public static final String GET_PREFIX = "get";
public static final String IS_PREFIX = "is";
public static final String SET_PREFIX = "set";
@@ -312,7 +317,10 @@ public Object put(String key, Object value) {
}
}
- return null;
+ Object previousValue = null;
+ mapListeners.valueUpdated(this, key, previousValue);
+
+ return previousValue;
}
/**
@@ -324,6 +332,15 @@ public Object remove(String key) {
throw new UnsupportedOperationException();
}
+ /**
+ * @throws UnsupportedOperationException
+ * This method is not supported.
+ */
+ @Override
+ public synchronized void clear() {
+ throw new UnsupportedOperationException();
+ }
+
/**
* Verifies the existence of a property. The property must have a getter
* method; write-only properties are not supported.
@@ -355,6 +372,38 @@ public boolean containsKey(String key) {
return containsKey;
}
+ /**
+ * @throws UnsupportedOperationException
+ * This method is not supported.
+ */
+ @Override
+ public boolean isEmpty() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * @throws UnsupportedOperationException
+ * This method is not supported.
+ */
+ @Override
+ public int getCount() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Comparator<String> getComparator() {
+ return null;
+ }
+
+ /**
+ * @throws UnsupportedOperationException
+ * This method is not supported.
+ */
+ @Override
+ public void setComparator(Comparator<String> comparator) {
+ throw new UnsupportedOperationException();
+ }
+
/**
* Tests the read-only state of a property.
*
@@ -404,6 +453,11 @@ public boolean isReadOnly(String key) {
return new PropertyIterator();
}
+ @Override
+ public ListenerList<MapListener<String, Object>> getMapListeners() {
+ return mapListeners;
+ }
+
/**
* Returns the getter method for a property.
*
@@ -417,6 +471,19 @@ private Method getGetterMethod(String key) {
return getGetterMethod(bean.getClass(), key);
}
+ /**
+ * Returns the setter method for a property.
+ *
+ * @param key
+ * The property name.
+ *
+ * @return
+ * The getter method, or <tt>null</tt> if the method does not exist.
+ */
+ private Method getSetterMethod(String key, Class<?> valueType) {
+ return getSetterMethod(bean.getClass(), key, valueType);
+ }
+
/**
* Returns the public, non-static field for a property. Note that fields
* will only be consulted for bean properties after bean methods.
@@ -432,19 +499,6 @@ private Field getField(String fieldName) {
return getField(bean.getClass(), fieldName);
}
- /**
- * Returns the setter method for a property.
- *
- * @param key
- * The property name.
- *
- * @return
- * The getter method, or <tt>null</tt> if the method does not exist.
- */
- private Method getSetterMethod(String key, Class<?> valueType) {
- return getSetterMethod(bean.getClass(), key, valueType);
- }
-
/**
* Tests the read-only state of a property. Note that is no such property
* exists, this method will return <tt>true</tt> (it will <u>not</u> throw
@@ -109,11 +109,6 @@ public synchronized V remove(K key) {
return value;
}
- @Override
- public synchronized boolean isEmpty() {
- return map.isEmpty();
- }
-
@Override
public synchronized void clear() {
if (!map.isEmpty()) {
@@ -127,6 +122,11 @@ public synchronized boolean containsKey(K key) {
return map.containsKey(key);
}
+ @Override
+ public synchronized boolean isEmpty() {
+ return map.isEmpty();
+ }
+
@Override
public synchronized int getCount() {
return map.getCount();
@@ -145,7 +145,7 @@ public boolean isNotifying(String key) {
* events.
*/
private void registerBeanListeners() {
- BeanAdapter beanDictionary = new BeanAdapter(bean);
+ BeanAdapter beanAdapter = new BeanAdapter(bean);
Method[] methods = bean.getClass().getMethods();
for (int i = 0; i < methods.length; i++) {
@@ -173,7 +173,7 @@ private void registerBeanListeners() {
String propertyName = interfaceMethodName.substring(0,
interfaceMethodName.length() - PROPERTY_CHANGE_SUFFIX.length());
- if (beanDictionary.containsKey(propertyName)) {
+ if (beanAdapter.containsKey(propertyName)) {
notifyingProperties.add(propertyName);
}
}
@@ -60,9 +60,9 @@ public ComponentPropertyInspectorSkin() {
beanMonitor.getPropertyChangeListeners().add(new PropertyChangeListener() {
@Override
public void propertyChanged(Object bean, String propertyName) {
- BeanAdapter beanDictionary = new BeanAdapter(bean);
- Class<?> type = beanDictionary.getType(propertyName);
- updateControl(beanDictionary, propertyName, type);
+ BeanAdapter beanAdapter = new BeanAdapter(bean);
+ Class<?> type = beanAdapter.getType(propertyName);
+ updateControl(beanAdapter, propertyName, type);
}
});
@@ -85,10 +85,10 @@ public void sourceChanged(ComponentInspector componentInspector, Component previ
new HashMap<Class<?>, List<String>>(classComparator);
// Partition the properties by their declaring class
- BeanAdapter beanDictionary = new BeanAdapter(source);
- for (String propertyName : beanDictionary) {
+ BeanAdapter beanAdapter = new BeanAdapter(source);
+ for (String propertyName : beanAdapter) {
if (beanMonitor.isNotifying(propertyName)
- && !beanDictionary.isReadOnly(propertyName)) {
+ && !beanAdapter.isReadOnly(propertyName)) {
Method method = BeanAdapter.getGetterMethod(sourceType, propertyName);
Class<?> declaringClass = method.getDeclaringClass();
@@ -110,8 +110,8 @@ public void sourceChanged(ComponentInspector componentInspector, Component previ
List<String> propertyNames = declaringClassPartitions.get(declaringClass);
for (String propertyName : propertyNames) {
- Class<?> type = beanDictionary.getType(propertyName);
- addControl(beanDictionary, propertyName, type, section);
+ Class<?> type = beanAdapter.getType(propertyName);
+ addControl(beanAdapter, propertyName, type, section);
}
}
}
@@ -214,9 +214,9 @@ public void editRow(TableView tableView, int rowIndex, int columnIndex) {
if (tableRow instanceof Dictionary<?, ?>) {
rowData = (Dictionary<String, Object>)tableRow;
} else {
- BeanAdapter beanDictionary = new BeanAdapter(tableRow);
- isReadOnly = beanDictionary.isReadOnly(columnName);
- rowData = beanDictionary;
+ BeanAdapter beanAdapter = new BeanAdapter(tableRow);
+ isReadOnly = beanAdapter.isReadOnly(columnName);
+ rowData = beanAdapter;
}
if (!isReadOnly) {
@@ -126,12 +126,12 @@ public EditorPopup(TableView tableView, int rowIndex, int columnIndex) {
// Get the row data, represented as a Dictionary
Object tableRow = tableView.getTableData().get(rowIndex);
Dictionary<String, Object> rowData;
- BeanAdapter beanDictionary = null;
+ BeanAdapter beanAdapter = null;
if (tableRow instanceof Dictionary<?, ?>) {
rowData = (Dictionary<String, Object>)tableRow;
} else {
- beanDictionary = new BeanAdapter(tableRow);
- rowData = beanDictionary;
+ beanAdapter = new BeanAdapter(tableRow);
+ rowData = beanAdapter;
}
// Set up the editor component hierarchy
@@ -178,8 +178,8 @@ public EditorPopup(TableView tableView, int rowIndex, int columnIndex) {
editorComponent = editorTextInput;
// Disable the text input for read-only properties
- if (beanDictionary != null
- && beanDictionary.isReadOnly(columnName)) {
+ if (beanAdapter != null
+ && beanAdapter.isReadOnly(columnName)) {
editorTextInput.setTextBindType(BindType.LOAD);
editorTextInput.setEnabled(false);
}
@@ -630,11 +630,11 @@ private void processStartElement(XMLStreamReader xmlStreamReader) throws Seriali
throw new SerializationException("Property elements cannot have a namespace prefix.");
}
- BeanAdapter beanDictionary = new BeanAdapter(element.value);
+ BeanAdapter beanAdapter = new BeanAdapter(element.value);
- if (beanDictionary.isReadOnly(localName)) {
+ if (beanAdapter.isReadOnly(localName)) {
elementType = Element.Type.READ_ONLY_PROPERTY;
- value = beanDictionary.get(localName);
+ value = beanAdapter.get(localName);
assert (value != null) : "Read-only properties cannot be null.";
if (attributes.getLength() > 0
@@ -921,8 +921,8 @@ private void processEndElement(XMLStreamReader xmlStreamReader)
}
case WRITABLE_PROPERTY: {
- BeanAdapter beanDictionary = new BeanAdapter(element.parent.value);
- beanDictionary.put(localName, element.value);
+ BeanAdapter beanAdapter = new BeanAdapter(element.parent.value);
+ beanAdapter.put(localName, element.value);
break;
}

0 comments on commit d878243

Please sign in to comment.