Changeset 5261

Show
Ignore:
Timestamp:
08/25/08 11:15:55 (3 months ago)
Author:
artur.signell@…
Message:

Fixes #1975 - CustomLayout? template can be specified in java

Location:
trunk
Files:
3 added
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java

    r5161 r5261  
    4343    private final HashMap widgetToCaptionWrapper = new HashMap(); 
    4444 
    45     /** Currently rendered style */ 
    46     String currentTemplate; 
     45    /** Name of the currently rendered style */ 
     46    String currentTemplateName; 
    4747 
    4848    /** Unexecuted scripts loaded from the template */ 
     
    5353 
    5454    private ApplicationConnection client; 
     55 
     56    /** Has the template been loaded from contents passed in UIDL **/ 
     57    private boolean hasTemplateContents = false; 
    5558 
    5659    public ICustomLayout() { 
     
    172175    private void initializeHTML(UIDL uidl, ApplicationConnection client) { 
    173176 
     177        final String newTemplateContents = uidl 
     178                .getStringAttribute("templateContents"); 
    174179        final String newTemplate = uidl.getStringAttribute("template"); 
    175180 
    176         // Get the HTML-template from client 
    177         String template = client 
    178                 .getResource("layouts/" + newTemplate + ".html"); 
    179         if (template == null) { 
    180             template = "<em>Layout file layouts/" 
    181                     + newTemplate 
    182                     + ".html is missing. Components will be drawn for debug purposes.</em>"; 
    183         } else { 
    184             currentTemplate = newTemplate; 
     181        currentTemplateName = null; 
     182        hasTemplateContents = false; 
     183 
     184        String template = ""; 
     185        if (newTemplate != null) { 
     186            // Get the HTML-template from client 
     187            template = client.getResource("layouts/" + newTemplate + ".html"); 
     188            if (template == null) { 
     189                template = "<em>Layout file layouts/" 
     190                        + newTemplate 
     191                        + ".html is missing. Components will be drawn for debug purposes.</em>"; 
     192            } else { 
     193                currentTemplateName = newTemplate; 
     194            } 
     195        } else { 
     196            hasTemplateContents = true; 
     197            template = newTemplateContents; 
    185198        } 
    186199 
     
    209222 
    210223    private boolean hasTemplate() { 
    211         if (currentTemplate == null) { 
     224        if (currentTemplateName == null && !hasTemplateContents) { 
    212225            return false; 
    213226        } else { 
  • trunk/src/com/itmill/toolkit/ui/CustomLayout.java

    r4747 r5261  
    55package com.itmill.toolkit.ui; 
    66 
     7import java.io.IOException; 
     8import java.io.InputStream; 
     9import java.io.InputStreamReader; 
    710import java.util.HashMap; 
    811import java.util.Iterator; 
     
    4043public class CustomLayout extends AbstractLayout { 
    4144 
     45    private static final int BUFFER_SIZE = 10000; 
     46 
    4247    /** 
    4348     * Custom layout slots containing the components. 
     
    4550    private final HashMap slots = new HashMap(); 
    4651 
    47     private String templateName; 
    48  
    49     /** 
    50      * Constructor for custom layout with given template name. 
     52    private String templateContents = null; 
     53 
     54    private String templateName = null; 
     55 
     56    /** 
     57     * Constructs a custom layout with the template given in the stream. 
     58     *  
     59     * @param templateStream 
     60     *                Stream containing template data. Must be using UTF-8 
     61     *                encoding. To use a String as a template use for instance 
     62     *                new ByteArrayInputStream("<template>".getBytes()). 
     63     * @param streamLength 
     64     *                Length of the templateStream 
     65     * @throws IOException 
     66     */ 
     67    public CustomLayout(InputStream templateStream) throws IOException { 
     68 
     69        InputStreamReader reader = new InputStreamReader(templateStream); 
     70        StringBuffer b = new StringBuffer(BUFFER_SIZE); 
     71 
     72        char[] cbuf = new char[BUFFER_SIZE]; 
     73        int offset = 0; 
     74 
     75        while (true) { 
     76            int nrRead = reader.read(cbuf, offset, BUFFER_SIZE); 
     77            b.append(cbuf, 0, nrRead); 
     78            if (nrRead < BUFFER_SIZE) { 
     79                break; 
     80            } 
     81        } 
     82 
     83        templateContents = b.toString(); 
     84    } 
     85 
     86    /** 
     87     * Constructor for custom layout with given template name. Template file is 
     88     * fetched from "<theme>/layout/<templateName>". 
    5189     */ 
    5290    public CustomLayout(String template) { 
     
    154192        super.paintContent(target); 
    155193 
    156         target.addAttribute("template", templateName); 
     194        if (templateName != null) { 
     195            target.addAttribute("template", templateName); 
     196        } else { 
     197            target.addAttribute("templateContents", templateContents); 
     198        } 
    157199        // Adds all items in all the locations 
    158200        for (final Iterator i = slots.keySet().iterator(); i.hasNext();) { 
     
    226268    public void setTemplateName(String templateName) { 
    227269        this.templateName = templateName; 
     270        templateContents = null; 
    228271        requestRepaint(); 
    229272    }