Search in sources :

Example 1 with Indexed

use of com.vaadin.data.Container.Indexed in project GridFastNavigation by TatuLund.

the class DemoUI method initNavigation.

private void initNavigation(final Grid grid) {
    FastNavigation nav = new FastNavigation(grid, false, true);
    nav.setChangeColumnAfterLastRow(true);
    nav.addRowEditListener(new RowEditListener() {

        @Override
        public void onEvent(RowEditEvent event) {
            int rowIndex = event.getRowIndex();
            if (rowIndex >= 0) {
                Indexed ds = grid.getContainerDataSource();
                Object itemId = event.getItemId();
                printChangedRow(rowIndex, ds, itemId);
            }
        }
    });
    // Open with F2
    nav.addEditorOpenShortcut(KeyCode.F2);
    writeOutput("Editor can also be opened with F2");
    // Close with F3
    nav.addEditorCloseShortcut(KeyCode.F3);
    writeOutput("Editor can also be closed with F3");
    // Row focus change
    nav.addRowFocusListener(new RowFocusListener() {

        @Override
        public void onEvent(RowFocusEvent event) {
            int row = event.getRow();
            writeOutput("Focus moved to row " + event.getRow());
            grid.select(event.getItemId());
        }
    });
    writeOutput("Added row focus change listener");
    // Cell focus change
    nav.addCellFocusListener(new CellFocusListener() {

        @Override
        public void onEvent(CellFocusEvent event) {
            int row = event.getRow();
            int col = event.getColumn();
            writeOutput("Focus moved to cell [" + row + ", " + col + " ]");
        }
    });
    writeOutput("Added cell focus change listener");
    // Listening to opening of editor
    nav.addEditorOpenListener(new EditorOpenListener() {

        @Override
        public void onEvent(EditorOpenEvent event) {
            int row = event.getRow();
            writeOutput("Editor opened on row " + row + " at column " + event.getColumn());
        }
    });
    writeOutput("Added editor open listener");
    // Listening to closing of editor
    nav.addEditorCloseListener(new EditorCloseListener() {

        @Override
        public void onEvent(EditorCloseEvent event) {
            writeOutput("Editor closed on row " + event.getRow() + ", column " + event.getColumn() + ", " + (event.wasCancelled() ? "user cancelled change" : "user saved change"));
        }
    });
    writeOutput("Added editor close listener");
    nav.addClickOutListener(new ClickOutListener() {

        @Override
        public void onEvent(ClickOutEvent event) {
            writeOutput("User click outside Grid: " + event.getSource().toString());
        }
    });
}
Also used : FastNavigation(org.vaadin.patrik.FastNavigation) RowFocusEvent(org.vaadin.patrik.events.RowFocusEvent) ClickOutEvent(org.vaadin.patrik.events.ClickOutEvent) EditorOpenListener(org.vaadin.patrik.FastNavigation.EditorOpenListener) ClickOutListener(org.vaadin.patrik.FastNavigation.ClickOutListener) RowEditEvent(org.vaadin.patrik.events.RowEditEvent) EditorCloseListener(org.vaadin.patrik.FastNavigation.EditorCloseListener) EditorCloseEvent(org.vaadin.patrik.events.EditorCloseEvent) RowEditListener(org.vaadin.patrik.FastNavigation.RowEditListener) EditorOpenEvent(org.vaadin.patrik.events.EditorOpenEvent) RowFocusListener(org.vaadin.patrik.FastNavigation.RowFocusListener) Indexed(com.vaadin.data.Container.Indexed) CellFocusEvent(org.vaadin.patrik.events.CellFocusEvent) CellFocusListener(org.vaadin.patrik.FastNavigation.CellFocusListener)

Example 2 with Indexed

use of com.vaadin.data.Container.Indexed in project GridFastNavigation by TatuLund.

the class FastNavigation method setupFastNavigation.

private void setupFastNavigation(final Grid g, boolean changeColumnOnEnter, boolean dispatchEditEventOnBlur) {
    getState().changeColumnOnEnter = changeColumnOnEnter;
    getState().dispatchEditEventOnBlur = dispatchEditEventOnBlur;
    g.setEditorBuffered(false);
    g.setEditorEnabled(true);
    registerRpc(new FastNavigationServerRPC() {

        @Override
        public void rowUpdated(int rowIndex) {
            Object itemId = getItemIdByRowIndex(g, rowIndex);
            rowEditListeners.dispatch(new RowEditEvent(g, rowIndex, itemId));
        }

        private Object getItemIdByRowIndex(final Grid g, int rowIndex) {
            Indexed ds = g.getContainerDataSource();
            Object itemId = null;
            if (rowIndex >= 0 && (ds.size() > 0))
                itemId = ds.getIdByIndex(rowIndex);
            return itemId;
        }

        @Override
        public void cellUpdated(int rowIndex, int colIndex, String newData) {
            Object itemId = getItemIdByRowIndex(g, rowIndex);
            cellEditListeners.dispatch(new CellEditEvent(g, rowIndex, colIndex, newData, itemId));
        }

        @Override
        public void focusUpdated(int rowIndex, int colIndex) {
            Object itemId = getItemIdByRowIndex(g, rowIndex);
            if (hasRowFocusListener && rowIndex != lastFocusedRow) {
                rowFocusListeners.dispatch(new RowFocusEvent(g, rowIndex, itemId));
            }
            if (hasCellFocusListener && (rowIndex != lastFocusedRow || colIndex != lastFocusedCol)) {
                cellFocusListeners.dispatch(new CellFocusEvent(g, rowIndex, colIndex, lastFocusedRow == rowIndex, lastFocusedCol == colIndex, itemId));
            }
            lastFocusedRow = rowIndex;
            lastFocusedCol = colIndex;
        }

        @Override
        public void editorOpened(int rowIndex, int colIndex, int lockId) {
            Object itemId = getItemIdByRowIndex(g, rowIndex);
            EditorOpenEvent ev = new EditorOpenEvent(g, rowIndex, colIndex, itemId);
            editorOpenListeners.dispatch(ev);
            // Update disabled columns or readonly fields status if changed dynamically
            ArrayList<Integer> disabledColumns = new ArrayList<Integer>();
            for (int i = 0; i < g.getColumns().size(); i++) {
                if (!g.getColumns().get(i).isEditable()) {
                    disabledColumns.add(i);
                } else if ((g.getColumns().get(i).getEditorField() != null) && g.getColumns().get(i).getEditorField().isReadOnly()) {
                    disabledColumns.add(i);
                }
            }
            getRPC().setDisabledColumns(disabledColumns);
            getRPC().unlockEditor(lockId);
        }

        @Override
        public void ping() {
            getLogger().info("Received ping");
        }

        @Override
        public void editorClosed(int rowIndex, int colIndex, boolean wasCancelled) {
            editorCloseListeners.dispatch(new EditorCloseEvent(g, rowIndex, colIndex, wasCancelled));
        }

        @Override
        public void clickOut() {
            clickOutListeners.dispatch(new ClickOutEvent(g));
        }
    }, FastNavigationServerRPC.class);
    extend(g);
}
Also used : RowFocusEvent(org.vaadin.patrik.events.RowFocusEvent) ClickOutEvent(org.vaadin.patrik.events.ClickOutEvent) Grid(com.vaadin.ui.Grid) ArrayList(java.util.ArrayList) RowEditEvent(org.vaadin.patrik.events.RowEditEvent) CellEditEvent(org.vaadin.patrik.events.CellEditEvent) EditorCloseEvent(org.vaadin.patrik.events.EditorCloseEvent) EditorOpenEvent(org.vaadin.patrik.events.EditorOpenEvent) FastNavigationServerRPC(org.vaadin.patrik.shared.FastNavigationServerRPC) Indexed(com.vaadin.data.Container.Indexed) CellFocusEvent(org.vaadin.patrik.events.CellFocusEvent)

Example 3 with Indexed

use of com.vaadin.data.Container.Indexed 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 4 with Indexed

use of com.vaadin.data.Container.Indexed 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 5 with Indexed

use of com.vaadin.data.Container.Indexed 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

Indexed (com.vaadin.data.Container.Indexed)6 GeneratedPropertyContainer (com.vaadin.data.util.GeneratedPropertyContainer)2 Component (com.vaadin.ui.Component)2 Column (com.vaadin.ui.Grid.Column)2 HtmlRenderer (com.vaadin.ui.renderers.HtmlRenderer)2 TextRenderer (com.vaadin.ui.renderers.TextRenderer)2 ComponentRenderer (de.datenhahn.vaadin.componentrenderer.ComponentRenderer)2 LinkedList (java.util.LinkedList)2 CellFocusEvent (org.vaadin.patrik.events.CellFocusEvent)2 ClickOutEvent (org.vaadin.patrik.events.ClickOutEvent)2 EditorCloseEvent (org.vaadin.patrik.events.EditorCloseEvent)2 EditorOpenEvent (org.vaadin.patrik.events.EditorOpenEvent)2 RowEditEvent (org.vaadin.patrik.events.RowEditEvent)2 RowFocusEvent (org.vaadin.patrik.events.RowFocusEvent)2 GeneratedPropertyListContainer (org.vaadin.viritin.grid.GeneratedPropertyListContainer)2 StringToBooleanConverter (com.vaadin.data.util.converter.StringToBooleanConverter)1 Grid (com.vaadin.ui.Grid)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 FastNavigation (org.vaadin.patrik.FastNavigation)1