Search in sources :

Example 1 with EntityOptions

use of io.jmix.ui.component.data.meta.EntityOptions in project jmix by jmix-framework.

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();
    Object 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
            EntityValues.setValue(option, inverseProperty.getName(), masterEntity);
        } else {
            Entity ref = EntityValues.getValue(option, inverseProperty.getName());
            if (ref == null) {
                continue;
            }
            // remove ref to the master entity if option is not in value
            if (Objects.equals(EntityValues.getId(ref), EntityValues.getId(masterEntity))) {
                EntityValues.setValue(option, inverseProperty.getName(), null);
            }
        }
    }
}
Also used : ContainerValueSource(io.jmix.ui.component.data.value.ContainerValueSource) MetaPropertyPath(io.jmix.core.metamodel.model.MetaPropertyPath) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) ListEntityOptions(io.jmix.ui.component.data.options.ListEntityOptions) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions) MapEntityOptions(io.jmix.ui.component.data.options.MapEntityOptions)

Example 2 with EntityOptions

use of io.jmix.ui.component.data.meta.EntityOptions in project jmix by jmix-framework.

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 = applicationContext.getBean(OptionsBinder.class);
        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(io.jmix.ui.component.data.options.OptionsBinder) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions)

Example 3 with EntityOptions

use of io.jmix.ui.component.data.meta.EntityOptions in project jmix by jmix-framework.

the class EditorBuilderProcessor method buildEditor.

@SuppressWarnings("unchecked")
public <E, 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 = transformForCollectionContainer(entityFromEditor, ct);
                E committedEntity = transform(reloadedEntity, builder);
                E mergedEntity = merge(committedEntity, origin, parentDataContext);
                if (builder.getMode() == EditMode.CREATE) {
                    boolean addsFirst;
                    if (!(ct instanceof Nested)) {
                        addsFirst = screenProperties.isCreateActionAddsFirst();
                        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 io.jmix.ui.component.Component.Focusable) {
                ((io.jmix.ui.component.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 reloadedEntity = transformForField(entityFromEditor, field);
                E editedEntity = transform(reloadedEntity, builder);
                if (field instanceof EntityComboBox) {
                    EntityComboBox entityComboBox = ((EntityComboBox) field);
                    Options options = entityComboBox.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 io.jmix.ui.component.Component.Focusable) {
                ((io.jmix.ui.component.Component.Focusable) field).focus();
            }
        });
    }
    if (builder instanceof EditorClassBuilder) {
        Consumer<AfterScreenShowEvent> afterShowListener = ((EditorClassBuilder) builder).getAfterShowListener();
        if (afterShowListener != null) {
            screen.addAfterShowListener(new AfterShowListenerAdapter(afterShowListener));
        }
        Consumer<AfterScreenCloseEvent> closeListener = ((EditorClassBuilder) builder).getAfterCloseListener();
        if (closeListener != null) {
            screen.addAfterCloseListener(new AfterCloseListenerAdapter(closeListener));
        }
    }
    return (S) screen;
}
Also used : Options(io.jmix.ui.component.data.Options) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) Nested(io.jmix.ui.model.Nested) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions) DataContext(io.jmix.ui.model.DataContext) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit) DataUnit(io.jmix.ui.component.data.DataUnit) HasValueSource(io.jmix.ui.component.data.HasValueSource) Screens(io.jmix.ui.Screens) io.jmix.ui.component(io.jmix.ui.component) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) HasValueSource(io.jmix.ui.component.data.HasValueSource) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit)

Example 4 with EntityOptions

use of io.jmix.ui.component.data.meta.EntityOptions in project jmix by jmix-framework.

the class EntityComboBoxImpl method setOptions.

@Override
public void setOptions(@Nullable Options<V> options) {
    if (this.optionsBinding != null) {
        this.optionsBinding.unbind();
        this.optionsBinding = null;
    }
    if (options != null) {
        OptionsBinder optionsBinder = applicationContext.getBean(OptionsBinder.class);
        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(io.jmix.ui.component.data.options.OptionsBinder) EntityOptions(io.jmix.ui.component.data.meta.EntityOptions)

Aggregations

EntityOptions (io.jmix.ui.component.data.meta.EntityOptions)4 OptionsBinder (io.jmix.ui.component.data.options.OptionsBinder)2 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)1 MetaPropertyPath (io.jmix.core.metamodel.model.MetaPropertyPath)1 Screens (io.jmix.ui.Screens)1 io.jmix.ui.component (io.jmix.ui.component)1 DataUnit (io.jmix.ui.component.data.DataUnit)1 HasValueSource (io.jmix.ui.component.data.HasValueSource)1 Options (io.jmix.ui.component.data.Options)1 ValueSource (io.jmix.ui.component.data.ValueSource)1 ContainerDataUnit (io.jmix.ui.component.data.meta.ContainerDataUnit)1 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)1 ListEntityOptions (io.jmix.ui.component.data.options.ListEntityOptions)1 MapEntityOptions (io.jmix.ui.component.data.options.MapEntityOptions)1 ContainerValueSource (io.jmix.ui.component.data.value.ContainerValueSource)1 DataContext (io.jmix.ui.model.DataContext)1 Nested (io.jmix.ui.model.Nested)1