Search in sources :

Example 1 with BindableProxy

use of org.jboss.errai.databinding.client.BindableProxy in project kie-wb-common by kiegroup.

the class FormDisplayer method doRender.

private void doRender(Element<? extends Definition<?>> element, Object definition, Path diagramPath, FieldChangeHandler changeHandler) {
    if (renderer.isInitialized()) {
        LOGGER.fine("Clearing previous form");
        renderer.unBind();
    }
    LOGGER.fine("Rendering a new form for element");
    Collection<FormElementFilter> filters = FormFiltersProviderFactory.getFilterForDefinition(element.getUUID(), element, definition);
    final BindableProxy<?> proxy = (BindableProxy<?>) BindableProxyFactory.getBindableProxy(definition);
    final StaticModelFormRenderingContext generatedCtx = modelGenerator.getContextForModel(proxy.deepUnwrap(), filters.stream().toArray(FormElementFilter[]::new));
    final FormRenderingContext<?> pathAwareCtx = new PathAwareFormContext<>(generatedCtx, diagramPath);
    renderer.render(pathAwareCtx);
    renderer.addFieldChangeHandler(changeHandler);
}
Also used : BindableProxy(org.jboss.errai.databinding.client.BindableProxy) StaticModelFormRenderingContext(org.kie.workbench.common.forms.dynamic.service.shared.impl.StaticModelFormRenderingContext) FormElementFilter(org.kie.workbench.common.forms.adf.engine.shared.FormElementFilter) PathAwareFormContext(org.kie.workbench.common.stunner.forms.context.PathAwareFormContext)

Example 2 with BindableProxy

use of org.jboss.errai.databinding.client.BindableProxy in project kie-wb-common by kiegroup.

the class DefaultModelValidator method validate.

@Override
public boolean validate(Collection<FormField> fields, MODEL model) {
    boolean isValid = true;
    if (model instanceof BindableProxy) {
        model = ((BindableProxy<MODEL>) model).deepUnwrap();
    }
    try {
        Set<ConstraintViolation<Object>> result = validator.validate(model);
        for (ConstraintViolation<Object> constraintViolation : result) {
            Optional<FormField> found = fields.stream().filter(formField -> {
                Path propertyPath = constraintViolation.getPropertyPath();
                String propertyName = propertyPath.iterator().next().getName();
                if (checkBinding(formField, propertyName)) {
                    return true;
                }
                propertyName = propertyPath.toString().replace(".", NESTED_PROPERTY_SEPARATOR);
                if (checkBinding(formField, propertyName)) {
                    return true;
                }
                return false;
            }).findFirst();
            if (!found.isPresent()) {
                continue;
            }
            FormField formField = found.get();
            if (formField == null) {
                continue;
            }
            isValid = false;
            formField.showError(constraintViolation.getMessage());
        }
    } catch (IllegalArgumentException ex) {
        GWT.log("Error trying to validate model: model does not any validation constraint. ");
    }
    return isValid;
}
Also used : NESTED_PROPERTY_SEPARATOR(org.kie.workbench.common.forms.processing.engine.handling.FormValidator.NESTED_PROPERTY_SEPARATOR) Default(javax.enterprise.inject.Default) FormField(org.kie.workbench.common.forms.processing.engine.handling.FormField) Collection(java.util.Collection) Set(java.util.Set) Validator(javax.validation.Validator) GWT(com.google.gwt.core.client.GWT) Inject(javax.inject.Inject) ModelValidator(org.kie.workbench.common.forms.processing.engine.handling.ModelValidator) Dependent(javax.enterprise.context.Dependent) Optional(java.util.Optional) Path(javax.validation.Path) BindableProxy(org.jboss.errai.databinding.client.BindableProxy) ConstraintViolation(javax.validation.ConstraintViolation) Path(javax.validation.Path) BindableProxy(org.jboss.errai.databinding.client.BindableProxy) ConstraintViolation(javax.validation.ConstraintViolation) FormField(org.kie.workbench.common.forms.processing.engine.handling.FormField)

Example 3 with BindableProxy

use of org.jboss.errai.databinding.client.BindableProxy in project kie-wb-common by kiegroup.

the class MultipleSubFormWidget method initCrud.

protected void initCrud() {
    final List<ColumnMeta> metas = new ArrayList<>();
    HasProperties hasProperties = null;
    try {
        hasProperties = bindingHelper.getProxyDefinition();
    } catch (Exception e) {
        GWT.log("Unable to find proxy: " + e.getMessage());
    }
    for (TableColumnMeta meta : field.getColumnMetas()) {
        String type = String.class.getName();
        if (hasProperties != null) {
            type = hasProperties.getBeanProperties().get(meta.getProperty()).getType().getName();
        }
        ColumnGenerator generator = columnGeneratorManager.getGeneratorByType(type);
        if (generator != null) {
            ColumnMeta<HasProperties> columnMeta = new ColumnMeta<HasProperties>(generator.getColumn(meta.getProperty()), meta.getLabel());
            metas.add(columnMeta);
        }
    }
    dataProvider = new AsyncDataProvider<HasProperties>() {

        @Override
        protected void onRangeChanged(HasData<HasProperties> hasData) {
            if (tableValues != null) {
                updateRowCount(tableValues.size(), true);
                updateRowData(0, tableValues);
            } else {
                updateRowCount(0, true);
                updateRowData(0, new ArrayList<HasProperties>());
            }
        }
    };
    crudComponent.init(new CrudActionsHelper() {

        @Override
        public int getPageSize() {
            return PAGE_SIZE;
        }

        @Override
        public boolean showEmbeddedForms() {
            return true;
        }

        @Override
        public boolean isAllowCreate() {
            return !isReadOnly;
        }

        @Override
        public boolean isAllowEdit() {
            return !isReadOnly;
        }

        @Override
        public boolean isAllowDelete() {
            return !isReadOnly;
        }

        @Override
        public List<ColumnMeta> getGridColumns() {
            return metas;
        }

        @Override
        public AsyncDataProvider getDataProvider() {
            return dataProvider;
        }

        public IsFormView<Object> getCreateInstanceForm() {
            if (field.getCreationForm() != null) {
                BindableProxy<?> proxy = bindingHelper.getNewProxy();
                formRenderer.render(renderingContext.getCopyFor(field.getCreationForm(), proxy));
                return formRenderer;
            }
            return null;
        }

        public IsFormView<Object> getEditInstanceForm(int position) {
            if (field.getEditionForm() != null) {
                BindableProxy instanceProxy = bindingHelper.getProxyForModel(values.get(position));
                BindableProxy editionProxy = bindingHelper.getProxyForModel(instanceProxy.deepUnwrap());
                formRenderer.render(renderingContext.getCopyFor(field.getEditionForm(), editionProxy));
                return formRenderer;
            }
            return null;
        }

        @Override
        public void createInstance() {
            IsFormView form = getCreateInstanceForm();
            crudComponent.displayForm(translationService.getTranslation(CrudComponentConstants.CrudComponentViewImplNewInstanceTitle), form, new FormDisplayer.FormDisplayerCallback() {

                @Override
                public void onCancel() {
                    formRenderer.unBind();
                }

                @Override
                public void onAccept() {
                    if (values == null) {
                        values = new ArrayList<>();
                    }
                    values.add(formRenderer.getModel());
                    tableValues.add((HasProperties) formRenderer.getModel());
                    refreshCrud();
                    fireFieldChange();
                    formRenderer.unBind();
                }
            });
        }

        @Override
        public void editInstance(int index) {
            IsFormView form = getEditInstanceForm(index);
            crudComponent.displayForm(translationService.getTranslation(CrudComponentConstants.CrudComponentViewImplEditInstanceTitle), form, new FormDisplayer.FormDisplayerCallback() {

                @Override
                public void onCancel() {
                    formRenderer.unBind();
                }

                @Override
                public void onAccept() {
                    BindableProxy editionProxy = (BindableProxy) formRenderer.getModel();
                    BindableProxy realProxy = bindingHelper.getProxyForModel(values.get(index));
                    FormDefinition editionform = (FormDefinition) renderingContext.getAvailableForms().get(field.getEditionForm());
                    editionform.getFields().forEach(fieldDefinition -> {
                        if (fieldDefinition.getBinding() != null) {
                            realProxy.set(fieldDefinition.getBinding(), editionProxy.get(fieldDefinition.getBinding()));
                        }
                    });
                    bindingHelper.afterEdit(realProxy);
                    values.set(index, realProxy);
                    tableValues.set(index, realProxy);
                    refreshCrud();
                    fireFieldChange();
                    formRenderer.unBind();
                }
            });
        }

        @Override
        public void deleteInstance(int index) {
            values.remove(index);
            tableValues.remove(index);
            refreshCrud();
            fireFieldChange();
        }
    });
    initValues();
}
Also used : TableColumnMeta(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.TableColumnMeta) BindableProxy(org.jboss.errai.databinding.client.BindableProxy) ArrayList(java.util.ArrayList) HasProperties(org.jboss.errai.databinding.client.HasProperties) CrudActionsHelper(org.kie.workbench.common.forms.crud.client.component.CrudActionsHelper) TableColumnMeta(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.TableColumnMeta) ColumnMeta(org.uberfire.ext.widgets.table.client.ColumnMeta) ColumnGenerator(org.kie.workbench.common.forms.dynamic.client.rendering.renderers.relations.multipleSubform.columns.ColumnGenerator) IsFormView(org.kie.workbench.common.forms.crud.client.component.formDisplay.IsFormView) AsyncDataProvider(com.google.gwt.view.client.AsyncDataProvider) ArrayList(java.util.ArrayList) List(java.util.List) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition)

Example 4 with BindableProxy

use of org.jboss.errai.databinding.client.BindableProxy in project kie-wb-common by kiegroup.

the class FormHandlerImpl method registerInput.

@Override
public void registerInput(FormField formField, Converter valueConverter) {
    Assert.notNull("FormHandler isn't correctly initialized, please run any of the setUp methods before use", binder);
    Assert.notNull("FormField cannot be null!", formField);
    String fieldName = formField.getFieldName();
    IsWidget widget = formField.getWidget();
    form.addField(formField);
    if (formField.isBindable()) {
        BindableProxy proxy = (BindableProxy) binder.getModel();
        Object modelValue = readPropertyValue(proxy, formField.getFieldBinding());
        StateSync stateSync = Optional.ofNullable(modelValue).isPresent() ? StateSync.FROM_MODEL : StateSync.FROM_UI;
        binder.bind(widget, formField.getFieldBinding(), valueConverter, stateSync);
    }
    fieldChangeManager.registerField(formField);
    formField.getChangeListeners().forEach(listener -> fieldChangeManager.addFieldChangeHandler(listener.getFieldToListen(), listener.getChangeHandler()));
    /**
     * if field isn't bindable we cannot listen to field value changes.
     */
    if (!formField.isBindable()) {
        return;
    }
    if (widget instanceof IsNestedModel) {
        IsNestedModel nestedModelWidget = (IsNestedModel) widget;
        nestedModelWidget.addFieldChangeHandler((childFieldName, newValue) -> fieldChangeManager.notifyFieldChange(fieldName + "." + childFieldName, newValue));
    } else {
        PropertyChangeUnsubscribeHandle unsubscribeHandle = binder.addPropertyChangeHandler(formField.getFieldBinding(), event -> fieldChangeManager.processFieldChange(fieldName, event.getNewValue(), binder.getModel()));
        unsubscribeHandlers.add(unsubscribeHandle);
    }
}
Also used : IsWidget(com.google.gwt.user.client.ui.IsWidget) PropertyChangeUnsubscribeHandle(org.jboss.errai.databinding.client.PropertyChangeUnsubscribeHandle) StateSync(org.jboss.errai.databinding.client.api.StateSync) BindableProxy(org.jboss.errai.databinding.client.BindableProxy) IsNestedModel(org.kie.workbench.common.forms.processing.engine.handling.IsNestedModel)

Aggregations

BindableProxy (org.jboss.errai.databinding.client.BindableProxy)4 GWT (com.google.gwt.core.client.GWT)1 IsWidget (com.google.gwt.user.client.ui.IsWidget)1 AsyncDataProvider (com.google.gwt.view.client.AsyncDataProvider)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Dependent (javax.enterprise.context.Dependent)1 Default (javax.enterprise.inject.Default)1 Inject (javax.inject.Inject)1 ConstraintViolation (javax.validation.ConstraintViolation)1 Path (javax.validation.Path)1 Validator (javax.validation.Validator)1 HasProperties (org.jboss.errai.databinding.client.HasProperties)1 PropertyChangeUnsubscribeHandle (org.jboss.errai.databinding.client.PropertyChangeUnsubscribeHandle)1 StateSync (org.jboss.errai.databinding.client.api.StateSync)1 FormElementFilter (org.kie.workbench.common.forms.adf.engine.shared.FormElementFilter)1 CrudActionsHelper (org.kie.workbench.common.forms.crud.client.component.CrudActionsHelper)1