From c6ed4be419abfe80d449cfcea32bbb470b7fd73c Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 25 Aug 2008 11:15:55 +0000 Subject: [PATCH] Fixes #1975 - CustomLayout template can be specified in java svn changeset:5261/svn branch:trunk --- .../tests-tickets/layouts/Ticket1975.html | 1 + .../terminal/gwt/client/ui/ICustomLayout.java | 35 +++++++---- .../toolkit/tests/tickets/Ticket1975.java | 60 +++++++++++++++++++ src/com/itmill/toolkit/ui/CustomLayout.java | 49 ++++++++++++++- 4 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html create mode 100644 src/com/itmill/toolkit/tests/tickets/Ticket1975.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 index 0000000000..26b6548dd0 --- /dev/null +++ b/WebContent/ITMILL/themes/tests-tickets/layouts/Ticket1975.html @@ -0,0 +1 @@ +Testing custom layout.. 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 = "Layout file layouts/" - + newTemplate - + ".html is missing. Components will be drawn for debug purposes."; + 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 = "Layout file layouts/" + + newTemplate + + ".html is missing. Components will be drawn for debug purposes."; + } 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 = "Blah"; + 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("