]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add declarative support for PopupView (#16334)
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Wed, 25 Mar 2015 12:31:32 +0000 (14:31 +0200)
committerTeemu Suo-Anttila <teemusa@vaadin.com>
Tue, 7 Apr 2015 07:52:11 +0000 (10:52 +0300)
Change-Id: Ic5fb0238f538fd05bb6d6e37ddff3ec06175f782

server/src/com/vaadin/ui/PopupView.java
server/tests/src/com/vaadin/tests/server/component/popupview/PopupViewDeclarativeTest.java [new file with mode: 0644]

index 2a2da26b62cbbd3c585f2a774e4be1144829b1ee..12034cb56ccb951362706329c45a1aec4fa93010 100644 (file)
@@ -20,8 +20,13 @@ import java.lang.reflect.Method;
 import java.util.Collections;
 import java.util.Iterator;
 
+import org.jsoup.nodes.Element;
+import org.jsoup.nodes.Node;
+import org.jsoup.parser.Tag;
+
 import com.vaadin.shared.ui.popupview.PopupViewServerRpc;
 import com.vaadin.shared.ui.popupview.PopupViewState;
+import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * 
@@ -61,9 +66,15 @@ public class PopupView extends AbstractComponent implements HasComponents {
 
     /* Constructors */
 
-    private PopupView() {
+    /**
+     * This is an internal constructor. Use
+     * {@link PopupView#PopupView(String, Component)} instead.
+     */
+    @Deprecated
+    public PopupView() {
         registerRpc(rpc);
         setHideOnMouseOut(true);
+        setContent(createContent("", new Label("")));
     }
 
     /**
@@ -77,18 +88,7 @@ public class PopupView extends AbstractComponent implements HasComponents {
      *            the full, Component-type representation
      */
     public PopupView(final java.lang.String small, final Component large) {
-        this(new PopupView.Content() {
-            @Override
-            public java.lang.String getMinimizedValueAsHTML() {
-                return small;
-            }
-
-            @Override
-            public Component getPopupComponent() {
-                return large;
-            }
-        });
-
+        this(createContent(small, large));
     }
 
     /**
@@ -103,6 +103,30 @@ public class PopupView extends AbstractComponent implements HasComponents {
         setContent(content);
     }
 
+    /**
+     * Creates a Content from given text representation and popup content.
+     * 
+     * @param minimizedValue
+     *            text representation when popup is hidden
+     * @param popupContent
+     *            popup content
+     * @return content with given data
+     */
+    protected static Content createContent(final String minimizedValue,
+            final Component popupContent) {
+        return new Content() {
+            @Override
+            public String getMinimizedValueAsHTML() {
+                return minimizedValue;
+            }
+
+            @Override
+            public Component getPopupComponent() {
+                return popupContent;
+            }
+        };
+    }
+
     /**
      * This method will replace the current content of the panel with a new one.
      * 
@@ -232,6 +256,44 @@ public class PopupView extends AbstractComponent implements HasComponents {
         return (visibleComponent != null ? 1 : 0);
     }
 
+    @Override
+    public void readDesign(Element design, DesignContext designContext) {
+
+        // Read content first to avoid NPE when setting popup visible
+        Component popupContent = null;
+        String minimizedValue = "";
+        for (Node childNode : design.childNodes()) {
+            if (childNode instanceof Element) {
+                Element child = (Element) childNode;
+                if (child.tagName().equals("popup-content")) {
+                    popupContent = designContext.readDesign(child.child(0));
+                } else {
+                    minimizedValue += child.toString();
+                }
+            } else {
+                minimizedValue += childNode.toString();
+            }
+        }
+        setContent(createContent(minimizedValue.trim(), popupContent));
+
+        super.readDesign(design, designContext);
+    }
+
+    @Override
+    public void writeDesign(Element design, DesignContext designContext) {
+        super.writeDesign(design, designContext);
+
+        Element popupContent = new Element(Tag.valueOf("popup-content"), "");
+        popupContent.appendChild(designContext.createElement(content
+                .getPopupComponent()));
+
+        String minimizedHTML = content.getMinimizedValueAsHTML();
+        if (minimizedHTML != null && !minimizedHTML.isEmpty()) {
+            design.append(minimizedHTML);
+        }
+        design.appendChild(popupContent);
+    }
+
     @Override
     protected PopupViewState getState() {
         return (PopupViewState) super.getState();
diff --git a/server/tests/src/com/vaadin/tests/server/component/popupview/PopupViewDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/popupview/PopupViewDeclarativeTest.java
new file mode 100644 (file)
index 0000000..8bad68f
--- /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.popupview;
+
+import org.junit.Test;
+
+import com.vaadin.tests.design.DeclarativeTestBase;
+import com.vaadin.ui.Component;
+import com.vaadin.ui.Label;
+import com.vaadin.ui.PopupView;
+import com.vaadin.ui.VerticalLayout;
+import com.vaadin.ui.declarative.DesignContext;
+
+public class PopupViewDeclarativeTest extends DeclarativeTestBase<PopupView> {
+
+    @Test
+    public void testEmptyPopupView() {
+        PopupView component = new PopupView();
+        Component popup = component.getContent().getPopupComponent();
+        String design = "<v-popup-view><popup-content>"
+                + new DesignContext().createElement(popup)
+                + "</popup-content></v-popup-view>";
+        testWrite(design, component);
+        testRead(design, component);
+    }
+
+    @Test
+    public void testVisiblePopupDesign() {
+        final VerticalLayout verticalLayout = new VerticalLayout();
+        verticalLayout.setWidth("300px");
+        verticalLayout.setHeight("400px");
+
+        PopupView component = new PopupView("Click <u>here</u> to open",
+                verticalLayout);
+        component.setHideOnMouseOut(true);
+        component.setPopupVisible(true);
+        // hide-on-mouse-out is true by default. not seen in design
+        String design = "<v-popup-view popup-visible='true'>" //
+                + "Click <u>here</u> to open"
+                + "<popup-content>"
+                + new DesignContext().createElement(verticalLayout)
+                + "</popup-content>" //
+                + "</v-popup-view>";
+        testWrite(design, component);
+        testRead(design, component);
+    }
+
+    @Test
+    public void testHideOnMouseOutDisabled() {
+        final Label label = new Label("Foo");
+        PopupView component = new PopupView("Click Me!", label);
+        component.setHideOnMouseOut(false);
+        String design = "<v-popup-view hide-on-mouse-out='false'>" //
+                + "Click Me!"
+                + "<popup-content>"
+                + new DesignContext().createElement(label) + "</popup-content>" //
+                + "</v-popup-view>";
+        testWrite(design, component);
+        testRead(design, component);
+    }
+}