diff options
3 files changed, 126 insertions, 4 deletions
diff --git a/server/src/main/java/com/vaadin/ui/Tree.java b/server/src/main/java/com/vaadin/ui/Tree.java index ddf933cb01..aeb2dcfb6d 100644 --- a/server/src/main/java/com/vaadin/ui/Tree.java +++ b/server/src/main/java/com/vaadin/ui/Tree.java @@ -238,6 +238,7 @@ public class Tree<T> extends Composite private ItemCaptionGenerator<T> captionGenerator = String::valueOf; private IconGenerator<T> iconProvider = t -> null; private final TreeRenderer renderer; + private boolean autoRecalculateWidth = true; /** * Constructs a new Tree Component. @@ -259,10 +260,18 @@ public class Tree<T> extends Composite treeGrid.setHeightUndefined(); treeGrid.setHeightMode(HeightMode.UNDEFINED); - treeGrid.addExpandListener(e -> fireExpandEvent(e.getExpandedItem(), - e.isUserOriginated())); - treeGrid.addCollapseListener(e -> fireCollapseEvent( - e.getCollapsedItem(), e.isUserOriginated())); + treeGrid.addExpandListener(e -> { + fireExpandEvent(e.getExpandedItem(), e.isUserOriginated()); + if (autoRecalculateWidth) { + treeGrid.recalculateColumnWidths(); + } + }); + treeGrid.addCollapseListener(e -> { + fireCollapseEvent(e.getCollapsedItem(), e.isUserOriginated()); + if (autoRecalculateWidth) { + treeGrid.recalculateColumnWidths(); + } + }); treeGrid.addItemClickListener(e -> fireEvent( new ItemClick<>(this, e.getItem(), e.getMouseEventDetails()))); } @@ -790,6 +799,35 @@ public class Tree<T> extends Composite } /** + * Returns the current state of automatic width recalculation. + * + * @return {@code true} if enabled; {@code false} if disabled + * + * @since 8.1.1 + */ + public boolean isAutoRecalculateWidth() { + return autoRecalculateWidth; + } + + /** + * Sets the automatic width recalculation on or off. This feature is on by + * default. + * + * @param autoRecalculateWidth + * {@code true} to enable recalculation; {@code false} to turn it + * off + * + * @since 8.1.1 + */ + public void setAutoRecalculateWidth(boolean autoRecalculateWidth) { + this.autoRecalculateWidth = autoRecalculateWidth; + + treeGrid.getColumns().get(0) + .setMinimumWidthFromContent(autoRecalculateWidth); + treeGrid.recalculateColumnWidths(); + } + + /** * Adds a context click listener that gets notified when a context click * happens. * diff --git a/uitest/src/main/java/com/vaadin/tests/components/tree/TreeWideContent.java b/uitest/src/main/java/com/vaadin/tests/components/tree/TreeWideContent.java new file mode 100644 index 0000000000..2835829ae3 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/tree/TreeWideContent.java @@ -0,0 +1,36 @@ +package com.vaadin.tests.components.tree; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.data.TreeData; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Tree; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class TreeWideContent extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + Tree<String> tree = new Tree<>(); + + tree.setWidth("150px"); + tree.setHeight("100px"); + + TreeData<String> data = new TreeData(); + data.addItem(null, "Foo"); + data.addItem("Foo", "Extra long text content that should be wider" + + " than the allocated width of the Tree."); + data.addItem(null, "Bar"); + data.addItem(null, "Baz"); + tree.setTreeData(data); + + // Expand the wide one initially. + tree.expand("Foo"); + + addComponent(tree); + addComponent(new Button("Toggle auto recalc", e -> tree + .setAutoRecalculateWidth(!tree.isAutoRecalculateWidth()))); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/tree/TreeWideContentTest.java b/uitest/src/test/java/com/vaadin/tests/components/tree/TreeWideContentTest.java new file mode 100644 index 0000000000..da353b545b --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/tree/TreeWideContentTest.java @@ -0,0 +1,48 @@ +package com.vaadin.tests.components.tree; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.TreeElement; +import com.vaadin.testbench.elements.TreeGridElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class TreeWideContentTest extends SingleBrowserTest { + + @Test + public void testInitialSize() { + openTestURL(); + + TreeGridElement tree = $(TreeElement.class).first() + .wrap(TreeGridElement.class); + Assert.assertTrue("Row should be wider than tree", + tree.getTableWrapper().getSize().getWidth() < tree.getRow(0) + .getSize().getWidth()); + } + + @Test + public void testSizeAfterCollapse() { + openTestURL(); + + TreeElement tree = $(TreeElement.class).first(); + tree.collapse(0); + TreeGridElement treeGrid = tree.wrap(TreeGridElement.class); + Assert.assertTrue("Row should be as wide as tree", + treeGrid.getTableWrapper().getSize().getWidth() == treeGrid + .getRow(0).getSize().getWidth()); + } + + @Test + public void testSizeWithAutoRecalcDisabled() { + openTestURL(); + // Disable auto recalc + $(ButtonElement.class).first().click(); + + TreeElement tree = $(TreeElement.class).first(); + TreeGridElement treeGrid = tree.wrap(TreeGridElement.class); + Assert.assertTrue("Row should be as wide as tree", + treeGrid.getTableWrapper().getSize().getWidth() == treeGrid + .getRow(0).getSize().getWidth()); + } +} |