Ver código fonte

Allow grid columns to optionally shrink to be narrower than contents

Fixes #8548
tags/8.1.0.beta2
Artur 7 anos atrás
pai
commit
39846ae083

+ 5
- 0
client/src/main/java/com/vaadin/client/connectors/grid/ColumnConnector.java Ver arquivo

column.setMinimumWidth(getState().minWidth); column.setMinimumWidth(getState().minWidth);
} }


@OnStateChange("minimumWidthFromContent")
void updateMinimumWidthFromContent() {
column.setMinimumWidthFromContent(getState().minimumWidthFromContent);
}

@OnStateChange("maxWidth") @OnStateChange("maxWidth")
void updateMaxWidth() { void updateMaxWidth() {
column.setMaximumWidth(getState().maxWidth); column.setMaximumWidth(getState().maxWidth);

+ 47
- 3
client/src/main/java/com/vaadin/client/widgets/Grid.java Ver arquivo

final int expandRatio = defaultExpandRatios ? 1 final int expandRatio = defaultExpandRatios ? 1
: column.getExpandRatio(); : column.getExpandRatio();
final double maxWidth = getMaxWidth(column); final double maxWidth = getMaxWidth(column);
final double newWidth = Math.min(maxWidth,
column.getWidthActual());
double newWidth;
if (column.isMinimumWidthFromContent()) {
newWidth = Math.min(maxWidth, column.getWidthActual());
} else {
newWidth = 0;
}

boolean shouldExpand = newWidth < maxWidth && expandRatio > 0 boolean shouldExpand = newWidth < maxWidth && expandRatio > 0
&& column != selectionColumn; && column != selectionColumn;
if (shouldExpand) { if (shouldExpand) {
* @param <T> * @param <T>
* the row type * the row type
*/ */
public static abstract class Column<C, T> {
public abstract static class Column<C, T> {


/** /**
* Default renderer for GridColumns. Renders everything into text * Default renderer for GridColumns. Renders everything into text
private double minimumWidthPx = GridConstants.DEFAULT_MIN_WIDTH; private double minimumWidthPx = GridConstants.DEFAULT_MIN_WIDTH;
private double maximumWidthPx = GridConstants.DEFAULT_MAX_WIDTH; private double maximumWidthPx = GridConstants.DEFAULT_MAX_WIDTH;
private int expandRatio = GridConstants.DEFAULT_EXPAND_RATIO; private int expandRatio = GridConstants.DEFAULT_EXPAND_RATIO;
private boolean minimumWidthFromContent = true;


/** /**
* Constructs a new column with a simple TextRenderer. * Constructs a new column with a simple TextRenderer.
return this; return this;
} }


/**
* Sets whether the width of the contents in the column should be
* considered minimum width for this column.
* <p>
* If this is set to <code>true</code> (default for backwards
* compatibility), then a column will not shrink to smaller than the
* width required to show the contents available when calculating the
* widths (first N rows).
* <p>
* If this is set to <code>false</code>, then a column will shrink if
* necessary to the minimum width defined by
* {@link #setMinimumWidth(double)} <em>when it is set to expand</em>.
*
* @param minimumWidthFromContent
* <code>true</code> to reserve space for all contents,
* <code>false</code> to allow the column to shrink smaller
* than the contents
* @since
*/
public void setMinimumWidthFromContent(
boolean minimumWidthFromContent) {
this.minimumWidthFromContent = minimumWidthFromContent;
}

/**
* Gets whether the width of the contents in the column should be
* considered minimum width for this column.
*
* @return <code>true</code> to reserve space for all contents,
* <code>false</code> to allow the column to shrink smaller than
* the contents
* @since
*/
public boolean isMinimumWidthFromContent() {
return minimumWidthFromContent;
}

/** /**
* Sets the maximum width for this column. * Sets the maximum width for this column.
* <p> * <p>
protected void setDefaultHeaderContent(HeaderCell cell) { protected void setDefaultHeaderContent(HeaderCell cell) {
cell.setText(headerCaption); cell.setText(headerCaption);
} }

} }


protected class BodyUpdater implements EscalatorUpdater { protected class BodyUpdater implements EscalatorUpdater {

+ 5
- 1
documentation/components/components-grid.asciidoc Ver arquivo

[methodname]#setExpandRatio()#. [methodname]#setExpandRatio()#.


When using expand ratios, the columns with a non-zero expand ratio use the extra When using expand ratios, the columns with a non-zero expand ratio use the extra
space remaining from other columns, in proportion to the defined ratios.
space remaining from other columns, in proportion to the defined ratios. Do note
that the minimum width of an expanded column by default is based on the contents
of the column (the initially rendered rows). To allow the column to become
narrower than this, use [methodname]#setMinimumWidthFromContent(false)#
(introduced in 8.1).


You can specify minimum and maximum widths for the expanding columns with You can specify minimum and maximum widths for the expanding columns with
[methodname]#setMinimumWidth()# and [methodname]#setMaximumWidth()#, [methodname]#setMinimumWidth()# and [methodname]#setMaximumWidth()#,

+ 54
- 0
server/src/main/java/com/vaadin/ui/Grid.java Ver arquivo

* This defines the minimum guaranteed pixel width of the column * This defines the minimum guaranteed pixel width of the column
* <em>when it is set to expand</em>. * <em>when it is set to expand</em>.
* *
* @param pixels
* the minimum width for the column
* @throws IllegalStateException * @throws IllegalStateException
* if the column is no longer attached to any grid * if the column is no longer attached to any grid
* @see #setExpandRatio(int) * @see #setExpandRatio(int)
* @return the column itself
*/ */
public Column<T, V> setMinimumWidth(double pixels) public Column<T, V> setMinimumWidth(double pixels)
throws IllegalStateException { throws IllegalStateException {
return getState(false).minWidth; return getState(false).minWidth;
} }


/**
* Sets whether the width of the contents in the column should be
* considered minimum width for this column.
* <p>
* If this is set to <code>true</code> (default for backwards
* compatibility), then a column will not shrink to smaller than the
* width required to show the contents available when calculating the
* widths (only the widths of the initially rendered rows are
* considered).
* <p>
* If this is set to <code>false</code> and the column has been set to
* expand using #setExpandRatio(int), then the contents of the column
* will be ignored when calculating the width, and the column will thus
* shrink down to the minimum width defined by #setMinimumWidth(double)
* if necessary.
*
* @param minimumWidthFromContent
* <code>true</code> to reserve space for all contents,
* <code>false</code> to allow the column to shrink smaller
* than the contents
* @return the column itself
* @throws IllegalStateException
* if the column is no longer attached to any grid
* @see #setMinimumWidth(double)
* @since
*/
public Column<T, V> setMinimumWidthFromContent(
boolean minimumWidthFromContent) throws IllegalStateException {
checkColumnIsAttached();

if (isMinimumWidthFromContent() != minimumWidthFromContent) {
getState().minimumWidthFromContent = minimumWidthFromContent;
getGrid().markAsDirty();
}
return this;
}

/**
* Gets whether the width of the contents in the column should be
* considered minimum width for this column.
*
* @return <code>true</code> to reserve space for all contents,
* <code>false</code> to allow the column to shrink smaller than
* the contents
* @see #setMinimumWidthFromContent(boolean)
* @since
*/
public boolean isMinimumWidthFromContent() {
return getState(false).minimumWidthFromContent;
}

/** /**
* Sets the maximum width for this column. * Sets the maximum width for this column.
* <p> * <p>

+ 4
- 0
shared/src/main/java/com/vaadin/shared/ui/grid/ColumnState.java Ver arquivo

public boolean resizable = true; public boolean resizable = true;


public Connector renderer; public Connector renderer;
/**
* Whether the contents define the minimum width for this column.
*/
public boolean minimumWidthFromContent = true;


} }

+ 45
- 0
uitest/src/main/java/com/vaadin/tests/components/grid/GridColumnShrinkSmallerThanContents.java Ver arquivo

/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.components.grid;

import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.components.AbstractTestUI;
import com.vaadin.ui.Component;
import com.vaadin.ui.Grid;

@Widgetset("com.vaadin.DefaultWidgetSet")
public class GridColumnShrinkSmallerThanContents extends AbstractTestUI {

@Override
protected void setup(VaadinRequest request) {
addComponent(createGrid(true));
addComponent(createGrid(false));
}

private Component createGrid(boolean minimumWidthFromContent) {
Grid<Object> grid = new Grid<>();
grid.addColumn(item -> "Contents in column 1");
grid.addColumn(
item -> "Contents in column 2. Contents in column 2. Contents in column 2. Contents in column 2. Contents in column 2.")
.setExpandRatio(1)
.setMinimumWidthFromContent(minimumWidthFromContent);
grid.setItems(new Object(), new Object());
grid.setWidth("500px");
return grid;
}

}

+ 35
- 0
uitest/src/test/java/com/vaadin/tests/components/grid/GridColumnShrinkSmallerThanContentsTest.java Ver arquivo

/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.components.grid;

import org.junit.Test;

import com.vaadin.testbench.elements.GridElement;
import com.vaadin.tests.tb3.SingleBrowserTest;

public class GridColumnShrinkSmallerThanContentsTest extends SingleBrowserTest {

@Test
public void scrollbarAndNoScrollbar() {
openTestURL();
GridElement noshrinkColumnGrid = $(GridElement.class).get(0);
GridElement shrinkColumnGrid = $(GridElement.class).get(1);
assertHorizontalScrollbar(noshrinkColumnGrid.getHorizontalScroller(),
"Should have a horizontal scrollbar as column 2 should be wide");
assertNoHorizontalScrollbar(shrinkColumnGrid.getHorizontalScroller(),
"Should not have a horizontal scrollbar as column 2 should be narrow");
}
}

+ 17
- 6
uitest/src/test/java/com/vaadin/tests/tb3/AbstractTB3Test.java Ver arquivo



protected void assertNoHorizontalScrollbar(WebElement element, protected void assertNoHorizontalScrollbar(WebElement element,
String errorMessage) { String errorMessage) {
assertHasHorizontalScrollbar(element, errorMessage, false);
}

protected void assertHorizontalScrollbar(WebElement element,
String errorMessage) {
assertHasHorizontalScrollbar(element, errorMessage, true);
}

private void assertHasHorizontalScrollbar(WebElement element,
String errorMessage, boolean expected) {
// IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth // IE rounds clientWidth/clientHeight down and scrollHeight/scrollWidth
// up, so using clientWidth/clientHeight will fail if the element height // up, so using clientWidth/clientHeight will fail if the element height
// is not an integer // is not an integer
int clientWidth = getClientWidth(element); int clientWidth = getClientWidth(element);
int scrollWidth = getScrollWidth(element); int scrollWidth = getScrollWidth(element);
boolean hasScrollbar = scrollWidth > clientWidth; boolean hasScrollbar = scrollWidth > clientWidth;

Assert.assertFalse(
"The element should not have a horizontal scrollbar (scrollWidth: "
+ scrollWidth + ", clientWidth: " + clientWidth + "): "
+ errorMessage,
hasScrollbar);
String message = "The element should";
if (!expected) {
message += " not";
}
message += " have a horizontal scrollbar (scrollWidth: " + scrollWidth
+ ", clientWidth: " + clientWidth + "): " + errorMessage;
Assert.assertEquals(message, expected, hasScrollbar);
} }


protected void assertNoVerticalScrollbar(WebElement element, protected void assertNoVerticalScrollbar(WebElement element,

Carregando…
Cancelar
Salvar