Search in sources :

Example 1 with GeneratedPropertyListContainer

use of org.vaadin.viritin.grid.GeneratedPropertyListContainer in project VaadinUtils by rlsutton1.

the class GridHeadingV2PropertySet method addActionColumn.

@SuppressWarnings("unchecked")
private void addActionColumn() {
    ((GeneratedPropertyListContainer<E>) grid.getContainerDataSource()).addGeneratedProperty(ACTION_MENU_PROPERTY_ID, getActionMenuColumnGenerator());
    final Column actionMenuColumn = grid.addColumn(ACTION_MENU_PROPERTY_ID);
    grid.setFrozenColumnCount(1);
    actionMenuColumn.setHeaderCaption("");
    actionMenuColumn.setRenderer(new ComponentRenderer());
    actionMenuColumn.setWidth(40);
    actionMenuColumn.setResizable(false);
    grid.setCellStyleGenerator(new CellStyleGenerator() {

        /**
         */
        private static final long serialVersionUID = 1L;

        @Override
        public String getStyle(CellReference event) {
            String style = "";
            if (event.getPropertyId().equals(ACTION_MENU_PROPERTY_ID)) {
                style = "grid-actionmenu";
            }
            return style;
        }
    });
}
Also used : ComponentRenderer(de.datenhahn.vaadin.componentrenderer.ComponentRenderer) Column(com.vaadin.ui.Grid.Column) CellStyleGenerator(com.vaadin.ui.Grid.CellStyleGenerator) CellReference(com.vaadin.ui.Grid.CellReference) GeneratedPropertyListContainer(org.vaadin.viritin.grid.GeneratedPropertyListContainer)

Example 2 with GeneratedPropertyListContainer

use of org.vaadin.viritin.grid.GeneratedPropertyListContainer in project VaadinUtils by rlsutton1.

the class GridHeadingV2PropertySet 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
 */
@Override
@SuppressWarnings("unchecked")
public void applyToGrid(final Class<E> entityClass, final Grid grid, final String uniqueId) {
    this.grid = grid;
    this.uniqueId = uniqueId;
    final List<String> columnsToShow = new LinkedList<>();
    try {
        // Avoid changing the container data source if we can
        Indexed gridContainer = grid.getContainerDataSource();
        if (actionColumnEnabled && actionMenuProvider != null) {
            gridContainer = wrapGridContainer(entityClass, grid);
            addActionColumn();
            columnsToShow.add(ACTION_MENU_PROPERTY_ID);
        } else {
            for (GridHeadingV2ToPropertyId column : getColumns()) {
                if (column.isGenerated()) {
                    gridContainer = wrapGridContainer(entityClass, grid);
                    break;
                }
            }
        }
        for (GridHeadingV2ToPropertyId column : getColumns()) {
            final String propertyId = column.getPropertyId();
            if (column.isGenerated()) {
                final PropertyValueGenerator<?> columnGenerator = column.getColumnGenerator();
                ((GeneratedPropertyListContainer<E>) gridContainer).addGeneratedProperty(propertyId, columnGenerator);
                // already exists, then we shouldn't try to add it again
                if (grid.getColumn(propertyId) == null) {
                    grid.addColumn(propertyId);
                }
                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());
            }
            columnsToShow.add(propertyId);
            final Column gridColumn = grid.getColumn(propertyId);
            if (column.getRenderer() != null) {
                gridColumn.setRenderer(column.getRenderer(), null);
            } else if (gridContainer.getType(propertyId) == Boolean.class) {
                // Show a tick/cross if column is a boolean
                gridColumn.setRenderer(new HtmlRenderer(), new StringToBooleanConverter(FontAwesome.CHECK.getHtml(), FontAwesome.TIMES.getHtml()));
            }
            gridColumn.setHeaderCaption(column.getHeader());
            columnWidthTypes.put(propertyId, column.getWidthType());
            if (column.getWidth() != null) {
                setColumnWidth(gridColumn, column.getWidth());
                if (column.getWidthType().equals(WidthType.FIXED)) {
                    gridColumn.setResizable(false);
                }
            } else {
                gridColumn.setExpandRatio(1);
                gridColumn.setMinimumWidth(1);
            }
            if (column.isVisibilityLocked()) {
                gridColumn.setHidable(false);
            } else {
                gridColumn.setHidable(true);
                if (!column.isVisible()) {
                    gridColumn.setHidden(true);
                }
            }
        }
        grid.setColumns(columnsToShow.toArray());
        if (eraseSavedConfig) {
            eraseSavedConfig(uniqueId);
        }
        if (!deferLoadSettings) {
            configureSaveColumnWidths(grid);
            configureSaveColumnOrder(grid);
            configureSaveColumnVisible(grid);
        }
    } catch (Exception e) {
        logger.error(e, e);
    }
}
Also used : LinkedList(java.util.LinkedList) GeneratedPropertyListContainer(org.vaadin.viritin.grid.GeneratedPropertyListContainer) StringToBooleanConverter(com.vaadin.data.util.converter.StringToBooleanConverter) ComponentRenderer(de.datenhahn.vaadin.componentrenderer.ComponentRenderer) Column(com.vaadin.ui.Grid.Column) HtmlRenderer(com.vaadin.ui.renderers.HtmlRenderer) Component(com.vaadin.ui.Component) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Indexed(com.vaadin.data.Container.Indexed) TextRenderer(com.vaadin.ui.renderers.TextRenderer)

Example 3 with GeneratedPropertyListContainer

use of org.vaadin.viritin.grid.GeneratedPropertyListContainer in project VaadinUtils by rlsutton1.

the class GridHeadingV2PropertySet method wrapGridContainer.

@SuppressWarnings("unchecked")
private GeneratedPropertyListContainer<E> wrapGridContainer(final Class<E> entityClass, final Grid grid) {
    final Indexed gridContainer = grid.getContainerDataSource();
    if (gridContainer instanceof GeneratedPropertyListContainer) {
        return (GeneratedPropertyListContainer<E>) gridContainer;
    }
    final GeneratedPropertyListContainer<E> gplc = new GeneratedPropertyListContainer<>(entityClass);
    gplc.setCollection((Collection<E>) gridContainer.getItemIds());
    final Collection<?> containerPropertyIds = gridContainer.getContainerPropertyIds();
    if (!containerPropertyIds.isEmpty()) {
        gplc.setContainerPropertyIds(containerPropertyIds.toArray(new String[containerPropertyIds.size()]));
    }
    grid.setContainerDataSource(gplc);
    return gplc;
}
Also used : Indexed(com.vaadin.data.Container.Indexed) GeneratedPropertyListContainer(org.vaadin.viritin.grid.GeneratedPropertyListContainer)

Aggregations

GeneratedPropertyListContainer (org.vaadin.viritin.grid.GeneratedPropertyListContainer)3 Indexed (com.vaadin.data.Container.Indexed)2 Column (com.vaadin.ui.Grid.Column)2 ComponentRenderer (de.datenhahn.vaadin.componentrenderer.ComponentRenderer)2 StringToBooleanConverter (com.vaadin.data.util.converter.StringToBooleanConverter)1 Component (com.vaadin.ui.Component)1 CellReference (com.vaadin.ui.Grid.CellReference)1 CellStyleGenerator (com.vaadin.ui.Grid.CellStyleGenerator)1 HtmlRenderer (com.vaadin.ui.renderers.HtmlRenderer)1 TextRenderer (com.vaadin.ui.renderers.TextRenderer)1 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1