Search in sources :

Example 1 with CrudActionsHelper

use of org.kie.workbench.common.forms.crud.client.component.CrudActionsHelper 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)

Aggregations

AsyncDataProvider (com.google.gwt.view.client.AsyncDataProvider)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BindableProxy (org.jboss.errai.databinding.client.BindableProxy)1 HasProperties (org.jboss.errai.databinding.client.HasProperties)1 CrudActionsHelper (org.kie.workbench.common.forms.crud.client.component.CrudActionsHelper)1 IsFormView (org.kie.workbench.common.forms.crud.client.component.formDisplay.IsFormView)1 ColumnGenerator (org.kie.workbench.common.forms.dynamic.client.rendering.renderers.relations.multipleSubform.columns.ColumnGenerator)1 TableColumnMeta (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.TableColumnMeta)1 FormDefinition (org.kie.workbench.common.forms.model.FormDefinition)1 ColumnMeta (org.uberfire.ext.widgets.table.client.ColumnMeta)1