]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes declarative update of existing Composite (#10825)
authorIlia Motornyi <elmot@vaadin.com>
Tue, 17 Apr 2018 12:50:41 +0000 (15:50 +0300)
committerTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Fri, 20 Apr 2018 12:19:23 +0000 (15:19 +0300)
Fixes #10170

server/src/main/java/com/vaadin/ui/ComponentRootSetter.java
server/src/main/java/com/vaadin/ui/declarative/Design.java
server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java

index 8deb2d91cb97fde492c16cbe954d6d08059591c6..aea60e0f41216952b655ba5b3e0d3e2d4386596d 100644 (file)
@@ -51,4 +51,24 @@ public class ComponentRootSetter implements Serializable {
         }
     }
 
+    /**
+     * Checks if the given custom component or composite may accept a root component.
+     * <p>
+     * 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;
+    }
 }
index b7be276ec0b1ce43b07a05084ea114bf90b79672..789f6c8a7baf7dbb565907c1aebb0b26bbaec022 100644 (file)
@@ -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 {
index b2c0c8ae214aa6771acda752113ee255896c28ad..0e51c93d1d5c333a9e1d468a7046fdec4fe3c32b 100644 (file)
@@ -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<Tree> {
@@ -140,6 +145,30 @@ public class TreeDeclarativeTest
         testWrite(design, tree);
     }
 
+    @Test
+    public void testUpdateExisting() {
+        Tree tree = new Tree();
+
+        String treeDesign =
+                "<vaadin-tree selection-mode=\"MULTI\">" +
+                        "<node item=\"A\">A</node>" +
+                        "<node item=\"B\">B</node>" +
+                        "<node item=\"AA\" parent=\"A\">AA</node>" +
+                        "</vaadin-tree>";
+
+        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";