use of com.haulmont.cuba.gui.components.data.options.ListOptions in project cuba by cuba-platform.
the class AbstractComponentGenerationStrategy method setOptionsLoader.
protected void setOptionsLoader(CategoryAttribute categoryAttribute, LookupField lookupField, ContainerValueSource valueSource) {
InstanceContainer<?> container = valueSource.getContainer();
Entity entity = container.getItemOrNull();
if (entity != null) {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) entity, categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
}
container.addItemChangeListener(e -> {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
});
List<CategoryAttribute> dependsOnAttributes = categoryAttribute.getConfiguration().getDependsOnAttributes();
if (dependsOnAttributes != null && !dependsOnAttributes.isEmpty()) {
List<String> dependsOnAttributesCodes = dependsOnAttributes.stream().map(a -> DynamicAttributesUtils.encodeAttributeCode(a.getCode())).collect(Collectors.toList());
container.addItemPropertyChangeListener(e -> {
if (dependsOnAttributesCodes.contains(e.getProperty())) {
List options = dynamicAttributesTools.loadOptions((BaseGenericIdEntity) e.getItem(), categoryAttribute);
// noinspection unchecked
lookupField.setOptions(new ListOptions(options));
if (!options.contains(lookupField.getValue())) {
// noinspection unchecked
lookupField.setValue(null);
}
}
});
}
}
Aggregations