//
// Add to default property identified by the type
//
IMetaclass parentMetaclass = XWT.getMetaclass(parent);
IProperty[] properties = parentMetaclass.getProperties();
IProperty useProperty = null;
int count = 0;
Class<?> childType = childElement.getClass();
for (IProperty property : properties) {
Class<?> propertyType = property.getType();
if (propertyType == null || propertyType == Object.class) {
continue;
}
if (property.isContainement()) {
useProperty = property;
count++;
}
}
if (count > 1) {
StringBuilder builder = new StringBuilder();
builder.append("Class has more containment properties: ");
count = 0;
for (IProperty property : properties) {
Class<?> propertyType = property.getType();
if (propertyType == null || propertyType == Object.class) {
continue;
}
if (property.isContainement()) {
if (count != 0) {
builder.append(", ");
}
builder.append(property.getName());
count++;
}
}
throw new XWTException(
"Class has more containment properties: ");
}
if (count == 0) {
for (IProperty property : properties) {
Class<?> propertyType = property.getType();
if (propertyType == null || propertyType == Object.class) {
continue;
}
if (propertyType.isArray()) {
Class<?> dataType = propertyType.getComponentType();
if (dataType.isAssignableFrom(childType)) {
if (useProperty == null) {
useProperty = property;
}
count++;
}
} else if (Collection.class.isAssignableFrom(propertyType)) {
if (useProperty == null) {
useProperty = property;
}
count++;
} else if (propertyType.isAssignableFrom(childType)) {
if (useProperty == null) {
useProperty = property;
}
count++;
}
}
}
if (count == 1) {
Class<?> propertyType = useProperty.getType();
if (propertyType.isArray()) {
Object[] existingValue = (Object[]) useProperty
.getValue(parent);
Class<?> dataType = propertyType.getComponentType();
Object[] value = null;
if (existingValue == null) {
value = (Object[]) Array.newInstance(dataType, 1);
value[0] = childElement;
} else {
value = (Object[]) Array.newInstance(dataType,
existingValue.length + 1);
System.arraycopy(existingValue, 0, value, 0,
existingValue.length);
value[existingValue.length] = childElement;
}
useProperty.setValue(parent, value);
} else if (Collection.class.isAssignableFrom(propertyType)
&& !(childElement instanceof IBinding)) {
Collection existingValue = (Collection) useProperty
.getValue(parent);
if (existingValue == null) {
existingValue = new ArrayList();
}
existingValue.add(childElement);
useProperty.setValue(parent, existingValue);
} else if (propertyType.isAssignableFrom(childType)) {
useProperty.setValue(parent, childElement);
}
}
}
}