Search in sources :

Example 1 with DataContext

use of io.jmix.ui.model.DataContext in project jmix by jmix-framework.

the class EditorBuilderProcessor method setupParentDataContext.

@Nullable
protected DataContext setupParentDataContext(FrameOwner origin, Screen screen, @Nullable InstanceContainer container, @Nullable DataContext parentContext) {
    DataContext dataContext = parentContext;
    if (dataContext == null && container instanceof Nested) {
        InstanceContainer masterContainer = ((Nested) container).getMaster();
        String property = ((Nested) container).getProperty();
        MetaClass masterMetaClass = masterContainer.getEntityMetaClass();
        MetaProperty metaProperty = masterMetaClass.getProperty(property);
        if (metaProperty.getType() == MetaProperty.Type.COMPOSITION) {
            dataContext = UiControllerUtils.getScreenData(origin).getDataContextOrNull();
        }
    }
    if (dataContext != null) {
        DataContext childContext = UiControllerUtils.getScreenData(screen).getDataContextOrNull();
        checkDataContext(screen, childContext);
        childContext.setParent(dataContext);
    }
    return dataContext;
}
Also used : DataContext(io.jmix.ui.model.DataContext) MetaClass(io.jmix.core.metamodel.model.MetaClass) Nested(io.jmix.ui.model.Nested) InstanceContainer(io.jmix.ui.model.InstanceContainer) MetaProperty(io.jmix.core.metamodel.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 2 with DataContext

use of io.jmix.ui.model.DataContext in project jmix by jmix-framework.

the class LookupBuilderProcessor method handleSelectionWithContainer.

protected <E> void handleSelectionWithContainer(LookupBuilder<E> builder, CollectionContainer<E> collectionDc, Collection<E> itemsFromLookup) {
    if (itemsFromLookup.isEmpty()) {
        return;
    }
    Collection<E> selectedItems = transform(itemsFromLookup, builder);
    boolean initializeMasterReference = false;
    Object masterItem = null;
    MetaProperty inverseMetaProperty = null;
    // update holder reference if needed
    if (collectionDc instanceof Nested) {
        InstanceContainer masterDc = ((Nested) collectionDc).getMaster();
        String property = ((Nested) collectionDc).getProperty();
        masterItem = masterDc.getItem();
        MetaProperty metaProperty = metadata.getClass(masterItem).getProperty(property);
        inverseMetaProperty = metaProperty.getInverse();
        if (inverseMetaProperty != null && !inverseMetaProperty.getRange().getCardinality().isMany()) {
            Class<?> inversePropClass = extendedEntities.getEffectiveClass(inverseMetaProperty.getDomain());
            Class<?> dcClass = extendedEntities.getEffectiveClass(collectionDc.getEntityMetaClass());
            initializeMasterReference = inversePropClass.isAssignableFrom(dcClass);
        }
    }
    DataContext dataContext = UiControllerUtils.getScreenData(builder.getOrigin()).getDataContext();
    List<E> mergedItems = new ArrayList<>(selectedItems.size());
    FetchPlan viewForCollectionContainer = screenProperties.isReloadUnfetchedAttributesFromLookupScreens() && collectionDc.getEntityMetaClass() != null && metadataTools.isJpaEntity(collectionDc.getEntityMetaClass()) ? getFetchPlanForCollectionContainer(collectionDc, initializeMasterReference, inverseMetaProperty) : null;
    for (E item : selectedItems) {
        if (!collectionDc.containsItem(EntityValues.getId(item))) {
            if (viewForCollectionContainer != null && !entityStates.isLoadedWithFetchPlan(item, viewForCollectionContainer)) {
                // noinspection unchecked
                item = (E) dataManager.load(Id.of(item)).fetchPlan(viewForCollectionContainer).one();
            }
            // track changes in the related instance
            E mergedItem = dataContext.merge(item);
            if (initializeMasterReference) {
                // change reference, now it will be marked as modified
                EntityValues.setValue(mergedItem, inverseMetaProperty.getName(), masterItem);
            }
            mergedItems.add(mergedItem);
        }
    }
    collectionDc.getMutableItems().addAll(mergedItems);
}
Also used : Nested(io.jmix.ui.model.Nested) InstanceContainer(io.jmix.ui.model.InstanceContainer) DataContext(io.jmix.ui.model.DataContext) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 3 with DataContext

use of io.jmix.ui.model.DataContext in project jmix by jmix-framework.

the class RefreshAction method execute.

/**
 * Executes the action.
 */
@Override
public void execute() {
    if (target == null) {
        throw new IllegalStateException("RefreshAction target is not set");
    }
    if (target.getItems() instanceof EmptyDataUnit) {
        return;
    }
    if (!(target.getItems() instanceof ContainerDataUnit)) {
        throw new IllegalStateException("RefreshAction target is null or does not implement SupportsContainerBinding");
    }
    CollectionContainer container = ((ContainerDataUnit) target.getItems()).getContainer();
    if (container == null) {
        throw new IllegalStateException("RefreshAction target is not bound to CollectionContainer");
    }
    DataLoader loader = null;
    if (container instanceof HasLoader) {
        loader = ((HasLoader) container).getLoader();
    }
    if (loader != null) {
        DataContext dataContext = loader.getDataContext();
        if (dataContext != null) {
            for (Object entity : container.getItems()) {
                dataContext.evict(entity);
            }
        }
        loader.load();
    } else {
        log.warn("RefreshAction '{}' target container has no loader, refresh is impossible", getId());
    }
}
Also used : DataLoader(io.jmix.ui.model.DataLoader) DataContext(io.jmix.ui.model.DataContext) EmptyDataUnit(io.jmix.ui.component.data.meta.EmptyDataUnit) CollectionContainer(io.jmix.ui.model.CollectionContainer) HasLoader(io.jmix.ui.model.HasLoader) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit)

Example 4 with DataContext

use of io.jmix.ui.model.DataContext in project jmix by jmix-framework.

the class EntityClearAction method execute.

/**
 * Executes the action.
 */
@Override
public void execute() {
    EntityPicker entityPicker = getEntityPicker();
    // remove entity if it is a composition
    Object value = entityPicker.getValue();
    ValueSource valueSource = entityPicker.getValueSource();
    if (value != null && !value.equals(entityPicker.getEmptyValue()) && valueSource instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) entityPicker.getValueSource();
        Object entity = entityPicker.getValue();
        if (entityValueSource.getMetaPropertyPath() != null && entityValueSource.getMetaPropertyPath().getMetaProperty().getType() == MetaProperty.Type.COMPOSITION) {
            FrameOwner screen = entityPicker.getFrame().getFrameOwner();
            DataContext dataContext = UiControllerUtils.getScreenData(screen).getDataContext();
            dataContext.remove(entity);
        }
    }
    super.execute();
}
Also used : EntityPicker(io.jmix.ui.component.EntityPicker) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource) DataContext(io.jmix.ui.model.DataContext) FrameOwner(io.jmix.ui.screen.FrameOwner) ValueSource(io.jmix.ui.component.data.ValueSource) EntityValueSource(io.jmix.ui.component.data.meta.EntityValueSource)

Example 5 with DataContext

use of io.jmix.ui.model.DataContext 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)

Aggregations

DataContext (io.jmix.ui.model.DataContext)5 Nested (io.jmix.ui.model.Nested)3 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)2 ValueSource (io.jmix.ui.component.data.ValueSource)2 ContainerDataUnit (io.jmix.ui.component.data.meta.ContainerDataUnit)2 EntityValueSource (io.jmix.ui.component.data.meta.EntityValueSource)2 InstanceContainer (io.jmix.ui.model.InstanceContainer)2 MetaClass (io.jmix.core.metamodel.model.MetaClass)1 Screens (io.jmix.ui.Screens)1 io.jmix.ui.component (io.jmix.ui.component)1 EntityPicker (io.jmix.ui.component.EntityPicker)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 EmptyDataUnit (io.jmix.ui.component.data.meta.EmptyDataUnit)1 EntityOptions (io.jmix.ui.component.data.meta.EntityOptions)1 CollectionContainer (io.jmix.ui.model.CollectionContainer)1 DataLoader (io.jmix.ui.model.DataLoader)1 HasLoader (io.jmix.ui.model.HasLoader)1 FrameOwner (io.jmix.ui.screen.FrameOwner)1