]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fixes #1975 - CustomLayout template can be specified in java
authorArtur Signell <artur.signell@itmill.com>
Mon, 25 Aug 2008 11:15:55 +0000 (11:15 +0000)
committerArtur Signell <artur.signell@itmill.com>
Mon, 25 Aug 2008 11:15:55 +0000 (11:15 +0000)
svn changeset:5261/svn branch:trunk

WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html [new file with mode: 0644]
src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java
src/com/itmill/toolkit/tests/tickets/Ticket1975.java [new file with mode: 0644]
src/com/itmill/toolkit/ui/CustomLayout.java

diff --git a/WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html b/WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html
new file mode 100644 (file)
index 0000000..26b6548
--- /dev/null
@@ -0,0 +1 @@
+<b>Testing custom layout..</b>\r
index 8b7349d187649daf8c322244b330d58c96a040b8..c5ee3a231a89abb270bea4d91cf23e49d69dbf75 100644 (file)
@@ -42,8 +42,8 @@ public class ICustomLayout extends ComplexPanel implements Paintable,
     /** Widget to captionwrapper map */
     private final HashMap widgetToCaptionWrapper = new HashMap();
 
-    /** Currently rendered style */
-    String currentTemplate;
+    /** Name of the currently rendered style */
+    String currentTemplateName;
 
     /** Unexecuted scripts loaded from the template */
     private String scripts = "";
@@ -53,6 +53,9 @@ public class ICustomLayout extends ComplexPanel implements Paintable,
 
     private ApplicationConnection client;
 
+    /** Has the template been loaded from contents passed in UIDL **/
+    private boolean hasTemplateContents = false;
+
     public ICustomLayout() {
         setElement(DOM.createDiv());
         // Clear any unwanted styling
@@ -171,17 +174,27 @@ public class ICustomLayout extends ComplexPanel implements Paintable,
     /** Initialize HTML-layout. */
     private void initializeHTML(UIDL uidl, ApplicationConnection client) {
 
+        final String newTemplateContents = uidl
+                .getStringAttribute("templateContents");
         final String newTemplate = uidl.getStringAttribute("template");
 
-        // Get the HTML-template from client
-        String template = client
-                .getResource("layouts/" + newTemplate + ".html");
-        if (template == null) {
-            template = "<em>Layout file layouts/"
-                    + newTemplate
-                    + ".html is missing. Components will be drawn for debug purposes.</em>";
+        currentTemplateName = null;
+        hasTemplateContents = false;
+
+        String template = "";
+        if (newTemplate != null) {
+            // Get the HTML-template from client
+            template = client.getResource("layouts/" + newTemplate + ".html");
+            if (template == null) {
+                template = "<em>Layout file layouts/"
+                        + newTemplate
+                        + ".html is missing. Components will be drawn for debug purposes.</em>";
+            } else {
+                currentTemplateName = newTemplate;
+            }
         } else {
-            currentTemplate = newTemplate;
+            hasTemplateContents = true;
+            template = newTemplateContents;
         }
 
         // Connect body of the template to DOM
@@ -208,7 +221,7 @@ public class ICustomLayout extends ComplexPanel implements Paintable,
     }-*/;
 
     private boolean hasTemplate() {
-        if (currentTemplate == null) {
+        if (currentTemplateName == null && !hasTemplateContents) {
             return false;
         } else {
             return true;
diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket1975.java b/src/com/itmill/toolkit/tests/tickets/Ticket1975.java
new file mode 100644 (file)
index 0000000..d012062
--- /dev/null
@@ -0,0 +1,60 @@
+package com.itmill.toolkit.tests.tickets;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+\r
+import com.itmill.toolkit.Application;\r
+import com.itmill.toolkit.terminal.gwt.server.WebApplicationContext;\r
+import com.itmill.toolkit.ui.Button;\r
+import com.itmill.toolkit.ui.CustomLayout;\r
+import com.itmill.toolkit.ui.GridLayout;\r
+import com.itmill.toolkit.ui.Window;\r
+import com.itmill.toolkit.ui.Button.ClickEvent;\r
+import com.itmill.toolkit.ui.Button.ClickListener;\r
+\r
+public class Ticket1975 extends Application {\r
+\r
+    private CustomLayout cl1;\r
+    private CustomLayout cl2;\r
+\r
+    public void init() {\r
+        Window w = new Window(getClass().getSimpleName());\r
+        setMainWindow(w);\r
+        setTheme("tests-tickets");\r
+        GridLayout layout = new GridLayout(1, 10);\r
+        w.setLayout(layout);\r
+        createUI(layout);\r
+    }\r
+\r
+    private void createUI(GridLayout layout) {\r
+        String s = "<b>Blah</b><input type=\"text\" value='Lorem\" ipsum'/>";\r
+        try {\r
+            cl1 = new CustomLayout(new ByteArrayInputStream(s.getBytes()));\r
+            layout.addComponent(cl1);\r
+            WebApplicationContext wc = ((WebApplicationContext) getContext());\r
+\r
+            layout.addComponent(new Button("Disable/Enable",\r
+                    new ClickListener() {\r
+\r
+                        public void buttonClick(ClickEvent event) {\r
+                            boolean e = cl1.isEnabled();\r
+\r
+                            cl1.setEnabled(!e);\r
+                            cl2.setEnabled(!e);\r
+                        }\r
+\r
+                    }));\r
+            File f = new File(wc.getBaseDirectory().getAbsoluteFile()\r
+                    + "/ITMILL/themes/" + getTheme() + "/layouts/Ticket1975.html");\r
+                    \r
+            cl2 = new CustomLayout(new FileInputStream(f));\r
+            layout.addComponent(cl2);\r
+\r
+        } catch (Exception e) {\r
+            // TODO Auto-generated catch block\r
+            e.printStackTrace();\r
+        }\r
+\r
+    }\r
+}\r
index 69c0bc9a6138b2eaacf1592d9bf984b4b9236685..0cb8851202e89596f278eda1d728b5bf7c49c996 100644 (file)
@@ -4,6 +4,9 @@
 
 package com.itmill.toolkit.ui;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.Iterator;
 
@@ -39,15 +42,50 @@ import com.itmill.toolkit.terminal.PaintTarget;
  */
 public class CustomLayout extends AbstractLayout {
 
+    private static final int BUFFER_SIZE = 10000;
+
     /**
      * Custom layout slots containing the components.
      */
     private final HashMap slots = new HashMap();
 
-    private String templateName;
+    private String templateContents = null;
+
+    private String templateName = null;
 
     /**
-     * Constructor for custom layout with given template name.
+     * Constructs a custom layout with the template given in the stream.
+     * 
+     * @param templateStream
+     *                Stream containing template data. Must be using UTF-8
+     *                encoding. To use a String as a template use for instance
+     *                new ByteArrayInputStream("<template>".getBytes()).
+     * @param streamLength
+     *                Length of the templateStream
+     * @throws IOException
+     */
+    public CustomLayout(InputStream templateStream) throws IOException {
+
+        InputStreamReader reader = new InputStreamReader(templateStream);
+        StringBuffer b = new StringBuffer(BUFFER_SIZE);
+
+        char[] cbuf = new char[BUFFER_SIZE];
+        int offset = 0;
+
+        while (true) {
+            int nrRead = reader.read(cbuf, offset, BUFFER_SIZE);
+            b.append(cbuf, 0, nrRead);
+            if (nrRead < BUFFER_SIZE) {
+                break;
+            }
+        }
+
+        templateContents = b.toString();
+    }
+
+    /**
+     * Constructor for custom layout with given template name. Template file is
+     * fetched from "<theme>/layout/<templateName>".
      */
     public CustomLayout(String template) {
         templateName = template;
@@ -153,7 +191,11 @@ public class CustomLayout extends AbstractLayout {
     public void paintContent(PaintTarget target) throws PaintException {
         super.paintContent(target);
 
-        target.addAttribute("template", templateName);
+        if (templateName != null) {
+            target.addAttribute("template", templateName);
+        } else {
+            target.addAttribute("templateContents", templateContents);
+        }
         // Adds all items in all the locations
         for (final Iterator i = slots.keySet().iterator(); i.hasNext();) {
             // Gets the (location,component)
@@ -225,6 +267,7 @@ public class CustomLayout extends AbstractLayout {
      */
     public void setTemplateName(String templateName) {
         this.templateName = templateName;
+        templateContents = null;
         requestRepaint();
     }