diff options
author | Artur Signell <artur.signell@itmill.com> | 2008-08-25 11:15:55 +0000 |
---|---|---|
committer | Artur Signell <artur.signell@itmill.com> | 2008-08-25 11:15:55 +0000 |
commit | c6ed4be419abfe80d449cfcea32bbb470b7fd73c (patch) | |
tree | fb3681cfe33a8113e182d9080a0d2150e92e43d6 | |
parent | 0788fec0540cca65255a253698efe3d5d2df62d6 (diff) | |
download | vaadin-framework-c6ed4be419abfe80d449cfcea32bbb470b7fd73c.tar.gz vaadin-framework-c6ed4be419abfe80d449cfcea32bbb470b7fd73c.zip |
Fixes #1975 - CustomLayout template can be specified in java
svn changeset:5261/svn branch:trunk
4 files changed, 131 insertions, 14 deletions
diff --git a/WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html b/WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html new file mode 100644 index 0000000000..26b6548dd0 --- /dev/null +++ b/WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html @@ -0,0 +1 @@ +<b>Testing custom layout..</b>
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java index 8b7349d187..c5ee3a231a 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java @@ -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 index 0000000000..d0120624c1 --- /dev/null +++ b/src/com/itmill/toolkit/tests/tickets/Ticket1975.java @@ -0,0 +1,60 @@ +package com.itmill.toolkit.tests.tickets;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.terminal.gwt.server.WebApplicationContext;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.CustomLayout;
+import com.itmill.toolkit.ui.GridLayout;
+import com.itmill.toolkit.ui.Window;
+import com.itmill.toolkit.ui.Button.ClickEvent;
+import com.itmill.toolkit.ui.Button.ClickListener;
+
+public class Ticket1975 extends Application {
+
+ private CustomLayout cl1;
+ private CustomLayout cl2;
+
+ public void init() {
+ Window w = new Window(getClass().getSimpleName());
+ setMainWindow(w);
+ setTheme("tests-tickets");
+ GridLayout layout = new GridLayout(1, 10);
+ w.setLayout(layout);
+ createUI(layout);
+ }
+
+ private void createUI(GridLayout layout) {
+ String s = "<b>Blah</b><input type=\"text\" value='Lorem\" ipsum'/>";
+ try {
+ cl1 = new CustomLayout(new ByteArrayInputStream(s.getBytes()));
+ layout.addComponent(cl1);
+ WebApplicationContext wc = ((WebApplicationContext) getContext());
+
+ layout.addComponent(new Button("Disable/Enable",
+ new ClickListener() {
+
+ public void buttonClick(ClickEvent event) {
+ boolean e = cl1.isEnabled();
+
+ cl1.setEnabled(!e);
+ cl2.setEnabled(!e);
+ }
+
+ }));
+ File f = new File(wc.getBaseDirectory().getAbsoluteFile()
+ + "/ITMILL/themes/" + getTheme() + "/layouts/Ticket1975.html");
+
+ cl2 = new CustomLayout(new FileInputStream(f));
+ layout.addComponent(cl2);
+
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+}
diff --git a/src/com/itmill/toolkit/ui/CustomLayout.java b/src/com/itmill/toolkit/ui/CustomLayout.java index 69c0bc9a61..0cb8851202 100644 --- a/src/com/itmill/toolkit/ui/CustomLayout.java +++ b/src/com/itmill/toolkit/ui/CustomLayout.java @@ -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(); } |