From 5b0accf49295cab4a494b2949d067ff7655d3c42 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Wed, 2 Aug 2017 15:32:31 +0300 Subject: Add automatic recalculation of Tree width on expand/collapse (#9767) Fixes #9628 --- server/src/main/java/com/vaadin/ui/Tree.java | 46 +++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) (limited to 'server/src') 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 extends Composite private ItemCaptionGenerator captionGenerator = String::valueOf; private IconGenerator iconProvider = t -> null; private final TreeRenderer renderer; + private boolean autoRecalculateWidth = true; /** * Constructs a new Tree Component. @@ -259,10 +260,18 @@ public class Tree 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()))); } @@ -789,6 +798,35 @@ public class Tree extends Composite renderer.getState().mode = contentMode; } + /** + * 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. -- cgit v1.2.3