Search in sources :

Example 1 with ColumnVisibilityChangeListener

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

the class GridHeadingV2PropertySet method configureSaveColumnVisible.

private void configureSaveColumnVisible(final Grid grid) {
    final String keyStub = uniqueId + "-visible";
    for (GridHeadingV2ToPropertyId 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 = 1L;

        @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 2 with ColumnVisibilityChangeListener

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

the class BasePortal method setupGridColumnVisibility.

private void setupGridColumnVisibility(final Grid grid, final String baseVisableKey) {
    if (columnVisibilityListeners.get(grid) != null) {
        grid.removeColumnVisibilityChangeListener(columnVisibilityListeners.get(grid));
    } else {
        ColumnVisibilityChangeListener columnVisibilityListener = new ColumnVisibilityChangeListener() {

            private static final long serialVersionUID = -9082974567948595049L;

            @Override
            public void columnVisibilityChanged(ColumnVisibilityChangeEvent event) {
                final Column column = event.getColumn();
                final boolean isVisible = !column.isHidden();
                int value = 0;
                if (isVisible) {
                    value = 1;
                }
                getConfigDelegate().setValue(getPortal(), baseVisableKey + column.getPropertyId(), value);
            }
        };
        columnVisibilityListeners.put(grid, columnVisibilityListener);
    }
    for (Entry<String, Integer> value : getConfigDelegate().getValuesLikeInt(getPortal(), baseVisableKey).entrySet()) {
        try {
            String key = value.getKey().substring(baseVisableKey.length(), value.getKey().length());
            Integer visable = value.getValue();
            grid.getColumn(key).setHidden(visable == 0);
        } catch (Exception e) {
            logger.warn(e);
        }
    }
    grid.addColumnVisibilityChangeListener(columnVisibilityListeners.get(grid));
}
Also used : Column(com.vaadin.ui.Grid.Column) ColumnVisibilityChangeEvent(com.vaadin.ui.Grid.ColumnVisibilityChangeEvent) ColumnVisibilityChangeListener(com.vaadin.ui.Grid.ColumnVisibilityChangeListener)

Example 3 with ColumnVisibilityChangeListener

use of com.vaadin.ui.Grid.ColumnVisibilityChangeListener 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 4 with ColumnVisibilityChangeListener

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

the class GridHeadingV2PropertySet method configureDynamicColumnWidth.

private void configureDynamicColumnWidth() {
    final AtomicBoolean resizing = new AtomicBoolean(false);
    final AtomicInteger gridWidth = new AtomicInteger();
    final SizeReporter sizeReporter = new SizeReporter(grid);
    // Grid resized
    sizeReporter.addResizeListener(new ComponentResizeListener() {

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

        @Override
        public void sizeChanged(ComponentResizeEvent event) {
            gridWidth.set(event.getWidth());
            if (gridWidth.get() > 1 && !grid.getColumns().isEmpty()) {
                final List<Column> newColumnsToResize = new ArrayList<>();
                final List<Column> columnsToResize = new ArrayList<>();
                double columnsToResizeTotalWidth = 0;
                double fixedColumnsTotalWidth = 0;
                for (Column column : grid.getColumns()) {
                    if (column.isHidden()) {
                        continue;
                    } else if (isColumnResizable(column)) {
                        if (column.getWidth() == -1) {
                            newColumnsToResize.add(column);
                        } else {
                            columnsToResize.add(column);
                            columnsToResizeTotalWidth += column.getWidth();
                        }
                    } else {
                        fixedColumnsTotalWidth += column.getWidth();
                    }
                }
                final int sizeForNewColumns = gridWidth.get() / grid.getColumns().size();
                for (Column column : newColumnsToResize) {
                    resizing.set(true);
                    setColumnWidth(column, sizeForNewColumns);
                    resizing.set(false);
                }
                if (grid.getSelectionModel() instanceof MultiSelectionModel) {
                    // in multi-select mode we need to allocate some space
                    // for the selector
                    fixedColumnsTotalWidth += 50;
                }
                final double widthDiscrepancy = gridWidth.get() - columnsToResizeTotalWidth - fixedColumnsTotalWidth - (newColumnsToResize.size() * sizeForNewColumns);
                if (widthDiscrepancy != 0) {
                    // Get the total width of all rows to be resized
                    double totalWidthOfColumnsToResize = 0;
                    for (Column column : columnsToResize) {
                        totalWidthOfColumnsToResize += column.getWidth();
                    }
                    // of the column by that amount
                    for (Column column : columnsToResize) {
                        final double trimPercentage = column.getWidth() / totalWidthOfColumnsToResize;
                        final double trimWidth = widthDiscrepancy * trimPercentage;
                        resizing.set(true);
                        setColumnWidth(column, column.getWidth() + trimWidth);
                        resizing.set(false);
                    }
                }
            }
        }
    });
    // Column resized
    grid.addColumnResizeListener(new ColumnResizeListener() {

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

        @Override
        public void columnResize(ColumnResizeEvent event) {
            if (!resizing.get() && gridWidth.get() > 0) {
                resizing.set(true);
                final List<Column> gridColumns = grid.getColumns();
                final int totalColumns = gridColumns.size();
                final Column resizedColumn = event.getColumn();
                final double resizedColumnWidth = resizedColumn.getWidth();
                final int resizedColumnIndex = gridColumns.indexOf(resizedColumn);
                final List<Column> columnsToResize = new ArrayList<>();
                double fixedColumnsTotalWidth = 0;
                // resized and total width of columns that won't
                for (int i = resizedColumnIndex + 1; i < totalColumns; i++) {
                    final Column column = gridColumns.get(i);
                    if (column.isHidden()) {
                        continue;
                    } else if (column.isResizable()) {
                        columnsToResize.add(column);
                    } else {
                        fixedColumnsTotalWidth += column.getWidth();
                    }
                }
                // availableWidth = grid width - width of column being
                // resized - columns to the right that won't be resized -
                // widths
                // of columns to the left
                double availableWidth = gridWidth.get() - resizedColumnWidth - fixedColumnsTotalWidth;
                for (int i = 0; i < resizedColumnIndex; i++) {
                    final Column column = gridColumns.get(i);
                    if (column.isHidden()) {
                        continue;
                    }
                    availableWidth -= column.getWidth();
                }
                // columns to the right of the column we are resizing)
                if (availableWidth != 0 && columnsToResize.size() == 0) {
                    setColumnWidth(resizedColumn, resizedColumn.getWidth() + availableWidth);
                } else // Otherwise resize columns to the right
                {
                    final double perColumnWidth = availableWidth / columnsToResize.size();
                    for (Column column : columnsToResize) {
                        setColumnWidth(column, perColumnWidth);
                    }
                }
                resizing.set(false);
            }
        }
    });
    // Column visibility toggled
    grid.addColumnVisibilityChangeListener(new ColumnVisibilityChangeListener() {

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

        @Override
        public void columnVisibilityChanged(ColumnVisibilityChangeEvent event) {
            if (gridWidth.get() > 0) {
                resizing.set(true);
                final List<Column> gridColumns = grid.getColumns();
                final Column toggledColumn = event.getColumn();
                final List<Column> columnsToResize = new ArrayList<>();
                double columnsToResizeTotalWidth = 0;
                double fixedColumnsTotalWidth = 0;
                for (Column column : gridColumns) {
                    if (column.equals(toggledColumn) || column.isHidden()) {
                        continue;
                    } else if (isColumnResizable(column)) {
                        columnsToResize.add(column);
                        columnsToResizeTotalWidth += column.getWidth();
                    } else {
                        fixedColumnsTotalWidth += column.getWidth();
                    }
                }
                // If toggled column has become visible, make room for it
                if (!toggledColumn.isHidden()) {
                    double newColumnWidth = 0;
                    // Trim <100% / new visible column count> from each
                    // column -
                    // add them up and this becomes the width of the newly
                    // visible column
                    double trimPercentage = 1d / (columnsToResize.size() + 1);
                    for (Column column : columnsToResize) {
                        final double trimWidth = column.getWidth() * trimPercentage;
                        setColumnWidth(column, column.getWidth() - trimWidth);
                        newColumnWidth += trimWidth;
                    }
                    if (!(newColumnWidth == 0)) {
                        setColumnWidth(toggledColumn, newColumnWidth);
                    }
                } else // Otherwise fill up the newly created blank space
                {
                    final double widthDiscrepancy = gridWidth.get() - columnsToResizeTotalWidth - fixedColumnsTotalWidth;
                    if (widthDiscrepancy != 0) {
                        final double perColumnChange = widthDiscrepancy / columnsToResize.size();
                        for (Column column : columnsToResize) {
                            setColumnWidth(column, column.getWidth() + perColumnChange);
                        }
                    }
                }
            }
            resizing.set(false);
        }
    });
}
Also used : ColumnResizeListener(com.vaadin.ui.Grid.ColumnResizeListener) ComponentResizeListener(com.ejt.vaadin.sizereporter.ComponentResizeListener) MultiSelectionModel(com.vaadin.ui.Grid.MultiSelectionModel) SizeReporter(com.ejt.vaadin.sizereporter.SizeReporter) ColumnVisibilityChangeListener(com.vaadin.ui.Grid.ColumnVisibilityChangeListener) ComponentResizeEvent(com.ejt.vaadin.sizereporter.ComponentResizeEvent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Column(com.vaadin.ui.Grid.Column) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ColumnResizeEvent(com.vaadin.ui.Grid.ColumnResizeEvent) ColumnVisibilityChangeEvent(com.vaadin.ui.Grid.ColumnVisibilityChangeEvent)

Aggregations

Column (com.vaadin.ui.Grid.Column)4 ColumnVisibilityChangeEvent (com.vaadin.ui.Grid.ColumnVisibilityChangeEvent)4 ColumnVisibilityChangeListener (com.vaadin.ui.Grid.ColumnVisibilityChangeListener)4 ComponentResizeEvent (com.ejt.vaadin.sizereporter.ComponentResizeEvent)1 ComponentResizeListener (com.ejt.vaadin.sizereporter.ComponentResizeListener)1 SizeReporter (com.ejt.vaadin.sizereporter.SizeReporter)1 ColumnResizeEvent (com.vaadin.ui.Grid.ColumnResizeEvent)1 ColumnResizeListener (com.vaadin.ui.Grid.ColumnResizeListener)1 MultiSelectionModel (com.vaadin.ui.Grid.MultiSelectionModel)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1