blob: 17445d298efd5bad2e03f2c888e92f9a855d30c7 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008 Matthew Hall and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Matthew Hall - initial API and implementation (bug 194734)
* Matthew Hall - bug 195222
******************************************************************************/
package org.eclipse.core.internal.databinding.beans;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.databinding.observable.map.MapDiff;
import org.eclipse.core.databinding.property.INativePropertyListener;
import org.eclipse.core.databinding.property.ISimplePropertyListener;
import org.eclipse.core.databinding.property.map.SimpleMapProperty;
/**
* @since 3.3
*
*/
public class PojoMapProperty extends SimpleMapProperty {
private final PropertyDescriptor propertyDescriptor;
private final Class keyType;
private final Class valueType;
/**
* @param propertyDescriptor
* @param keyType
* @param valueType
*/
public PojoMapProperty(PropertyDescriptor propertyDescriptor,
Class keyType, Class valueType) {
this.propertyDescriptor = propertyDescriptor;
this.keyType = keyType;
this.valueType = valueType;
}
public Object getKeyType() {
return keyType;
}
public Object getValueType() {
return valueType;
}
protected Map doGetMap(Object source) {
return asMap(BeanPropertyHelper
.readProperty(source, propertyDescriptor));
}
private Map asMap(Object propertyValue) {
if (propertyValue == null)
return new HashMap();
return (Map) propertyValue;
}
protected void doSetMap(Object source, Map map, MapDiff diff) {
BeanPropertyHelper.writeProperty(source, propertyDescriptor, map);
}
public INativePropertyListener adaptListener(
ISimplePropertyListener listener) {
return null;
}
public void doAddListener(Object source, INativePropertyListener listener) {
}
public void doRemoveListener(Object source, INativePropertyListener listener) {
}
public String toString() {
Class beanClass = propertyDescriptor.getReadMethod()
.getDeclaringClass();
String propertyName = propertyDescriptor.getName();
String s = beanClass.getName() + "." + propertyName + "{:}"; //$NON-NLS-1$ //$NON-NLS-2$
if (keyType != null || valueType != null) {
s += " <" + keyType + ", " + valueType + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
}
return s;
}
}