Search in sources :

Example 1 with EntityOptions

use of com.haulmont.cuba.gui.components.data.meta.EntityOptions in project cuba by cuba-platform.

the class LookupBuilderProcessor method handleSelectionWithField.

@SuppressWarnings("unchecked")
protected <E extends Entity> void handleSelectionWithField(@SuppressWarnings("unused") LookupBuilder<E> builder, HasValue<E> field, Collection<E> itemsFromLookup) {
    if (itemsFromLookup.isEmpty()) {
        return;
    }
    Collection<E> selectedItems = transform(itemsFromLookup, builder);
    Entity newValue = selectedItems.iterator().next();
    View viewForField = clientConfig.getReloadUnfetchedAttributesFromLookupScreens() && metadataTools.isPersistent(newValue.getClass()) ? getViewForField(field) : null;
    if (viewForField != null && !entityStates.isLoadedWithView(newValue, viewForField)) {
        newValue = dataManager.reload(newValue, viewForField);
    }
    if (field instanceof LookupPickerField) {
        LookupPickerField lookupPickerField = (LookupPickerField) field;
        Options options = lookupPickerField.getOptions();
        if (options instanceof EntityOptions) {
            EntityOptions entityOptions = (EntityOptions) options;
            if (entityOptions.containsItem(newValue)) {
                entityOptions.updateItem(newValue);
            }
            if (lookupPickerField.isRefreshOptionsOnLookupClose()) {
                entityOptions.refresh();
            }
        }
    }
    // In case of PickerField set the value as if the user had set it
    if (field instanceof SupportsUserAction) {
        ((SupportsUserAction<E>) field).setValueFromUser((E) newValue);
    } else {
        field.setValue((E) newValue);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) LookupPickerField(com.haulmont.cuba.gui.components.LookupPickerField) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) Options(com.haulmont.cuba.gui.components.data.Options) SupportsUserAction(com.haulmont.cuba.gui.components.SupportsUserAction) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions)

Example 2 with EntityOptions

use of com.haulmont.cuba.gui.components.data.meta.EntityOptions in project cuba by cuba-platform.

the class EditorBuilderProcessor method buildEditor.

@SuppressWarnings("unchecked")
public <E extends Entity, S extends Screen> S buildEditor(EditorBuilder<E> builder) {
    FrameOwner origin = builder.getOrigin();
    Screens screens = getScreenContext(origin).getScreens();
    ListComponent<E> listComponent = builder.getListComponent();
    CollectionContainer<E> container = builder.getContainer();
    if (container == null && listComponent != null) {
        DataUnit items = listComponent.getItems();
        container = items instanceof ContainerDataUnit ? ((ContainerDataUnit) items).getContainer() : null;
    }
    E entity = initEntity(builder, container);
    if (builder.getMode() == EditMode.EDIT && entity == null) {
        throw new IllegalStateException(String.format("Editor of %s cannot be open with mode EDIT, entity is not set", builder.getEntityClass()));
    }
    Screen screen = createScreen(builder, screens, entity);
    EditorScreen<E> editorScreen = (EditorScreen<E>) screen;
    editorScreen.setEntityToEdit(entity);
    DataContext parentDataContext = setupParentDataContext(origin, screen, container, builder.getParentDataContext());
    if (container != null) {
        CollectionContainer<E> ct = container;
        screen.addAfterCloseListener(event -> {
            CloseAction closeAction = event.getCloseAction();
            if (isCommitCloseAction(closeAction)) {
                E entityFromEditor = getCommittedEntity(editorScreen, parentDataContext);
                E reloadedEntity = reloadIfNeeded(entityFromEditor, ct, builder);
                E committedEntity = transform(reloadedEntity, builder);
                E mergedEntity = merge(committedEntity, origin, parentDataContext);
                if (builder.getMode() == EditMode.CREATE) {
                    boolean addsFirst;
                    if (!(ct instanceof Nested)) {
                        addsFirst = clientConfig.getCreateActionAddsFirst();
                        if (builder.getAddFirst() != null) {
                            addsFirst = builder.getAddFirst();
                        }
                    } else {
                        addsFirst = false;
                    }
                    if (ct instanceof Nested || !addsFirst) {
                        ct.getMutableItems().add(mergedEntity);
                    } else {
                        ct.getMutableItems().add(0, mergedEntity);
                    }
                } else {
                    ct.replaceItem(mergedEntity);
                }
            }
            if (listComponent instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
                ((com.haulmont.cuba.gui.components.Component.Focusable) listComponent).focus();
            }
        });
    }
    HasValue<E> field = builder.getField();
    if (field != null) {
        if (parentDataContext == null && field instanceof HasValueSource) {
            ValueSource fieldValueSource = ((HasValueSource) field).getValueSource();
            if (fieldValueSource instanceof EntityValueSource) {
                if (isCompositionProperty((EntityValueSource) fieldValueSource)) {
                    DataContext thisDataContext = UiControllerUtils.getScreenData(origin).getDataContext();
                    DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
                    checkDataContext(screen, dataContext);
                    dataContext.setParent(thisDataContext);
                }
            }
        }
        screen.addAfterCloseListener(event -> {
            CloseAction closeAction = event.getCloseAction();
            if (isCommitCloseAction(closeAction)) {
                E entityFromEditor = editorScreen.getEditedEntity();
                E editedEntity = transform(entityFromEditor, builder);
                if (field instanceof LookupPickerField) {
                    LookupPickerField lookupPickerField = ((LookupPickerField) field);
                    Options options = lookupPickerField.getOptions();
                    if (options instanceof EntityOptions) {
                        EntityOptions entityOptions = (EntityOptions) options;
                        if (entityOptions.containsItem(editedEntity)) {
                            entityOptions.updateItem(editedEntity);
                        }
                    }
                }
                if (field instanceof SupportsUserAction) {
                    ((SupportsUserAction) field).setValueFromUser(editedEntity);
                } else {
                    field.setValue(editedEntity);
                }
            }
            if (field instanceof com.haulmont.cuba.gui.components.Component.Focusable) {
                ((com.haulmont.cuba.gui.components.Component.Focusable) field).focus();
            }
        });
    }
    if (builder instanceof EditorClassBuilder) {
        @SuppressWarnings("unchecked") Consumer<AfterScreenCloseEvent> closeListener = ((EditorClassBuilder) builder).getCloseListener();
        if (closeListener != null) {
            screen.addAfterCloseListener(new AfterCloseListenerAdapter(closeListener));
        }
    }
    return (S) screen;
}
Also used : EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) Options(com.haulmont.cuba.gui.components.data.Options) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions) DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) Screens(com.haulmont.cuba.gui.Screens) HasValueSource(com.haulmont.cuba.gui.components.data.HasValueSource) EntityValueSource(com.haulmont.cuba.gui.components.data.meta.EntityValueSource) ValueSource(com.haulmont.cuba.gui.components.data.ValueSource) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)

Example 3 with EntityOptions

use of com.haulmont.cuba.gui.components.data.meta.EntityOptions in project cuba by cuba-platform.

the class WebSearchPickerField method setOptions.

@Override
public void setOptions(Options<V> options) {
    if (this.optionsBinding != null) {
        this.optionsBinding.unbind();
        this.optionsBinding = null;
    }
    if (options != null) {
        OptionsBinder optionsBinder = beanLocator.get(OptionsBinder.NAME);
        this.optionsBinding = optionsBinder.bind(options, this, this::setItemsToPresentation);
        this.optionsBinding.activate();
        if (getMetaClass() == null && options instanceof EntityOptions) {
            setMetaClass(((EntityOptions<V>) options).getEntityMetaClass());
        }
    }
}
Also used : OptionsBinder(com.haulmont.cuba.gui.components.data.options.OptionsBinder) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions)

Example 4 with EntityOptions

use of com.haulmont.cuba.gui.components.data.meta.EntityOptions in project cuba by cuba-platform.

the class WebLookupPickerField method setOptions.

@Override
public void setOptions(Options<V> options) {
    if (this.optionsBinding != null) {
        this.optionsBinding.unbind();
        this.optionsBinding = null;
    }
    if (options != null) {
        OptionsBinder optionsBinder = beanLocator.get(OptionsBinder.NAME);
        this.optionsBinding = optionsBinder.bind(options, this, this::setItemsToPresentation);
        this.optionsBinding.activate();
        if (getMetaClass() == null && options instanceof EntityOptions) {
            setMetaClass(((EntityOptions<V>) options).getEntityMetaClass());
        }
    }
}
Also used : OptionsBinder(com.haulmont.cuba.gui.components.data.options.OptionsBinder) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions)

Example 5 with EntityOptions

use of com.haulmont.cuba.gui.components.data.meta.EntityOptions in project cuba by cuba-platform.

the class WebTokenList method updateMasterRefIfOptionsRefreshed.

/**
 * Sets master-entity reference to the value and remove master-entity reference
 * from options if they are not in nested container.
 *
 * @param value value items
 */
protected void updateMasterRefIfOptionsRefreshed(Collection<V> value) {
    if (!isRefreshOptionsEnabled()) {
        return;
    }
    if (!(getValueSource() instanceof ContainerValueSource)) {
        return;
    }
    EntityOptions<V> options = (EntityOptions<V>) getOptions();
    if (options == null) {
        return;
    }
    ContainerValueSource valueSource = (ContainerValueSource) getValueSource();
    MetaPropertyPath mpp = valueSource.getMetaPropertyPath();
    MetaProperty inverseProperty = mpp.getMetaProperty().getInverse();
    Entity masterEntity = valueSource.getItem();
    if (inverseProperty == null || masterEntity == null) {
        return;
    }
    List<V> optionItems = getOptions().getOptions().collect(Collectors.toList());
    for (V option : optionItems) {
        // skip all options that did not load master-reference
        if (!entityStates.isLoaded(option, inverseProperty.getName())) {
            continue;
        }
        if (value.contains(option)) {
            // reset master-entity reference
            option.setValue(inverseProperty.getName(), masterEntity);
        } else {
            Entity ref = option.getValue(inverseProperty.getName());
            if (ref == null) {
                continue;
            }
            // remove ref to the master entity if option is not in value
            if (Objects.equals(ref.getId(), masterEntity.getId())) {
                option.setValue(inverseProperty.getName(), null);
            }
        }
    }
}
Also used : ContainerValueSource(com.haulmont.cuba.gui.components.data.value.ContainerValueSource) Entity(com.haulmont.cuba.core.entity.Entity) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) MetaProperty(com.haulmont.chile.core.model.MetaProperty) EntityOptions(com.haulmont.cuba.gui.components.data.meta.EntityOptions)

Aggregations

EntityOptions (com.haulmont.cuba.gui.components.data.meta.EntityOptions)5 Entity (com.haulmont.cuba.core.entity.Entity)2 Options (com.haulmont.cuba.gui.components.data.Options)2 OptionsBinder (com.haulmont.cuba.gui.components.data.options.OptionsBinder)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 Screens (com.haulmont.cuba.gui.Screens)1 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)1 LookupPickerField (com.haulmont.cuba.gui.components.LookupPickerField)1 SupportsUserAction (com.haulmont.cuba.gui.components.SupportsUserAction)1 DataUnit (com.haulmont.cuba.gui.components.data.DataUnit)1 HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)1 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)1 ContainerDataUnit (com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)1 EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)1 ContainerValueSource (com.haulmont.cuba.gui.components.data.value.ContainerValueSource)1