use of com.vaadin.ui.Grid.ColumnResizeEvent in project VaadinUtils by rlsutton1.
the class GridHeadingPropertySet method configureSaveColumnWidths.
private void configureSaveColumnWidths(final Grid grid, final String uniqueId) {
final String keyStub = uniqueId + "-width";
for (GridHeadingToPropertyId column : getColumns()) {
final String columnId = column.getPropertyId();
final String setting = keyStub + "-" + columnId;
final String columnWidth = UserSettingsStorageFactory.getUserSettingsStorage().get(setting);
if (columnWidth != null && columnWidth.length() > 0) {
try {
final Double width = Double.parseDouble(columnWidth);
if (width > 0) {
grid.getColumn(columnId).setWidth(Double.parseDouble(columnWidth));
}
} catch (NumberFormatException e) {
logger.error("Invalid width setting for " + setting);
}
}
}
grid.addColumnResizeListener(new ColumnResizeListener() {
private static final long serialVersionUID = 4034036880290943146L;
@Override
public void columnResize(ColumnResizeEvent event) {
final String propertyId = (String) event.getColumn().getPropertyId();
final double width = event.getColumn().getWidth();
UserSettingsStorageFactory.getUserSettingsStorage().store(keyStub + "-" + propertyId, "" + width);
}
});
if (dynamicColumnWidth) {
configureDynamicColumnWidth();
}
}
use of com.vaadin.ui.Grid.ColumnResizeEvent 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);
}
}
});
}
use of com.vaadin.ui.Grid.ColumnResizeEvent in project VaadinUtils by rlsutton1.
the class GridHeadingV2PropertySet method configureSaveColumnWidths.
private void configureSaveColumnWidths(final Grid grid) {
final String keyStub = uniqueId + "-width";
for (GridHeadingV2ToPropertyId id : getColumns()) {
final String propertyId = id.getPropertyId();
final String setting = keyStub + "-" + propertyId;
final Double savedWidth = getSavedWidth(setting);
if (savedWidth > 0) {
if (!columnWidthTypes.get(propertyId).equals(WidthType.FIXED)) {
setColumnWidth(grid.getColumn(propertyId), savedWidth);
}
}
}
grid.addColumnResizeListener(new ColumnResizeListener() {
/**
*/
private static final long serialVersionUID = 1L;
@Override
public void columnResize(ColumnResizeEvent event) {
final String propertyId = (String) event.getColumn().getPropertyId();
final double width = event.getColumn().getWidth();
UserSettingsStorageFactory.getUserSettingsStorage().store(keyStub + "-" + propertyId, "" + width);
}
});
if (dynamicColumnWidth) {
configureDynamicColumnWidth();
}
}
use of com.vaadin.ui.Grid.ColumnResizeEvent 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);
}
});
}
Aggregations