Search in sources :

Example 1 with DynamicAttributesGuiTools

use of com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools in project cuba by cuba-platform.

the class DataGridEditorComponentGenerationStrategy method createEntityField.

@Override
protected Field createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
    CollectionDatasource optionsDatasource = null;
    if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
        DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
        CategoryAttribute attribute = metaProperty.getAttribute();
        if (Boolean.TRUE.equals(attribute.getLookup())) {
            DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
            optionsDatasource = dynamicAttributesGuiTools.createOptionsDatasourceForLookup(metaProperty.getRange().asClass(), attribute.getJoinClause(), attribute.getWhereClause());
        }
    }
    PickerField pickerField;
    if (optionsDatasource == null) {
        pickerField = componentsFactory.createComponent(PickerField.class);
        setDatasource(pickerField, context);
        pickerField.addLookupAction();
        if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
            DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
            DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
            dynamicAttributesGuiTools.initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
        }
        PickerField.LookupAction lookupAction = (PickerField.LookupAction) pickerField.getActionNN(PickerField.LookupAction.NAME);
        // Opening lookup screen in another mode will close editor
        lookupAction.setLookupScreenOpenType(WindowManager.OpenType.DIALOG);
        // In case of adding special logic for lookup screen opened from DataGrid editor
        lookupAction.setLookupScreenParams(ParamsMap.of("dataGridEditor", true));
        boolean actionsByMetaAnnotations = ComponentsHelper.createActionsByMetaAnnotations(pickerField);
        if (!actionsByMetaAnnotations) {
            pickerField.addClearAction();
        }
    } else {
        LookupPickerField lookupPickerField = componentsFactory.createComponent(LookupPickerField.class);
        setDatasource(lookupPickerField, context);
        lookupPickerField.setOptionsDatasource(optionsDatasource);
        pickerField = lookupPickerField;
        ComponentsHelper.createActionsByMetaAnnotations(pickerField);
    }
    return pickerField;
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Example 2 with DynamicAttributesGuiTools

use of com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools in project cuba by cuba-platform.

the class EditAction method internalOpenEditor.

protected void internalOpenEditor(CollectionDatasource datasource, Entity existingItem, Datasource parentDs, Map<String, Object> params) {
    Window.Editor window = target.getFrame().openEditor(getWindowId(), existingItem, getOpenType(), params, parentDs);
    if (editorCloseListener == null) {
        window.addCloseListener(actionId -> {
            // move focus to owner
            target.requestFocus();
            if (Window.COMMIT_ACTION_ID.equals(actionId)) {
                Entity editedItem = window.getItem();
                if (editedItem != null) {
                    if (parentDs == null) {
                        if (editedItem instanceof BaseGenericIdEntity) {
                            BaseGenericIdEntity genericEditedEntity = (BaseGenericIdEntity) editedItem;
                            if (datasource.getLoadDynamicAttributes() && genericEditedEntity.getDynamicAttributes() == null) {
                                DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                                dynamicAttributesGuiTools.reloadDynamicAttributes(genericEditedEntity);
                            }
                        }
                        // noinspection unchecked
                        datasource.updateItem(editedItem);
                    }
                    afterCommit(editedItem);
                    if (afterCommitHandler != null) {
                        afterCommitHandler.handle(editedItem);
                    }
                }
            }
            afterWindowClosed(window);
            if (afterWindowClosedHandler != null) {
                afterWindowClosedHandler.handle(window, actionId);
            }
        });
    } else {
        window.addCloseListener(editorCloseListener);
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Example 3 with DynamicAttributesGuiTools

use of com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools in project cuba by cuba-platform.

the class AbstractComponentGenerationStrategy method createEntityField.

protected Component createEntityField(ComponentGenerationContext context, MetaPropertyPath mpp) {
    String linkAttribute = null;
    Element xmlDescriptor = context.getXmlDescriptor();
    if (xmlDescriptor != null) {
        linkAttribute = xmlDescriptor.attributeValue("link");
    }
    if (!Boolean.parseBoolean(linkAttribute)) {
        CollectionDatasource optionsDatasource = context.getOptionsDatasource();
        if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
            DynamicAttributesMetaProperty metaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
            CategoryAttribute attribute = metaProperty.getAttribute();
            if (Boolean.TRUE.equals(attribute.getLookup())) {
                DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                optionsDatasource = dynamicAttributesGuiTools.createOptionsDatasourceForLookup(metaProperty.getRange().asClass(), attribute.getJoinClause(), attribute.getWhereClause());
            }
        }
        PickerField pickerField;
        if (optionsDatasource == null) {
            pickerField = componentsFactory.createComponent(PickerField.class);
            setDatasource(pickerField, context);
            if (mpp.getMetaProperty().getType() == MetaProperty.Type.ASSOCIATION) {
                pickerField.addLookupAction();
                if (DynamicAttributesUtils.isDynamicAttribute(mpp.getMetaProperty())) {
                    DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                    DynamicAttributesMetaProperty dynamicAttributesMetaProperty = (DynamicAttributesMetaProperty) mpp.getMetaProperty();
                    dynamicAttributesGuiTools.initEntityPickerField(pickerField, dynamicAttributesMetaProperty.getAttribute());
                }
                boolean actionsByMetaAnnotations = ComponentsHelper.createActionsByMetaAnnotations(pickerField);
                if (!actionsByMetaAnnotations) {
                    pickerField.addClearAction();
                }
            } else {
                pickerField.addOpenAction();
                pickerField.addClearAction();
            }
        } else {
            LookupPickerField lookupPickerField = componentsFactory.createComponent(LookupPickerField.class);
            setDatasource(lookupPickerField, context);
            lookupPickerField.setOptionsDatasource(optionsDatasource);
            pickerField = lookupPickerField;
            ComponentsHelper.createActionsByMetaAnnotations(pickerField);
        }
        if (xmlDescriptor != null) {
            String captionProperty = xmlDescriptor.attributeValue("captionProperty");
            if (StringUtils.isNotEmpty(captionProperty)) {
                pickerField.setCaptionMode(CaptionMode.PROPERTY);
                pickerField.setCaptionProperty(captionProperty);
            }
        }
        return pickerField;
    } else {
        EntityLinkField linkField = componentsFactory.createComponent(EntityLinkField.class);
        setDatasource(linkField, context);
        setLinkFieldAttributes(linkField, context);
        return linkField;
    }
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) DynamicAttributesMetaProperty(com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Element(org.dom4j.Element) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Example 4 with DynamicAttributesGuiTools

use of com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools in project cuba by cuba-platform.

the class EditorWindowDelegate method setItem.

@SuppressWarnings("unchecked")
public void setItem(Entity item) {
    Datasource ds = getDatasource();
    DataSupplier dataservice = ds.getDataSupplier();
    DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent();
    DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.NAME);
    if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getWrapper().getId())) {
        ds.setLoadDynamicAttributes(true);
    }
    Class<? extends Entity> entityClass = item.getClass();
    Object entityId = item.getId();
    if (parentDs != null) {
        if (!PersistenceHelper.isNew(item) && !parentDs.getItemsToCreate().contains(item) && !parentDs.getItemsToUpdate().contains(item) && parentDs instanceof CollectionDatasource && ((CollectionDatasource) parentDs).containsItem(item.getId()) && !entityStates.isLoadedWithView(item, ds.getView())) {
            item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
            if (parentDs instanceof CollectionPropertyDatasourceImpl) {
                ((CollectionPropertyDatasourceImpl) parentDs).replaceItem(item);
            } else {
                ((CollectionDatasource) parentDs).updateItem(item);
            }
        }
        item = EntityCopyUtils.copyCompositions(item);
        handlePreviouslyDeletedCompositionItems(item, parentDs);
    } else if (!PersistenceHelper.isNew(item)) {
        item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
    }
    if (item == null) {
        throw new EntityAccessException(entityClass, entityId);
    }
    if (PersistenceHelper.isNew(item) && !ds.getMetaClass().equals(item.getMetaClass())) {
        Entity newItem = ds.getDataSupplier().newInstance(ds.getMetaClass());
        metadata.getTools().copy(item, newItem);
        item = newItem;
    }
    if (ds.getLoadDynamicAttributes() && item instanceof BaseGenericIdEntity) {
        if (PersistenceHelper.isNew(item)) {
            dynamicAttributesGuiTools.initDefaultAttributeValues((BaseGenericIdEntity) item, item.getMetaClass());
        }
        if (item instanceof Categorized) {
            dynamicAttributesGuiTools.listenCategoryChanges(ds);
        }
    }
    ds.setItem(item);
    if (PersistenceHelper.isNew(item)) {
        // make sure that they will be saved on commit.
        for (Datasource datasource : ds.getDsContext().getAll()) {
            if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == ds) {
                if (datasource.getItem() != null && PersistenceHelper.isNew(datasource.getItem()))
                    ((DatasourceImplementation) datasource).modified(datasource.getItem());
            }
        }
    }
    ((DatasourceImplementation) ds).setModified(false);
    Security security = AppBeans.get(Security.NAME);
    if (!PersistenceHelper.isNew(item) && security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
        readOnly = false;
        LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), item.getId().toString());
        if (lockInfo == null) {
            justLocked = true;
        } else if (!(lockInfo instanceof LockNotSupported)) {
            window.getWindowManager().showNotification(messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), Datatypes.getNN(Date.class).format(lockInfo.getSince(), userSessionSource.getLocale())), Frame.NotificationType.HUMANIZED);
            Action action = window.getAction(Window.Editor.WINDOW_COMMIT);
            if (action != null)
                action.setEnabled(false);
            action = window.getAction(Window.Editor.WINDOW_COMMIT_AND_CLOSE);
            if (action != null)
                action.setEnabled(false);
            readOnly = true;
        }
    }
}
Also used : Categorized(com.haulmont.cuba.core.entity.Categorized) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) CollectionPropertyDatasourceImpl(com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)

Example 5 with DynamicAttributesGuiTools

use of com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools in project cuba by cuba-platform.

the class CreateAction method internalOpenEditor.

@SuppressWarnings("unchecked")
protected void internalOpenEditor(CollectionDatasource datasource, Entity newItem, Datasource parentDs, Map<String, Object> params) {
    Window.Editor window = target.getFrame().openEditor(getWindowId(), newItem, getOpenType(), params, parentDs);
    if (editorCloseListener == null) {
        window.addCloseListener(actionId -> {
            // move focus to owner
            target.requestFocus();
            if (Window.COMMIT_ACTION_ID.equals(actionId)) {
                Entity editedItem = window.getItem();
                if (editedItem != null) {
                    if (parentDs == null) {
                        if (editedItem instanceof BaseGenericIdEntity) {
                            BaseGenericIdEntity genericEditedEntity = (BaseGenericIdEntity) editedItem;
                            if (datasource.getLoadDynamicAttributes() && genericEditedEntity.getDynamicAttributes() == null) {
                                DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.class);
                                dynamicAttributesGuiTools.reloadDynamicAttributes(genericEditedEntity);
                            }
                        }
                        if (addFirst && datasource instanceof CollectionDatasource.Ordered)
                            ((CollectionDatasource.Ordered) datasource).includeItemFirst(editedItem);
                        else
                            datasource.includeItem(editedItem);
                    }
                    target.setSelected(editedItem);
                    afterCommit(editedItem);
                    if (afterCommitHandler != null) {
                        afterCommitHandler.handle(editedItem);
                    }
                }
            }
            afterWindowClosed(window);
            if (afterWindowClosedHandler != null) {
                afterWindowClosedHandler.handle(window, actionId);
            }
        });
    } else {
        window.addCloseListener(editorCloseListener);
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)

Aggregations

DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)5 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)3 Entity (com.haulmont.cuba.core.entity.Entity)3 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)2 Categorized (com.haulmont.cuba.core.entity.Categorized)1 CollectionPropertyDatasourceImpl (com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)1 DatasourceImplementation (com.haulmont.cuba.gui.data.impl.DatasourceImplementation)1 Element (org.dom4j.Element)1