--- /dev/null
+<b>Testing custom layout..</b>\r
/** 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 = "";
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
/** 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
}-*/;
private boolean hasTemplate() {
- if (currentTemplate == null) {
+ if (currentTemplateName == null && !hasTemplateContents) {
return false;
} else {
return true;
--- /dev/null
+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
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;
*/
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;
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)
*/
public void setTemplateName(String templateName) {
this.templateName = templateName;
+ templateContents = null;
requestRepaint();
}