Search in sources :

Example 6 with Column

use of com.vaadin.ui.Grid.Column in project VaadinUtils by rlsutton1.

the class BasePortal method calculateColumnOrder.

/**
 * If a column order has already been saved for a user, but the columns for
 * a grid have been modified, then we need to remove any columns that no
 * longer exist and add any new columns to the list of visible columns.
 *
 * @param availableColumns
 *            the columns that are available in the table
 * @param parsedColumns
 *            the column order that has been restored from preferences
 * @return the calculated order of columns with old removed and new added
 */
private Object[] calculateColumnOrder(final List<Column> availableColumns, final Object[] parsedColumns) {
    final List<Object> availableList = new ArrayList<>(availableColumns.size());
    for (Column column : availableColumns) {
        availableList.add(column.getPropertyId());
    }
    final List<Object> parsedList = new ArrayList<>(Arrays.asList(parsedColumns));
    // Remove old columns
    parsedList.retainAll(availableList);
    // Add new columns in the same index position as they were added to the
    // table in
    final List<Object> newList = new ArrayList<>(availableList);
    newList.removeAll(parsedList);
    for (Object column : newList) {
        parsedList.add(availableList.indexOf(column), column);
    }
    return parsedList.toArray();
}
Also used : Column(com.vaadin.ui.Grid.Column) ArrayList(java.util.ArrayList)

Example 7 with Column

use of com.vaadin.ui.Grid.Column in project VaadinUtils by rlsutton1.

the class GridHeadingPropertySet method configureSaveColumnVisible.

private void configureSaveColumnVisible(final Grid grid, final String uniqueId) {
    final String keyStub = uniqueId + "-visible";
    for (GridHeadingToPropertyId id : getColumns()) {
        final String setVisible = UserSettingsStorageFactory.getUserSettingsStorage().get(keyStub + "-" + id.getPropertyId());
        if (setVisible != null && !setVisible.isEmpty()) {
            grid.getColumn(id.getPropertyId()).setHidden(!Boolean.parseBoolean(setVisible));
        }
    }
    grid.addColumnVisibilityChangeListener(new ColumnVisibilityChangeListener() {

        private static final long serialVersionUID = -9082974567948595049L;

        @Override
        public void columnVisibilityChanged(ColumnVisibilityChangeEvent event) {
            final Column column = event.getColumn();
            final boolean isVisible = !column.isHidden();
            UserSettingsStorageFactory.getUserSettingsStorage().store(keyStub + "-" + column.getPropertyId(), "" + isVisible);
        }
    });
}
Also used : Column(com.vaadin.ui.Grid.Column) ColumnVisibilityChangeEvent(com.vaadin.ui.Grid.ColumnVisibilityChangeEvent) ColumnVisibilityChangeListener(com.vaadin.ui.Grid.ColumnVisibilityChangeListener)

Example 8 with Column

use of com.vaadin.ui.Grid.Column 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)

Example 9 with Column

use of com.vaadin.ui.Grid.Column in project VaadinUtils by rlsutton1.

the class GridHeadingPropertySet method calculateColumnOrder.

/**
 * If a column order has already been saved for a user, but the columns for
 * a grid have been modified, then we need to remove any columns that no
 * longer exist and add any new columns to the list of visible columns.
 *
 * @param availableColumns
 *            the columns that are available in the table
 * @param parsedColumns
 *            the column order that has been restored from preferences
 * @return the calculated order of columns with old removed and new added
 */
private Object[] calculateColumnOrder(final List<Column> availableColumns, final Object[] parsedColumns) {
    final List<Object> availableList = new ArrayList<>(availableColumns.size());
    for (Column column : availableColumns) {
        availableList.add(column.getPropertyId());
    }
    final List<Object> parsedList = new ArrayList<>(Arrays.asList(parsedColumns));
    // Remove old columns
    parsedList.retainAll(availableList);
    // Add new columns in the same index position as they were added to the
    // table in
    final List<Object> newList = new ArrayList<>(availableList);
    newList.removeAll(parsedList);
    for (Object column : newList) {
        parsedList.add(availableList.indexOf(column), column);
    }
    return parsedList.toArray();
}
Also used : Column(com.vaadin.ui.Grid.Column) ArrayList(java.util.ArrayList)

Example 10 with Column

use of com.vaadin.ui.Grid.Column 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)

Aggregations

Column (com.vaadin.ui.Grid.Column)16 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)6 ColumnVisibilityChangeEvent (com.vaadin.ui.Grid.ColumnVisibilityChangeEvent)4 ColumnVisibilityChangeListener (com.vaadin.ui.Grid.ColumnVisibilityChangeListener)4 List (java.util.List)4 ColumnReorderEvent (com.vaadin.ui.Grid.ColumnReorderEvent)3 ColumnReorderListener (com.vaadin.ui.Grid.ColumnReorderListener)3 ComponentRenderer (de.datenhahn.vaadin.componentrenderer.ComponentRenderer)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ComponentResizeEvent (com.ejt.vaadin.sizereporter.ComponentResizeEvent)2 ComponentResizeListener (com.ejt.vaadin.sizereporter.ComponentResizeListener)2 SizeReporter (com.ejt.vaadin.sizereporter.SizeReporter)2 Indexed (com.vaadin.data.Container.Indexed)2 Component (com.vaadin.ui.Component)2 ColumnResizeEvent (com.vaadin.ui.Grid.ColumnResizeEvent)2 ColumnResizeListener (com.vaadin.ui.Grid.ColumnResizeListener)2 HtmlRenderer (com.vaadin.ui.renderers.HtmlRenderer)2 TextRenderer (com.vaadin.ui.renderers.TextRenderer)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2