Search in sources :

Example 1 with DataUnit

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

the class ScreenBuilders method lookup.

/**
 * Creates a screen builder using list component.
 * <p>
 * Example of building a lookup screen for adding row to table / tree component:
 * <pre>{@code
 * SomeCustomerListScreen screen = screenBuilders.lookup(customersTable)
 *         .withScreen(SomeCustomerListScreen.class)
 *         .build();
 * }</pre>
 *
 * @param listComponent {@link Table}, {@link DataGrid} or another component containing the list of entities
 * @param <E>           type of entity
 * @see #lookup(Class, FrameOwner)
 */
public <E extends Entity> LookupBuilder<E> lookup(ListComponent<E> listComponent) {
    checkNotNullArgument(listComponent);
    checkNotNullArgument(listComponent.getFrame());
    FrameOwner frameOwner = listComponent.getFrame().getFrameOwner();
    Class<E> entityClass;
    DataUnit items = listComponent.getItems();
    if (items instanceof EntityDataUnit) {
        entityClass = ((EntityDataUnit) items).getEntityMetaClass().getJavaClass();
    } else {
        throw new IllegalStateException(String.format("Component %s is not bound to data", listComponent));
    }
    LookupBuilder<E> builder = new LookupBuilder<>(frameOwner, entityClass, lookupBuilderProcessor::buildLookup);
    builder.withListComponent(listComponent);
    return builder;
}
Also used : DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) EntityDataUnit(com.haulmont.cuba.gui.components.data.meta.EntityDataUnit) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) EntityDataUnit(com.haulmont.cuba.gui.components.data.meta.EntityDataUnit)

Example 2 with DataUnit

use of com.haulmont.cuba.gui.components.data.DataUnit 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 DataUnit

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

the class WebFilterHelper method initTableFtsTooltips.

@Override
public void initTableFtsTooltips(ListComponent listComponent, MetaClass metaClass, String searchTerm) {
    FtsFilterHelper ftsFilterHelper;
    if (beanLocator.containsBean(FtsFilterHelper.NAME)) {
        ftsFilterHelper = beanLocator.get(FtsFilterHelper.class);
    } else {
        return;
    }
    if (listComponent instanceof Table) {
        Map<Object, String> tooltipsCache = new HashMap<>();
        Map<Object, String> metaClassesCache = new HashMap<>();
        listComponent.withUnwrapped(com.vaadin.v7.ui.Table.class, vTable -> vTable.setItemDescriptionGenerator((source, itemId, propertyId) -> {
            return tooltipsCache.computeIfAbsent(itemId, k -> {
                if (k instanceof GroupInfo) {
                    return "";
                }
                return ftsFilterHelper.buildTableTooltip(metaClassesCache.computeIfAbsent(itemId, id -> {
                    DataUnit dataUnit = listComponent.getItems();
                    Entity<?> entity = null;
                    if (dataUnit instanceof DatasourceDataUnit) {
                        // legacy GUI
                        entity = ((DatasourceDataUnit) dataUnit).getDatasource().getItem(id);
                    } else if (dataUnit instanceof ContainerDataUnit) {
                        entity = ((ContainerDataUnit) dataUnit).getContainer().getItem(id);
                    }
                    if (entity != null)
                        return entity.getMetaClass().getName();
                    return metaClass.getName();
                }), k, searchTerm);
            });
        }));
    } else if (listComponent instanceof DataGrid) {
        ((DataGrid) listComponent).setRowDescriptionProvider(o -> {
            if (o instanceof Entity) {
                Entity entity = (Entity) o;
                return ftsFilterHelper.buildTableTooltip(entity.getMetaClass().getName(), entity.getId(), searchTerm);
            } else {
                return null;
            }
        }, ContentMode.HTML);
    }
}
Also used : FtsFilterHelper(com.haulmont.cuba.gui.components.filter.FtsFilterHelper) java.util(java.util) GroupCondition(com.haulmont.cuba.gui.components.filter.condition.GroupCondition) DropLocation(com.vaadin.shared.ui.grid.DropLocation) Screen(com.haulmont.cuba.gui.screen.Screen) AppFolderEditWindow(com.haulmont.cuba.web.app.folders.AppFolderEditWindow) MetaClass(com.haulmont.chile.core.model.MetaClass) Inject(javax.inject.Inject) GroupInfo(com.haulmont.cuba.gui.data.GroupInfo) Configuration(com.haulmont.cuba.core.global.Configuration) TreeGridDragSource(com.vaadin.ui.components.grid.TreeGridDragSource) ScreenFragment(com.haulmont.cuba.gui.screen.ScreenFragment) AppUI(com.haulmont.cuba.web.AppUI) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) Nullable(javax.annotation.Nullable) CubaTree(com.haulmont.cuba.web.widgets.CubaTree) Presentations(com.haulmont.cuba.gui.presentations.Presentations) DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) AbstractSearchFolder(com.haulmont.cuba.core.entity.AbstractSearchFolder) FoldersPane(com.haulmont.cuba.gui.components.mainwindow.FoldersPane) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) AbstractCondition(com.haulmont.cuba.gui.components.filter.condition.AbstractCondition) CubaTextField(com.haulmont.cuba.web.widgets.CubaTextField) TreeGridDropTarget(com.vaadin.ui.components.grid.TreeGridDropTarget) Folder(com.haulmont.cuba.core.entity.Folder) ConditionsTree(com.haulmont.cuba.gui.components.filter.ConditionsTree) FilterHelper(com.haulmont.cuba.gui.components.filter.FilterHelper) UiControllerUtils.getHostScreen(com.haulmont.cuba.gui.screen.UiControllerUtils.getHostScreen) WebConfig(com.haulmont.cuba.web.WebConfig) DatasourceDataUnit(com.haulmont.cuba.gui.components.data.meta.DatasourceDataUnit) ShortcutListenerDelegate(com.haulmont.cuba.web.gui.components.util.ShortcutListenerDelegate) Node(com.haulmont.bali.datastruct.Node) FolderEditWindow(com.haulmont.cuba.web.app.folders.FolderEditWindow) BeanLocator(com.haulmont.cuba.core.global.BeanLocator) CubaFoldersPane(com.haulmont.cuba.web.app.folders.CubaFoldersPane) UiComponents(com.haulmont.cuba.gui.UiComponents) DropMode(com.vaadin.shared.ui.grid.DropMode) Entity(com.haulmont.cuba.core.entity.Entity) Component(com.vaadin.ui.Component) Entity(com.haulmont.cuba.core.entity.Entity) GroupInfo(com.haulmont.cuba.gui.data.GroupInfo) DatasourceDataUnit(com.haulmont.cuba.gui.components.data.meta.DatasourceDataUnit) DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) DatasourceDataUnit(com.haulmont.cuba.gui.components.data.meta.DatasourceDataUnit) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) FtsFilterHelper(com.haulmont.cuba.gui.components.filter.FtsFilterHelper)

Example 4 with DataUnit

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

the class RemoveOperation method builder.

/**
 * Creates a remove builder using list component, e.g. {@link Table} or {@link DataGrid}.
 *
 * @param listComponent list component
 * @param <E>           type of entity
 */
public <E extends Entity> RemoveBuilder<E> builder(ListComponent<E> listComponent) {
    checkNotNullArgument(listComponent);
    checkNotNullArgument(listComponent.getFrame());
    FrameOwner frameOwner = listComponent.getFrame().getFrameOwner();
    Class<E> entityClass;
    DataUnit items = listComponent.getItems();
    if (items instanceof ContainerDataUnit) {
        entityClass = ((ContainerDataUnit) items).getEntityMetaClass().getJavaClass();
    } else {
        throw new IllegalStateException(String.format("Component %s is not bound to data", listComponent));
    }
    return builder(entityClass, frameOwner).withListComponent(listComponent);
}
Also used : DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit) ContainerDataUnit(com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)

Example 5 with DataUnit

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

the class ScreenBuilders method editor.

/**
 * Creates a screen builder using list component.
 * <p>
 * Example of building a screen for editing a currently selected entity:
 * <pre>{@code
 * SomeCustomerEditor screen = screenBuilders.editor(customersTable)
 *          .withScreen(SomeCustomerEditor.class)
 *          .build();
 * }</pre>
 * <p>
 * Example of building a screen for creating a new entity instance:
 * <pre>{@code
 * SomeCustomerEditor screen = screenBuilders.editor(customersTable)
 *          .withScreen(SomeCustomerEditor.class)
 *          .newEntity()
 *          .build();
 * }</pre>
 *
 * @param listComponent {@link Table}, {@link DataGrid} or another component containing the list of entities
 * @see #editor(Class, FrameOwner)
 */
public <E extends Entity> EditorBuilder<E> editor(ListComponent<E> listComponent) {
    checkNotNullArgument(listComponent);
    checkNotNullArgument(listComponent.getFrame());
    FrameOwner frameOwner = listComponent.getFrame().getFrameOwner();
    Class<E> entityClass;
    DataUnit items = listComponent.getItems();
    if (items instanceof EntityDataUnit) {
        entityClass = ((EntityDataUnit) items).getEntityMetaClass().getJavaClass();
    } else {
        throw new IllegalStateException(String.format("Component %s is not bound to data", listComponent));
    }
    EditorBuilder<E> builder = new EditorBuilder<>(frameOwner, entityClass, editorBuilderProcessor::buildEditor);
    builder.withListComponent(listComponent);
    builder.editEntity(listComponent.getSingleSelected());
    return builder;
}
Also used : DataUnit(com.haulmont.cuba.gui.components.data.DataUnit) EntityDataUnit(com.haulmont.cuba.gui.components.data.meta.EntityDataUnit) FrameOwner(com.haulmont.cuba.gui.screen.FrameOwner) EntityDataUnit(com.haulmont.cuba.gui.components.data.meta.EntityDataUnit)

Aggregations

DataUnit (com.haulmont.cuba.gui.components.data.DataUnit)5 ContainerDataUnit (com.haulmont.cuba.gui.components.data.meta.ContainerDataUnit)3 FrameOwner (com.haulmont.cuba.gui.screen.FrameOwner)3 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)2 EntityDataUnit (com.haulmont.cuba.gui.components.data.meta.EntityDataUnit)2 Node (com.haulmont.bali.datastruct.Node)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 AbstractSearchFolder (com.haulmont.cuba.core.entity.AbstractSearchFolder)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Folder (com.haulmont.cuba.core.entity.Folder)1 BeanLocator (com.haulmont.cuba.core.global.BeanLocator)1 Configuration (com.haulmont.cuba.core.global.Configuration)1 Screens (com.haulmont.cuba.gui.Screens)1 UiComponents (com.haulmont.cuba.gui.UiComponents)1 HasValueSource (com.haulmont.cuba.gui.components.data.HasValueSource)1 Options (com.haulmont.cuba.gui.components.data.Options)1 ValueSource (com.haulmont.cuba.gui.components.data.ValueSource)1 DatasourceDataUnit (com.haulmont.cuba.gui.components.data.meta.DatasourceDataUnit)1 EntityOptions (com.haulmont.cuba.gui.components.data.meta.EntityOptions)1 EntityValueSource (com.haulmont.cuba.gui.components.data.meta.EntityValueSource)1