From 307bdc7db64fd631ada4b62f0433f41e66781911 Mon Sep 17 00:00:00 2001 From: Ilia Motornyi Date: Tue, 17 Apr 2018 15:50:41 +0300 Subject: Fixes declarative update of existing Composite (#10825) Fixes #10170 --- .../java/com/vaadin/ui/ComponentRootSetter.java | 20 +++++++++++++++ .../java/com/vaadin/ui/declarative/Design.java | 4 +-- .../server/component/tree/TreeDeclarativeTest.java | 29 ++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) (limited to 'server') diff --git a/server/src/main/java/com/vaadin/ui/ComponentRootSetter.java b/server/src/main/java/com/vaadin/ui/ComponentRootSetter.java index 8deb2d91cb..aea60e0f41 100644 --- a/server/src/main/java/com/vaadin/ui/ComponentRootSetter.java +++ b/server/src/main/java/com/vaadin/ui/ComponentRootSetter.java @@ -51,4 +51,24 @@ public class ComponentRootSetter implements Serializable { } } + /** + * Checks if the given custom component or composite may accept a root component. + *

+ * For internal use only. + * + * @param customComponent + * the custom component or composite + * @return + * @since + * + */ + public static boolean canSetRoot(Component customComponent) { + if(customComponent instanceof CustomComponent) { + return true; + } + if(customComponent instanceof Composite) { + return ((Composite)customComponent).getCompositionRoot() == null; + } + return false; + } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/Design.java b/server/src/main/java/com/vaadin/ui/declarative/Design.java index b7be276ec0..789f6c8a7b 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/Design.java +++ b/server/src/main/java/com/vaadin/ui/declarative/Design.java @@ -495,8 +495,8 @@ public class Design implements Serializable { designContext.addComponentCreationListener(creationListener); // create subtree - if (componentRoot instanceof CustomComponent - || componentRoot instanceof Composite) { + + if (ComponentRootSetter.canSetRoot(componentRoot)) { Component rootComponent = designContext.readDesign(element); ComponentRootSetter.setRoot(componentRoot, rootComponent); } else { diff --git a/server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java index b2c0c8ae21..0e51c93d1d 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java @@ -1,9 +1,13 @@ package com.vaadin.tests.server.component.tree; +import java.io.ByteArrayInputStream; + +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertTrue; import org.junit.Test; +import com.vaadin.data.SelectionModel; import com.vaadin.data.TreeData; import com.vaadin.data.provider.HierarchicalQuery; import com.vaadin.shared.ui.ContentMode; @@ -13,6 +17,7 @@ import com.vaadin.ui.Grid.SelectionMode; import com.vaadin.ui.IconGenerator; import com.vaadin.ui.ItemCaptionGenerator; import com.vaadin.ui.Tree; +import com.vaadin.ui.declarative.Design; public class TreeDeclarativeTest extends AbstractComponentDeclarativeTestBase { @@ -140,6 +145,30 @@ public class TreeDeclarativeTest testWrite(design, tree); } + @Test + public void testUpdateExisting() { + Tree tree = new Tree(); + + String treeDesign = + "" + + "A" + + "B" + + "AA" + + ""; + + Design.read(new ByteArrayInputStream(treeDesign.getBytes()), tree); + Object[] items = tree.getDataProvider(). + fetchChildren(new HierarchicalQuery(null, null)).toArray(); + assertArrayEquals(new Object[]{"A", "B"}, items); + Object[] itemsA = tree.getDataProvider(). + fetchChildren(new HierarchicalQuery(null, "A")).toArray(); + assertArrayEquals(new Object[]{"AA"}, itemsA); + long countB = tree.getDataProvider(). + fetchChildren(new HierarchicalQuery(null, "B")).count(); + assertEquals(0L, countB); + assertTrue(tree.getSelectionModel() instanceof SelectionModel.Multi); + } + @Override protected String getComponentTag() { return "vaadin-tree"; -- cgit v1.2.3