use of com.haulmont.cuba.gui.components.ListEditor in project cuba by cuba-platform.
the class DynamicAttributeCustomFieldGenerator method generateField.
@Override
public Component generateField(Datasource datasource, String propertyId) {
ComponentsFactory componentsFactory = AppBeans.get(ComponentsFactory.class);
ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
MetaPropertyPath metaPropertyPath = DynamicAttributesUtils.getMetaPropertyPath(datasource.getMetaClass(), propertyId);
if (metaPropertyPath == null) {
log.error("MetaPropertyPath for dynamic attribute {} not found", propertyId);
return null;
}
CategoryAttribute categoryAttribute = DynamicAttributesUtils.getCategoryAttribute(metaPropertyPath.getMetaProperty());
if (categoryAttribute == null) {
log.error("Dynamic attribute {} not found", propertyId);
return null;
}
listEditor.setEntityJoinClause(categoryAttribute.getJoinClause());
listEditor.setEntityWhereClause(categoryAttribute.getWhereClause());
ListEditor.ItemType itemType = listEditorItemTypeFromDynamicAttrType(categoryAttribute.getDataType());
listEditor.setItemType(itemType);
Metadata metadata = AppBeans.get(Metadata.class);
Scripting scripting = AppBeans.get(Scripting.class);
if (!Strings.isNullOrEmpty(categoryAttribute.getEntityClass())) {
Class<?> clazz = scripting.loadClass(categoryAttribute.getEntityClass());
if (clazz == null) {
log.error("Unable to find class of entity {} for dynamic attribute {}", categoryAttribute.getEntityClass(), categoryAttribute.getCode());
return null;
}
MetaClass metaClass = metadata.getClassNN(clazz);
listEditor.setEntityName(metaClass.getName());
listEditor.setUseLookupField(BooleanUtils.isTrue(categoryAttribute.getLookup()));
}
// noinspection unchecked
datasource.addStateChangeListener(e -> {
if (e.getState() == Datasource.State.VALID) {
Object value = datasource.getItem().getValue(propertyId);
if (value != null && value instanceof Collection) {
listEditor.setValue(value);
}
}
});
listEditor.addValueChangeListener(e -> {
datasource.getItem().setValue(propertyId, e.getValue());
});
listEditor.setWidthFull();
return listEditor;
}
Aggregations