Search in sources :

Example 16 with ContainerDataUnit

use of io.jmix.ui.component.data.meta.ContainerDataUnit 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 17 with ContainerDataUnit

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

the class LookupBuilderProcessor method buildLookup.

@SuppressWarnings("unchecked")
public <E, S extends Screen> S buildLookup(LookupBuilder<E> builder) {
    FrameOwner origin = builder.getOrigin();
    Screens screens = getScreenContext(origin).getScreens();
    Screen screen = createScreen(builder, screens);
    if (!(screen instanceof LookupScreen)) {
        throw new IllegalArgumentException(String.format("Screen %s does not implement LookupScreen: %s", screen.getId(), screen.getClass()));
    }
    LookupScreen<E> lookupScreen = (LookupScreen) screen;
    if (builder.getField() != null) {
        HasValue field = builder.getField();
        if (field instanceof io.jmix.ui.component.Component.Focusable) {
            screen.addAfterCloseListener(event -> {
                // move focus to owner
                ((io.jmix.ui.component.Component.Focusable) field).focus();
            });
        }
        lookupScreen.setSelectHandler(items -> handleSelectionWithField(builder, field, items, builder.isFieldCollectionValue()));
    }
    CollectionContainer<E> container = null;
    if (builder.getListComponent() != null) {
        ListComponent<E> listComponent = builder.getListComponent();
        if (listComponent instanceof io.jmix.ui.component.Component.Focusable) {
            screen.addAfterCloseListener(event -> {
                // move focus to owner
                ((io.jmix.ui.component.Component.Focusable) listComponent).focus();
            });
        }
        if (listComponent.getItems() instanceof ContainerDataUnit) {
            container = ((ContainerDataUnit<E>) listComponent.getItems()).getContainer();
        }
    }
    if (builder.getContainer() != null) {
        container = builder.getContainer();
    }
    if (container != null) {
        CollectionContainer<E> collectionDc = container;
        lookupScreen.setSelectHandler(items -> handleSelectionWithContainer(builder, collectionDc, items));
    }
    if (builder.getSelectHandler() != null) {
        lookupScreen.setSelectHandler(builder.getSelectHandler());
    }
    if (builder.getSelectValidator() != null) {
        lookupScreen.setSelectValidator(builder.getSelectValidator());
    }
    if (builder instanceof LookupClassBuilder) {
        Consumer<AfterScreenShowEvent> afterShowListener = ((LookupClassBuilder) builder).getAfterShowListener();
        if (afterShowListener != null) {
            screen.addAfterShowListener(new AfterShowListenerAdapter(afterShowListener));
        }
        Consumer<AfterScreenCloseEvent> afterCloseListener = ((LookupClassBuilder) builder).getAfterCloseListener();
        if (afterCloseListener != null) {
            screen.addAfterCloseListener(new AfterCloseListenerAdapter(afterCloseListener));
        }
    }
    return (S) screen;
}
Also used : LookupScreen(io.jmix.ui.screen.LookupScreen) LookupScreen(io.jmix.ui.screen.LookupScreen) Screen(io.jmix.ui.screen.Screen) Screens(io.jmix.ui.Screens) io.jmix.ui.component(io.jmix.ui.component) FrameOwner(io.jmix.ui.screen.FrameOwner) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit)

Example 18 with ContainerDataUnit

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

the class AbstractTable method setupPaginationDataSourceProvider.

public void setupPaginationDataSourceProvider() {
    if (pagination == null) {
        return;
    }
    DataUnit items = getItems();
    PaginationDataBinder provider;
    if (items instanceof ContainerDataUnit) {
        provider = applicationContext.getBean(PaginationDataUnitBinder.class, items);
    } else if (items instanceof EmptyDataUnit && items instanceof EntityDataUnit) {
        provider = new PaginationEmptyBinder(((EntityDataUnit) items).getEntityMetaClass());
    } else {
        throw new IllegalStateException("Unsupported data unit type: " + items);
    }
    pagination.setDataBinder(provider);
}
Also used : PaginationDataUnitBinder(io.jmix.ui.component.pagination.data.PaginationDataUnitBinder) PaginationEmptyBinder(io.jmix.ui.component.pagination.data.PaginationEmptyBinder) EmptyDataUnit(io.jmix.ui.component.data.meta.EmptyDataUnit) DataUnit(io.jmix.ui.component.data.DataUnit) EntityDataUnit(io.jmix.ui.component.data.meta.EntityDataUnit) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit) EmptyDataUnit(io.jmix.ui.component.data.meta.EmptyDataUnit) PaginationDataBinder(io.jmix.ui.component.pagination.data.PaginationDataBinder) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit) EntityDataUnit(io.jmix.ui.component.data.meta.EntityDataUnit)

Example 19 with ContainerDataUnit

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

the class UiTestIdsSupport method getInferredTestId.

@Nullable
public String getInferredTestId(DataUnit dataUnit, String suffix) {
    if (dataUnit instanceof ContainerDataUnit) {
        ContainerDataUnit<?> dcDataUnit = (ContainerDataUnit<?>) dataUnit;
        MetaClass entityMetaClass = dcDataUnit.getEntityMetaClass();
        return entityMetaClass.getName() + suffix;
    }
    return null;
}
Also used : MetaClass(io.jmix.core.metamodel.model.MetaClass) ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit) Nullable(javax.annotation.Nullable)

Example 20 with ContainerDataUnit

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

the class TestCommentaryPanel method addMessage.

protected void addMessage(String text) {
    if (getCommentProvider() == null) {
        return;
    }
    CommentObject comment = getCommentProvider().apply(text);
    DataGridItems<CommentObject> items = commentsDataGrid.getItems();
    if (items instanceof ContainerDataUnit) {
        // noinspection unchecked
        CollectionContainer<CommentObject> container = ((ContainerDataUnit<CommentObject>) items).getContainer();
        container.getMutableItems().add(comment);
    } else {
        throw new IllegalStateException("Items must implement ContainerDataUnit");
    }
}
Also used : ContainerDataUnit(io.jmix.ui.component.data.meta.ContainerDataUnit) CommentObject(test_support.entity.model_objects.CommentObject)

Aggregations

ContainerDataUnit (io.jmix.ui.component.data.meta.ContainerDataUnit)20 MetaClass (io.jmix.core.metamodel.model.MetaClass)8 DataUnit (io.jmix.ui.component.data.DataUnit)6 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)5 Nested (io.jmix.ui.model.Nested)4 UiEntityAttributeContext (io.jmix.ui.accesscontext.UiEntityAttributeContext)3 EmptyDataUnit (io.jmix.ui.component.data.meta.EmptyDataUnit)3 EntityDataUnit (io.jmix.ui.component.data.meta.EntityDataUnit)3 CollectionContainer (io.jmix.ui.model.CollectionContainer)3 Screens (io.jmix.ui.Screens)2 io.jmix.ui.component (io.jmix.ui.component)2 Frame (io.jmix.ui.component.Frame)2 Window (io.jmix.ui.component.Window)2 PaginationDataBinder (io.jmix.ui.component.pagination.data.PaginationDataBinder)2 PaginationDataUnitBinder (io.jmix.ui.component.pagination.data.PaginationDataUnitBinder)2 PaginationEmptyBinder (io.jmix.ui.component.pagination.data.PaginationEmptyBinder)2 DataContext (io.jmix.ui.model.DataContext)2 DataLoader (io.jmix.ui.model.DataLoader)2 HasLoader (io.jmix.ui.model.HasLoader)2 FrameOwner (io.jmix.ui.screen.FrameOwner)2