Search in sources :

Example 1 with GeneratedPropertyContainer

use of com.vaadin.data.util.GeneratedPropertyContainer in project cuba by cuba-platform.

the class WebDataGrid method initComponent.

protected void initComponent(CubaGrid component) {
    setSelectionMode(SelectionMode.SINGLE);
    component.setImmediate(true);
    component.setColumnReorderingAllowed(true);
    containerWrapper = new GeneratedPropertyContainer(component.getContainerDataSource());
    component.setContainerDataSource(containerWrapper);
    component.addSelectionListener(e -> {
        if (datasource == null) {
            return;
        }
        final Set<E> selected = getSelected();
        if (selected.isEmpty()) {
            Entity dsItem = datasource.getItemIfValid();
            // noinspection unchecked
            datasource.setItem(null);
            if (dsItem == null) {
                // in this case item change event will not be generated
                refreshActionsState();
            }
        } else {
            // reset selection and select new item
            if (isMultiSelect()) {
                // noinspection unchecked
                datasource.setItem(null);
            }
            Entity newItem = selected.iterator().next();
            Entity dsItem = datasource.getItemIfValid();
            // noinspection unchecked
            datasource.setItem(newItem);
            if (Objects.equals(dsItem, newItem)) {
                // in this case item change event will not be generated
                refreshActionsState();
            }
        }
        LookupSelectionChangeEvent selectionChangeEvent = new LookupSelectionChangeEvent(this);
        getEventRouter().fireEvent(LookupSelectionChangeListener.class, LookupSelectionChangeListener::lookupValueChanged, selectionChangeEvent);
        if (getEventRouter().hasListeners(SelectionListener.class)) {
            List<E> addedItems = getItemsByIds(e.getAdded());
            List<E> removedItems = getItemsByIds(e.getRemoved());
            List<E> selectedItems = getItemsByIds(e.getSelected());
            SelectionEvent<E> event = new SelectionEvent<>(WebDataGrid.this, addedItems, removedItems, selectedItems);
            // noinspection unchecked
            getEventRouter().fireEvent(SelectionListener.class, SelectionListener::selected, event);
        }
    });
    component.addShortcutListener(new ShortcutListener("dataGridEnter", KeyCode.ENTER, null) {

        @Override
        public void handleAction(Object sender, Object target) {
            if (target == WebDataGrid.this.component) {
                if (WebDataGrid.this.isEditorEnabled()) {
                    // since it's the default shortcut to open editor
                    return;
                }
                if (enterPressAction != null) {
                    enterPressAction.actionPerform(WebDataGrid.this);
                } else {
                    handleDoubleClickAction();
                }
            }
        }
    });
    component.addItemClickListener(e -> {
        if (e.isDoubleClick() && e.getItem() != null && !WebDataGrid.this.isEditorEnabled()) {
            // note: for now Grid doesn't send double click if editor is enabled,
            // but it's better to handle it manually
            handleDoubleClickAction();
        }
        if (getEventRouter().hasListeners(ItemClickListener.class)) {
            MouseEventDetails mouseEventDetails = WebWrapperUtils.toMouseEventDetails(e);
            // noinspection unchecked
            E item = (E) datasource.getItem(e.getItemId());
            if (item == null) {
                // datasource, so we don't want to send such event because it's useless
                return;
            }
            Column column = getColumnByPropertyId(e.getPropertyId());
            ItemClickEvent<E> event = new ItemClickEvent<>(WebDataGrid.this, mouseEventDetails, item, e.getItemId(), column != null ? column.getId() : null);
            // noinspection unchecked
            getEventRouter().fireEvent(ItemClickListener.class, ItemClickListener::onItemClick, event);
        }
    });
    component.addColumnReorderListener(e -> {
        if (e.isUserOriginated()) {
            // Grid doesn't know about columns hidden by security permissions,
            // so we need to return them back to they previous positions
            columnsOrder = restoreColumnsOrder(getColumnsOrderInternal());
            if (getEventRouter().hasListeners(ColumnReorderListener.class)) {
                ColumnReorderEvent event = new ColumnReorderEvent(WebDataGrid.this);
                getEventRouter().fireEvent(ColumnReorderListener.class, ColumnReorderListener::columnReordered, event);
            }
        }
    });
    componentComposition = new GridComposition();
    componentComposition.setPrimaryStyleName("c-data-grid-composition");
    componentComposition.setGrid(component);
    componentComposition.addComponent(component);
    componentComposition.setWidthUndefined();
    component.setSizeUndefined();
    component.setHeightMode(HeightMode.UNDEFINED);
    component.setRowStyleGenerator(createRowStyleGenerator());
    component.setCellStyleGenerator(createCellStyleGenerator());
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) ShortcutListener(com.vaadin.event.ShortcutListener) GeneratedPropertyContainer(com.vaadin.data.util.GeneratedPropertyContainer)

Example 2 with GeneratedPropertyContainer

use of com.vaadin.data.util.GeneratedPropertyContainer in project cuba by cuba-platform.

the class ComponentGridDecorator method initGpc.

/**
 * Replaces the current grid container with a {@link GeneratedPropertyContainer}
 * while preserving the {@link Grid.DetailsGenerator}.
 */
private void initGpc() {
    gpc = new GeneratedPropertyContainer(grid.getContainerDataSource());
    Grid.DetailsGenerator details = grid.getDetailsGenerator();
    grid.setContainerDataSource(gpc);
    grid.setDetailsGenerator(details);
}
Also used : GeneratedPropertyContainer(com.vaadin.data.util.GeneratedPropertyContainer) Grid(com.vaadin.ui.Grid)

Example 3 with GeneratedPropertyContainer

use of com.vaadin.data.util.GeneratedPropertyContainer in project cuba by cuba-platform.

the class WebDataGrid method setDatasource.

@Override
public void setDatasource(CollectionDatasource datasource) {
    checkNotNullArgument(datasource, "datasource is null");
    if (!(datasource instanceof CollectionDatasource.Indexed)) {
        throw new IllegalArgumentException("Datasource must implement " + "com.haulmont.cuba.gui.data.CollectionDatasource.Indexed");
    }
    if (this.datasource != null) {
        if (!this.datasource.getMetaClass().equals(datasource.getMetaClass())) {
            throw new IllegalArgumentException("The new datasource must correspond to the same MetaClass");
        }
        if (collectionDsListenersWrapper != null) {
            collectionDsListenersWrapper.unbind(this.datasource);
            if (containerDatasource != null) {
                containerDatasource.unsubscribe();
                containerDatasource = null;
            }
        }
    }
    addInitialColumns(datasource);
    this.datasource = datasource;
    List<Column> visibleColumnsOrder = getInitialVisibleColumns();
    if (collectionDsListenersWrapper == null) {
        collectionDsListenersWrapper = new CollectionDsListenersWrapper();
    }
    component.removeAllColumns();
    containerDatasource = createContainerDatasource((CollectionDatasource.Indexed) datasource, getPropertyColumns(), collectionDsListenersWrapper);
    containerWrapper = new GeneratedPropertyContainer(containerDatasource);
    component.setContainerDataSource(containerWrapper);
    createStubsForGeneratedColumns();
    // mark columns hidden by security permissions as visible = false
    // and remove Grid.Column to prevent its property changing
    columnsOrder.stream().filter(column -> !visibleColumnsOrder.contains(column)).forEach(column -> {
        ColumnImpl columnImpl = (ColumnImpl) column;
        columnImpl.setVisible(false);
        columnImpl.setGridColumn(null);
    });
    for (Column column : visibleColumnsOrder) {
        Grid.Column gridColumn = component.getColumn(((ColumnImpl) column).getColumnPropertyId());
        setupGridColumnProperties(gridColumn, column);
    }
    component.setColumnOrder(getColumnPropertyIds());
    initShowInfoAction(datasource);
    if (rowsCount != null) {
        rowsCount.setDatasource(datasource);
    }
    collectionDsListenersWrapper.bind(datasource);
    refreshActionsState();
    if (!canBeSorted(datasource)) {
        setSortable(false);
    }
    assignAutoDebugId();
    component.setCollectionDatasource(datasource);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Datasource(com.haulmont.cuba.gui.data.Datasource) Dom4j(com.haulmont.bali.util.Dom4j) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) IconResolver(com.haulmont.cuba.web.gui.icons.IconResolver) Window(com.haulmont.cuba.gui.components.Window) AppUI(com.haulmont.cuba.web.AppUI) MenuItem(com.vaadin.addon.contextmenu.MenuItem) EventRouter(com.haulmont.bali.events.EventRouter) SecuredActionsHolder(com.haulmont.cuba.gui.components.Component.SecuredActionsHolder) FormatterBasedConverter(com.haulmont.cuba.web.toolkit.ui.converters.FormatterBasedConverter) Preconditions.checkNotNullArgument(com.haulmont.bali.util.Preconditions.checkNotNullArgument) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) PropertyDatasource(com.haulmont.cuba.gui.data.PropertyDatasource) ImmutableMap(com.google.common.collect.ImmutableMap) PropertyValueGenerator(com.vaadin.data.util.PropertyValueGenerator) App(com.haulmont.cuba.web.App) Collectors(java.util.stream.Collectors) ObjectToObjectConverter(com.haulmont.cuba.web.toolkit.ui.converters.ObjectToObjectConverter) PropertyChangeListener(java.beans.PropertyChangeListener) ComponentsHelper.findActionById(com.haulmont.cuba.gui.ComponentsHelper.findActionById) GeneratedPropertyContainer(com.vaadin.data.util.GeneratedPropertyContainer) ThemeConstants(com.haulmont.cuba.gui.theme.ThemeConstants) ClientConfig(com.haulmont.cuba.client.ClientConfig) ComponentsHelper(com.haulmont.cuba.gui.ComponentsHelper) com.haulmont.cuba.web.gui.components.renderers(com.haulmont.cuba.web.gui.components.renderers) java.util(java.util) CollectionFormatter(com.haulmont.cuba.gui.components.formatters.CollectionFormatter) GridContextMenuOpenListener(com.vaadin.addon.contextmenu.GridContextMenu.GridContextMenuOpenListener) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) CssLayout(com.vaadin.ui.CssLayout) MetaClass(com.haulmont.chile.core.model.MetaClass) CollectionUtils(org.apache.commons.collections4.CollectionUtils) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Validator(com.vaadin.data.Validator) Field(com.haulmont.cuba.gui.components.Field) SortableDataGridIndexedCollectionDsWrapper(com.haulmont.cuba.web.gui.data.SortableDataGridIndexedCollectionDsWrapper) com.haulmont.cuba.gui.data.impl(com.haulmont.cuba.gui.data.impl) DataGridIndexedCollectionDsWrapper(com.haulmont.cuba.web.gui.data.DataGridIndexedCollectionDsWrapper) com.haulmont.cuba.web.toolkit.ui(com.haulmont.cuba.web.toolkit.ui) KeyCode(com.vaadin.event.ShortcutAction.KeyCode) Nullable(javax.annotation.Nullable) Item(com.vaadin.data.Item) Formatter(com.haulmont.cuba.gui.components.Formatter) DataGridContainer(com.haulmont.cuba.web.toolkit.data.DataGridContainer) MetaProperty(com.haulmont.chile.core.model.MetaProperty) StringToObjectConverter(com.haulmont.cuba.web.toolkit.ui.converters.StringToObjectConverter) ErrorMessage(com.vaadin.server.ErrorMessage) Preconditions(com.haulmont.bali.util.Preconditions) HeightMode(com.vaadin.shared.ui.grid.HeightMode) ShortcutListener(com.vaadin.event.ShortcutListener) Menu(com.vaadin.addon.contextmenu.Menu) Element(org.dom4j.Element) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) YesNoIconConverter(com.haulmont.cuba.web.toolkit.ui.converters.YesNoIconConverter) Entity(com.haulmont.cuba.core.entity.Entity) com.vaadin.ui(com.vaadin.ui) Component(com.vaadin.ui.Component) GeneratedPropertyContainer(com.vaadin.data.util.GeneratedPropertyContainer)

Example 4 with GeneratedPropertyContainer

use of com.vaadin.data.util.GeneratedPropertyContainer in project VaadinUtils by rlsutton1.

the class GridHeadingPropertySet method wrapGridContainer.

private GeneratedPropertyContainer wrapGridContainer(final Grid grid) {
    Indexed gridContainer = grid.getContainerDataSource();
    if (!(gridContainer instanceof GeneratedPropertyContainer)) {
        final GeneratedPropertyContainer gpc = new GeneratedPropertyContainer(gridContainer);
        grid.setContainerDataSource(gpc);
        gridContainer = gpc;
    }
    return (GeneratedPropertyContainer) gridContainer;
}
Also used : GeneratedPropertyContainer(com.vaadin.data.util.GeneratedPropertyContainer) Indexed(com.vaadin.data.Container.Indexed)

Example 5 with GeneratedPropertyContainer

use of com.vaadin.data.util.GeneratedPropertyContainer in project VaadinUtils by rlsutton1.

the class GridHeadingPropertySet method applyToGrid.

/**
 * @param grid
 * @param uniqueId
 *            - an id for this layout/grid combination, it is used to
 *            identify stored column widths in a key value map
 */
public void applyToGrid(final Grid grid, final String uniqueId) {
    this.grid = grid;
    this.uniqueId = uniqueId;
    try {
        // Changing the bound container at this point can cause issues
        // elsewhere, so we avoid it if possible
        Indexed gridContainer = grid.getContainerDataSource();
        for (GridHeadingToPropertyId column : getColumns()) {
            if (column.isGenerated()) {
                gridContainer = wrapGridContainer(grid);
                break;
            }
        }
        final List<String> colsToShow = new LinkedList<>();
        for (GridHeadingToPropertyId column : getColumns()) {
            final String propertyId = column.getPropertyId();
            if (column.isGenerated()) {
                final PropertyValueGenerator<?> columnGenerator = column.getColumnGenerator();
                ((GeneratedPropertyContainer) gridContainer).addGeneratedProperty(propertyId, columnGenerator);
                final Column gridColumn = grid.getColumn(propertyId);
                if (columnGenerator.getType() == String.class && gridColumn.getRenderer() instanceof TextRenderer) {
                    gridColumn.setRenderer(new HtmlRenderer(), null);
                } else if (columnGenerator.getType() == Component.class) {
                    gridColumn.setRenderer(new ComponentRenderer());
                }
            } else {
                Preconditions.checkArgument(grid.getContainerDataSource().getContainerPropertyIds().contains(propertyId), propertyId + " is not a valid property id, valid property ids are " + grid.getContainerDataSource().getContainerPropertyIds().toString());
            }
            colsToShow.add(propertyId);
            final Column gridColumn = grid.getColumn(propertyId);
            if (column.getRenderer() != null) {
                gridColumn.setRenderer(column.getRenderer());
            }
            if (column.getConverter() != null) {
                gridColumn.setConverter(column.getConverter());
            }
            gridColumn.setHeaderCaption(column.getHeader());
            if (column.getWidth() != null) {
                gridColumn.setWidth(column.getWidth());
            } else {
                gridColumn.setExpandRatio(1);
                gridColumn.setMinimumWidth(1);
            }
            if (column.isLocked()) {
                gridColumn.setHidable(false);
            } else {
                gridColumn.setHidable(true);
                if (!column.isVisibleByDefault()) {
                    gridColumn.setHidden(true);
                }
            }
        }
        grid.setColumns(colsToShow.toArray());
        if (eraseSavedConfig) {
            eraseSavedConfig(uniqueId);
        }
        if (!deferLoadSettings) {
            configureSaveColumnWidths(grid, uniqueId);
            configureSaveColumnOrder(grid, uniqueId);
            configureSaveColumnVisible(grid, uniqueId);
        }
    } catch (Exception e) {
        logger.error(e, e);
    }
}
Also used : LinkedList(java.util.LinkedList) ComponentRenderer(de.datenhahn.vaadin.componentrenderer.ComponentRenderer) Column(com.vaadin.ui.Grid.Column) GeneratedPropertyContainer(com.vaadin.data.util.GeneratedPropertyContainer) HtmlRenderer(com.vaadin.ui.renderers.HtmlRenderer) Component(com.vaadin.ui.Component) Indexed(com.vaadin.data.Container.Indexed) TextRenderer(com.vaadin.ui.renderers.TextRenderer)

Aggregations

GeneratedPropertyContainer (com.vaadin.data.util.GeneratedPropertyContainer)6 Entity (com.haulmont.cuba.core.entity.Entity)2 Indexed (com.vaadin.data.Container.Indexed)2 Component (com.vaadin.ui.Component)2 Grid (com.vaadin.ui.Grid)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 EventRouter (com.haulmont.bali.events.EventRouter)1 Dom4j (com.haulmont.bali.util.Dom4j)1 Preconditions (com.haulmont.bali.util.Preconditions)1 Preconditions.checkNotNullArgument (com.haulmont.bali.util.Preconditions.checkNotNullArgument)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 ComponentsHelper (com.haulmont.cuba.gui.ComponentsHelper)1 ComponentsHelper.findActionById (com.haulmont.cuba.gui.ComponentsHelper.findActionById)1 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)1 SecuredActionsHolder (com.haulmont.cuba.gui.components.Component.SecuredActionsHolder)1 Field (com.haulmont.cuba.gui.components.Field)1