]> source.dussan.org Git - vaadin-framework.git/commitdiff
Declarative support for CssLayout (#7749)
authorMatti Hosio <mhosio@vaadin.com>
Tue, 9 Dec 2014 12:42:57 +0000 (14:42 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 10 Dec 2014 06:40:40 +0000 (06:40 +0000)
Change-Id: Ic99fb1e9af921b3e14127888e2b3dffbd54f493e

server/src/com/vaadin/ui/CssLayout.java
server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeFromDesign.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeToDesign.java [new file with mode: 0644]

index e7b63cc87a8baa22c4778505139ddbad3395782b..33b8c9fcbcaa86036584e9dd97d7a59da5145ada 100644 (file)
@@ -18,6 +18,8 @@ package com.vaadin.ui;
 import java.util.Iterator;
 import java.util.LinkedList;
 
+import org.jsoup.nodes.Element;
+
 import com.vaadin.event.LayoutEvents.LayoutClickEvent;
 import com.vaadin.event.LayoutEvents.LayoutClickListener;
 import com.vaadin.event.LayoutEvents.LayoutClickNotifier;
@@ -26,6 +28,7 @@ import com.vaadin.shared.EventId;
 import com.vaadin.shared.MouseEventDetails;
 import com.vaadin.shared.ui.csslayout.CssLayoutServerRpc;
 import com.vaadin.shared.ui.csslayout.CssLayoutState;
+import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * CssLayout is a layout component that can be used in browser environment only.
@@ -358,4 +361,46 @@ public class CssLayout extends AbstractLayout implements LayoutClickNotifier {
         return components.get(index);
     }
 
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * com.vaadin.ui.AbstractComponent#synchronizeFromDesign(org.jsoup.nodes
+     * .Element, com.vaadin.ui.declarative.DesignContext)
+     */
+    @Override
+    public void synchronizeFromDesign(Element design,
+            DesignContext designContext) {
+        // process default attributes
+        super.synchronizeFromDesign(design, designContext);
+        // remove current children
+        removeAllComponents();
+        // handle children
+        for (Element childComponent : design.children()) {
+            DesignSynchronizable newChild = designContext
+                    .createChild(childComponent);
+            addComponent(newChild);
+        }
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * com.vaadin.ui.AbstractComponent#synchronizeToDesign(org.jsoup.nodes.Element
+     * , com.vaadin.ui.declarative.DesignContext)
+     */
+    @Override
+    public void synchronizeToDesign(Element design, DesignContext designContext) {
+        // synchronize default attributes
+        super.synchronizeToDesign(design, designContext);
+        // handle children
+        Element designElement = design;
+        for (Component child : this) {
+            DesignSynchronizable childComponent = (DesignSynchronizable) child;
+            Element childNode = designContext.createNode(childComponent);
+            designElement.appendChild(childNode);
+        }
+    }
+
 }
diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeFromDesign.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeFromDesign.java
new file mode 100644 (file)
index 0000000..0e1b964
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2000-2014 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.server.component.csslayout;
+
+import junit.framework.TestCase;
+
+import org.jsoup.nodes.Attributes;
+import org.jsoup.nodes.Element;
+import org.jsoup.parser.Tag;
+
+import com.vaadin.ui.CssLayout;
+import com.vaadin.ui.DesignSynchronizable;
+import com.vaadin.ui.declarative.DesignContext;
+
+/**
+ * Test case for reading CssLayout from design
+ * 
+ * @author Vaadin Ltd
+ */
+public class TestSynchronizeFromDesign extends TestCase {
+
+    public void testChildCount() {
+        CssLayout root = createLayout();
+        assertEquals(2, root.getComponentCount());
+    }
+
+    public void testAttributes() {
+        CssLayout root = createLayout();
+        assertEquals("test-layout", root.getCaption());
+        assertEquals("test-label", root.getComponent(0).getCaption());
+        assertEquals("test-button", root.getComponent(1).getCaption());
+    }
+
+    private CssLayout createLayout() {
+        DesignContext ctx = new DesignContext();
+        Element design = createDesign();
+        DesignSynchronizable child = ctx.createChild(design);
+        return (CssLayout) child;
+    }
+
+    private Element createDesign() {
+
+        Attributes rootAttributes = new Attributes();
+        rootAttributes.put("caption", "test-layout");
+        Element node = new Element(Tag.valueOf("v-css-layout"), "",
+                rootAttributes);
+
+        Attributes firstChildAttributes = new Attributes();
+        firstChildAttributes.put("caption", "test-label");
+        Element firstChild = new Element(Tag.valueOf("v-label"), "",
+                firstChildAttributes);
+        node.appendChild(firstChild);
+
+        Attributes secondChildAttributes = new Attributes();
+        Element secondChild = new Element(Tag.valueOf("v-button"), "",
+                secondChildAttributes);
+        secondChild.html("test-button");
+        node.appendChild(secondChild);
+        return node;
+    }
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeToDesign.java b/server/tests/src/com/vaadin/tests/server/component/csslayout/TestSynchronizeToDesign.java
new file mode 100644 (file)
index 0000000..2dfd560
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2000-2014 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.server.component.csslayout;
+
+import junit.framework.TestCase;
+
+import org.jsoup.nodes.Attributes;
+import org.jsoup.nodes.Element;
+import org.jsoup.parser.Tag;
+
+import com.vaadin.ui.CssLayout;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.declarative.DesignContext;
+
+/**
+ * Test case for writing CssLayout to design
+ * 
+ * @author Vaadin Ltd
+ */
+public class TestSynchronizeToDesign extends TestCase {
+
+    public void testSynchronizeEmptyLayout() {
+        CssLayout layout = new CssLayout();
+        layout.setCaption("changed-caption");
+        Element design = createDesign();
+        layout.synchronizeToDesign(design, createDesignContext());
+        assertEquals(0, design.childNodes().size());
+        assertEquals("changed-caption", design.attr("caption"));
+    }
+
+    public void testSynchronizeLayoutWithChildren() {
+        CssLayout layout = new CssLayout();
+        layout.addComponent(new Label("test-label"));
+        layout.getComponent(0).setCaption("test-caption");
+        layout.addComponent(new Label("test-label-2"));
+        Element design = createDesign();
+        layout.synchronizeToDesign(design, createDesignContext());
+        assertEquals(2, design.childNodes().size());
+        assertEquals("v-label", ((Element) design.childNode(0)).tagName());
+        assertEquals("test-caption", design.childNode(0).attr("caption"));
+    }
+
+    private Element createDesign() {
+        // make sure that the design node has old content that should be removed
+        Attributes rootAttributes = new Attributes();
+        rootAttributes.put("caption", "test-layout");
+        Element node = new Element(Tag.valueOf("v-vertical-layout"), "",
+                rootAttributes);
+        Attributes firstChildAttributes = new Attributes();
+        firstChildAttributes.put("caption", "test-label");
+        Element firstChild = new Element(Tag.valueOf("v-label"), "",
+                firstChildAttributes);
+        node.appendChild(firstChild);
+
+        Element secondChild = new Element(Tag.valueOf("v-button"), "",
+                new Attributes());
+        secondChild.html("test-button");
+        node.appendChild(secondChild);
+        return node;
+    }
+
+    private DesignContext createDesignContext() {
+        return new DesignContext();
+    }
+}