Search in sources :

Example 1 with ComponentResizeEvent

use of com.ejt.vaadin.sizereporter.ComponentResizeEvent in project VaadinUtils by rlsutton1.

the class GridHeadingPropertySet method configureDynamicColumnWidth.

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

        private static final long serialVersionUID = 1L;

        @Override
        public void sizeChanged(ComponentResizeEvent event) {
            final int newGridWidth = event.getWidth();
            gridWidth.set(newGridWidth);
            if (newGridWidth > 1) {
                final List<Column> gridColumns = grid.getColumns();
                double columnsTotalWidth = 0;
                for (Column column : gridColumns) {
                    columnsTotalWidth += column.getWidth();
                }
                final double widthDiscrepancy = gridWidth.get() - columnsTotalWidth;
                if (widthDiscrepancy != 0) {
                    final double perColumnChange = widthDiscrepancy / gridColumns.size();
                    for (Column column : gridColumns) {
                        resizing.set(true);
                        column.setWidth(column.getWidth() + perColumnChange);
                        resizing.set(false);
                    }
                }
            }
        }
    });
    grid.addColumnResizeListener(new ColumnResizeListener() {

        private static final long serialVersionUID = 1L;

        @Override
        public void columnResize(ColumnResizeEvent event) {
            if (gridWidth.get() > 0 && !resizing.get()) {
                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);
                // availableWidth = grid width - width of column being
                // resized - widths of columns to the left
                double availableWidth = gridWidth.get() - resizedColumnWidth;
                for (int i = 0; i < resizedColumnIndex; i++) {
                    availableWidth -= gridColumns.get(i).getWidth();
                }
                // columnsToResize = total columns - column being resized -
                // number of columns to the right
                final int columnsToResize = totalColumns - resizedColumnIndex - 1;
                final double perColumnWidth = availableWidth / columnsToResize;
                for (int i = (resizedColumnIndex + 1); i < totalColumns; i++) {
                    gridColumns.get(i).setWidth(perColumnWidth);
                }
                resizing.set(false);
            }
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ColumnResizeListener(com.vaadin.ui.Grid.ColumnResizeListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComponentResizeListener(com.ejt.vaadin.sizereporter.ComponentResizeListener) Column(com.vaadin.ui.Grid.Column) SizeReporter(com.ejt.vaadin.sizereporter.SizeReporter) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ColumnResizeEvent(com.vaadin.ui.Grid.ColumnResizeEvent) ComponentResizeEvent(com.ejt.vaadin.sizereporter.ComponentResizeEvent)

Example 2 with ComponentResizeEvent

use of com.ejt.vaadin.sizereporter.ComponentResizeEvent 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

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