diff options
7 files changed, 126 insertions, 3 deletions
diff --git a/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java b/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java index d1baf5ba99..1fa35d6a7c 100644 --- a/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java +++ b/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java @@ -127,6 +127,19 @@ public interface ColumnConfiguration { throws IllegalArgumentException; /** + * Sets the column width to as wide as the widest currently visible content + * in that column. + * + * @param index + * the index of the column for which to calculate the width. + * @throws IllegalArgumentException + * if {@code index} is not a valid column index, or if any cell + * in the given column is a part of a colspan range + */ + public void setColumnWidthToContent(int index) + throws IllegalArgumentException; + + /** * Returns the user-defined width of a column. * * @param index @@ -173,4 +186,4 @@ public interface ColumnConfiguration { */ public void refreshColumns(int index, int numberOfColumns) throws IndexOutOfBoundsException, IllegalArgumentException; -}
\ No newline at end of file +} diff --git a/client/src/com/vaadin/client/ui/grid/Escalator.java b/client/src/com/vaadin/client/ui/grid/Escalator.java index 3aaa1f6c06..1fc95ecc08 100644 --- a/client/src/com/vaadin/client/ui/grid/Escalator.java +++ b/client/src/com/vaadin/client/ui/grid/Escalator.java @@ -1869,6 +1869,51 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker return new Cell(domRowIndex, domColumnIndex, cellElement); } + int getMaxCellWidth(int colIndex) throws IllegalArgumentException { + int maxCellWidth = -1; + + NodeList<TableRowElement> rows = root.getRows(); + for (int row = 0; row < rows.getLength(); row++) { + TableRowElement rowElement = rows.getItem(row); + TableCellElement cellOriginal = rowElement.getCells().getItem( + colIndex); + + if (cellIsPartOfSpan(cellOriginal)) { + throw new IllegalArgumentException("Encountered a column " + + "spanned cell in column " + colIndex + "."); + } + + /* + * To get the actual width of the contents, we need to get the + * cell content without any hardcoded height or width. + * + * But we don't want to modify the existing column, because that + * might trigger some unnecessary listeners and whatnot. So, + * instead, we make a deep clone of that cell, but without any + * explicit dimensions, and measure that instead. + */ + + TableCellElement cellClone = TableCellElement + .as((Element) cellOriginal.cloneNode(true)); + cellClone.getStyle().clearHeight(); + cellClone.getStyle().clearWidth(); + + rowElement.insertBefore(cellClone, cellOriginal); + maxCellWidth = Math.max(cellClone.getOffsetWidth(), + maxCellWidth); + cellClone.removeFromParent(); + } + + return maxCellWidth; + } + + private boolean cellIsPartOfSpan(TableCellElement cell) { + boolean cellHasColspan = cell.getColSpan() > 1; + boolean cellIsHidden = Display.NONE.getCssName().equals( + cell.getStyle().getDisplay()); + return cellHasColspan || cellIsHidden; + } + void refreshColumns(int index, int numberOfColumns) { if (getRowCount() > 0) { Range rowRange = Range.withLength(0, getRowCount()); @@ -3832,6 +3877,35 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker return columns.get(index).getCalculatedWidth(); } + @Override + public void setColumnWidthToContent(int index) + throws IllegalArgumentException { + + if (index < 0 || index >= getColumnCount()) { + throw new IllegalArgumentException(index + + " is not a valid index for a column"); + } + + int maxWidth = getMaxCellWidth(index); + + if (maxWidth == -1) { + return; + } + + setCalculatedColumnWidth(index, maxWidth); + header.reapplyColumnWidths(); + footer.reapplyColumnWidths(); + body.reapplyColumnWidths(); + } + + private int getMaxCellWidth(int colIndex) + throws IllegalArgumentException { + int headerWidth = header.getMaxCellWidth(colIndex); + int bodyWidth = body.getMaxCellWidth(colIndex); + int footerWidth = footer.getMaxCellWidth(colIndex); + return Math.max(headerWidth, Math.max(bodyWidth, footerWidth)); + } + /** * Calculates the width of the columns in a given range. * @@ -3840,8 +3914,7 @@ public class Escalator extends Widget implements RequiresResize, DeferredWorker * @return the total width of the columns in the given * <code>columns</code> */ - int getCalculatedColumnsWidth(@SuppressWarnings("hiding") - final Range columns) { + int getCalculatedColumnsWidth(final Range columns) { /* * This is an assert instead of an exception, since this is an * internal method. diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeatures.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeatures.java index 478fa53893..94144b233d 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeatures.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeatures.java @@ -16,6 +16,7 @@ package com.vaadin.tests.components.grid.basicfeatures; +import com.vaadin.annotations.Title; import com.vaadin.annotations.Widgetset; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.widgetset.TestingWidgetSet; @@ -23,6 +24,7 @@ import com.vaadin.ui.AbstractComponent; import com.vaadin.ui.UI; @Widgetset(TestingWidgetSet.NAME) +@Title("Escalator basic client features") public class EscalatorBasicClientFeatures extends UI { public class EscalatorTestComponent extends AbstractComponent { diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeaturesTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeaturesTest.java index 0c58b01062..bc5815cd91 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeaturesTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorBasicClientFeaturesTest.java @@ -39,6 +39,7 @@ public abstract class EscalatorBasicClientFeaturesTest extends MultiBrowserTest protected static final String REMOVE_ONE_COLUMN_FROM_BEGINNING = "Remove one column from beginning"; protected static final String REMOVE_ONE_ROW_FROM_BEGINNING = "Remove one row from beginning"; protected static final String ADD_ONE_OF_EACH_ROW = "Add one of each row"; + protected static final String RESIZE_FIRST_COLUMN_TO_MAX_WIDTH = "Resize first column to max width"; protected static final String HEADER_ROWS = "Header Rows"; protected static final String BODY_ROWS = "Body Rows"; diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorRowColumnTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorRowColumnTest.java index f45eec07f8..d4c5e37797 100644 --- a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorRowColumnTest.java +++ b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/EscalatorRowColumnTest.java @@ -15,6 +15,7 @@ */ package com.vaadin.tests.components.grid.basicfeatures; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -200,4 +201,17 @@ public class EscalatorRowColumnTest extends EscalatorBasicClientFeaturesTest { assertNull(getBodyCell(0, 0)); } + + @Test + public void testResizeColToFit() { + openTestURL(); + selectMenuPath(GENERAL, POPULATE_COLUMN_ROW); + + int originalWidth = getBodyCell(0, 0).getSize().getWidth(); + selectMenuPath(COLUMNS_AND_ROWS, COLUMNS, + RESIZE_FIRST_COLUMN_TO_MAX_WIDTH); + int newWidth = getBodyCell(0, 0).getSize().getWidth(); + assertNotEquals("Column width should've changed", originalWidth, + newWidth); + } } diff --git a/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorBasicClientFeaturesWidget.java b/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorBasicClientFeaturesWidget.java index 08dc1d2eae..b1a5498081 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorBasicClientFeaturesWidget.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorBasicClientFeaturesWidget.java @@ -463,6 +463,15 @@ public class EscalatorBasicClientFeaturesWidget extends escalator.getColumnConfiguration().refreshColumns(0, 1); } }, menupath); + + addMenuCommand("Resize first column to max width", + new ScheduledCommand() { + @Override + public void execute() { + escalator.getColumnConfiguration() + .setColumnWidthToContent(0); + } + }, menupath); } private void createHeaderRowsMenu() { diff --git a/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorProxy.java b/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorProxy.java index f1d64a50e7..baa8c07855 100644 --- a/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorProxy.java +++ b/uitest/src/com/vaadin/tests/widgetset/client/grid/EscalatorProxy.java @@ -82,6 +82,17 @@ public class EscalatorProxy extends Escalator { } @Override + public void setColumnWidthToContent(int index) + throws IllegalArgumentException { + int oldWidth = columnConfiguration.getColumnWidthActual(index); + columnConfiguration.setColumnWidthToContent(index); + int newWidth = columnConfiguration.getColumnWidthActual(index); + logWidget.log("Changed column " + index + " width from " + oldWidth + + "px to " + newWidth + "px"); + logWidget.updateDebugLabel(); + } + + @Override public void refreshColumns(int index, int numberOfColumns) throws IndexOutOfBoundsException, IllegalArgumentException { columnConfiguration.refreshColumns(index, numberOfColumns); |