use of com.haulmont.cuba.gui.components.data.value.DatasourceValueSource in project cuba by cuba-platform.
the class DynamicAttributesGuiTools method getValueChangeEventListener.
/**
* Returns {@code ValueChangeEventListener} for dynamic attribute that has one or more dependent attributes.
* This listener recalculates values for all dependent dynamic attributes hierarchically. The listener uses
* {@code recalculationInProgress} ThreadLocal variable to avoid unnecessary calculation.
*/
@SuppressWarnings("unchecked")
public Consumer<HasValue.ValueChangeEvent> getValueChangeEventListener(final CategoryAttribute attribute) {
if (attribute.getConfiguration().getDependentAttributes() != null && !attribute.getConfiguration().getDependentAttributes().isEmpty()) {
return valueChangeEvent -> {
if (Boolean.TRUE.equals(recalculationInProgress.get())) {
return;
}
try {
recalculationInProgress.set(true);
com.haulmont.cuba.gui.components.Component component = valueChangeEvent.getComponent();
if (component instanceof HasValueSource) {
{
BaseGenericIdEntity entity = null;
String attributeCode = DynamicAttributesUtils.encodeAttributeCode(attribute.getCode());
if (((HasValueSource) component).getValueSource() instanceof ContainerValueSource) {
ContainerValueSource valueSource = (ContainerValueSource) ((HasValueSource) component).getValueSource();
InstanceContainer container = valueSource.getContainer();
if (container.getItem() instanceof BaseGenericIdEntity) {
entity = (BaseGenericIdEntity) container.getItem();
}
} else if (((HasValueSource) component).getValueSource() instanceof DatasourceValueSource) {
DatasourceValueSource valueSource = (DatasourceValueSource) ((HasValueSource) component).getValueSource();
if (valueSource.getItem() instanceof BaseGenericIdEntity) {
entity = (BaseGenericIdEntity) valueSource.getItem();
} else if (valueSource.getItem() instanceof DynamicAttributesEntity) {
entity = ((DynamicAttributesEntity) valueSource.getItem()).getMainItem();
}
}
entity.setValue(attributeCode, valueChangeEvent.getValue());
recalculationTools.recalculateDynamicAttributes(entity, attribute);
}
}
} finally {
recalculationInProgress.remove();
}
};
}
return null;
}
use of com.haulmont.cuba.gui.components.data.value.DatasourceValueSource in project cuba by cuba-platform.
the class FieldGroupFieldFactoryImpl method createFieldComponent.
protected GeneratedField createFieldComponent(FieldGroup.FieldConfig fc) {
MetaClass metaClass = resolveMetaClass(fc.getTargetDatasource());
if (DynamicAttributesUtils.isDynamicAttribute(fc.getProperty())) {
CategoryAttribute attribute = dynamicAttributes.getAttributeForMetaClass(metaClass, fc.getProperty());
if (attribute != null && BooleanUtils.isTrue(attribute.getIsCollection())) {
// noinspection unchecked
DatasourceValueSource valueSource = new DatasourceValueSource(fc.getTargetDatasource(), fc.getProperty());
Component fieldComponent = dynamicAttributeComponentsGenerator.generateComponent(valueSource, attribute);
return new GeneratedField(fieldComponent);
}
}
ComponentGenerationContext context = new ComponentGenerationContext(metaClass, fc.getProperty()).setDatasource(fc.getTargetDatasource()).setOptionsDatasource(fc.getOptionsDatasource()).setXmlDescriptor(fc.getXmlDescriptor()).setComponentClass(FieldGroup.class);
return new GeneratedField(uiComponentsGenerator.generate(context));
}
use of com.haulmont.cuba.gui.components.data.value.DatasourceValueSource in project cuba by cuba-platform.
the class AttributeAccessSupport method visitComponent.
protected void visitComponent(com.haulmont.cuba.gui.components.Component component, boolean reset) {
if (!(component instanceof HasValueSource)) {
return;
}
ValueSource valueSource = ((HasValueSource) component).getValueSource();
if (!(valueSource instanceof EntityValueSource)) {
return;
}
EntityValueSource entityValueSource = (EntityValueSource) valueSource;
MetaPropertyPath propertyPath = entityValueSource.getMetaPropertyPath();
if (valueSource.getState() != BindingState.ACTIVE || propertyPath == null) {
return;
}
if (reset) {
component.setVisible(security.isEntityAttrReadPermitted(entityValueSource.getEntityMetaClass(), propertyPath.toString()));
if (component instanceof Editable) {
((Editable) component).setEditable(security.isEntityAttrUpdatePermitted(entityValueSource.getEntityMetaClass(), propertyPath.toString()));
}
if (component instanceof Field) {
((Field) component).setRequired(propertyPath.getMetaProperty().isMandatory());
}
}
Entity item = entityValueSource.getItem();
ComponentState componentState = calculateComponentState(item, propertyPath);
if (metadataTools.isEmbeddable(item.getMetaClass()) && entityValueSource instanceof DatasourceValueSource) {
Datasource ds = ((DatasourceValueSource) entityValueSource).getDatasource();
if (ds instanceof EmbeddedDatasource) {
Datasource masterDs = ((EmbeddedDatasource) ds).getMaster();
item = masterDs.getItem();
componentState = calculateComponentState(item, metadataTools.resolveMetaPropertyPath(masterDs.getMetaClass(), ((EmbeddedDatasource) ds).getProperty().getName() + "." + propertyPath));
}
}
if (componentState.hidden) {
component.setVisible(false);
}
if (componentState.readOnly) {
if (component instanceof Editable) {
((Editable) component).setEditable(false);
}
}
if (component instanceof Field) {
if (componentState.required && ((Field) component).isEditable() && component.isVisibleRecursive()) {
((Field) component).setRequired(true);
}
}
}
use of com.haulmont.cuba.gui.components.data.value.DatasourceValueSource in project cuba by cuba-platform.
the class AttributeEditor method initCalculatedAttrsAndOptionsFieldGroup.
protected void initCalculatedAttrsAndOptionsFieldGroup() {
recalculationScript.setHeight(themeConstants.get("cuba.gui.AttributeEditor.recalculationGroovyScriptField.height"));
recalculationScript.setContextHelpIconClickHandler(e -> showMessageDialog(getMessage("recalculationScript"), getMessage("recalculationScriptHelp"), MessageType.CONFIRMATION_HTML.modal(false).width(560f)));
dependsOnAttributesListEditor = uiComponents.create(ListEditor.NAME);
dependsOnAttributesListEditor.setValueSource(new DatasourceValueSource(configurationDs, "dependsOnAttributes"));
dependsOnAttributesListEditor.setWidth(fieldWidth);
dependsOnAttributesListEditor.setFrame(frame);
dependsOnAttributesListEditor.setItemType(ListEditor.ItemType.ENTITY);
dependsOnAttributesListEditor.setEntityName("sys$CategoryAttribute");
dependsOnAttributesListEditor.addValidator(categoryAttributes -> {
if (recalculationScript.getValue() != null && CollectionUtils.isEmpty(categoryAttributes)) {
throw new ValidationException(getMessage("dependsOnAttributesValidationMsg"));
}
});
calculatedAttrsAndOptionsFieldGroup.getFieldNN("dependsOnAttributes").setComponent(dependsOnAttributesListEditor);
whereClause.setSuggester((source, text, cursorPosition) -> requestHint(whereClause, cursorPosition));
whereClause.setHeight(themeConstants.get("cuba.gui.customConditionFrame.whereField.height"));
joinClause.setSuggester((source, text, cursorPosition) -> requestHint(joinClause, cursorPosition));
joinClause.setHeight(themeConstants.get("cuba.gui.customConditionFrame.joinField.height"));
}
Aggregations