]> source.dussan.org Git - vaadin-framework.git/commitdiff
Customlayout works
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>
Fri, 15 Jun 2007 06:51:13 +0000 (06:51 +0000)
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>
Fri, 15 Jun 2007 06:51:13 +0000 (06:51 +0000)
svn changeset:1742/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/Client.java
src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java

index 174b4797f9e85191c1a3dce34b01cd0c26104ed8..1cc34dad2eecbf7b365b5f62d52521f57e71c577 100755 (executable)
@@ -2,6 +2,7 @@ package com.itmill.toolkit.terminal.gwt.client;
 
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Vector;
 
 import com.google.gwt.core.client.EntryPoint;
@@ -13,6 +14,7 @@ import com.google.gwt.http.client.Response;
 import com.google.gwt.json.client.JSONArray;
 import com.google.gwt.json.client.JSONObject;
 import com.google.gwt.json.client.JSONParser;
+import com.google.gwt.json.client.JSONString;
 import com.google.gwt.json.client.JSONValue;
 import com.google.gwt.user.client.ui.FocusWidget;
 import com.google.gwt.user.client.ui.RootPanel;
@@ -27,6 +29,8 @@ public class Client implements EntryPoint {
 
        private String appUri;
 
+       private HashMap resourcesMap = new HashMap();
+
        // TODO remove repaintAll until things start to pile up
        private RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, appUri
                        + "/UIDL/?repaintAll=1&");
@@ -109,6 +113,14 @@ public class Client implements EntryPoint {
                        console.log(jsonText);
                        return;
                }
+               // Store resources
+               JSONObject resources = (JSONObject) ((JSONObject) json)
+                               .get("resources");
+               for (Iterator i = resources.keySet().iterator(); i.hasNext();) {
+                       String key = (String) i.next();
+                       resourcesMap.put(key, ((JSONString)resources.get(key)).stringValue());
+               }
+
                // Process changes
                JSONArray changes = (JSONArray) ((JSONObject) json).get("changes");
                for (int i = 0; i < changes.size(); i++) {
@@ -302,9 +314,9 @@ public class Client implements EntryPoint {
                // Visibility, Disabling and read-only status
                if (component instanceof FocusWidget) {
                        boolean enabled = true;
-                       if(uidl.hasAttribute("disabled"))
+                       if (uidl.hasAttribute("disabled"))
                                enabled = !uidl.getBooleanAttribute("disabled");
-                       else if(uidl.hasAttribute("readonly")) 
+                       else if (uidl.hasAttribute("readonly"))
                                enabled = !uidl.getBooleanAttribute("readonly");
                        ((FocusWidget) component).setEnabled(enabled);
                }
@@ -336,5 +348,9 @@ public class Client implements EntryPoint {
                registerPaintable(id, (Paintable) w);
                return w;
        }
+       
+       public String getResource(String name) {
+               return (String) resourcesMap.get(name);
+       }
 
 }
index b650d07e463cea9bf53f7fba8e2dedb0a786721c..769733645e6aed2f447f4c53d7a193edc9862b81 100644 (file)
@@ -3,6 +3,8 @@ package com.itmill.toolkit.terminal.gwt.client.ui;
 import java.util.HashMap;
 import java.util.Iterator;
 
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.ui.HTMLPanel;
 import com.google.gwt.user.client.ui.SimplePanel;
 import com.google.gwt.user.client.ui.VerticalPanel;
@@ -17,36 +19,54 @@ public class ICustomLayout extends SimplePanel implements Paintable, Layout {
 
        private HashMap componentToWrapper = new HashMap();
        HTMLPanel html;
-       private static HashMap styleToTemplate = new HashMap();
        String currentStyle;
+       String locationPrefix = HTMLPanel.createUniqueId() + "_";
        
        public void updateFromUIDL(UIDL uidl, Client client) {
                
                if (client.updateComponent(this, uidl, false)) return;
                
-               // Initialize HTMLPanel when needed
-               String newStyle = uidl.getStringAttribute("style");
-               if (currentStyle == null || !currentStyle.equals(newStyle)) {
-                       String template = (String) styleToTemplate.get(newStyle);
-                       if (template == null) {
-                               template = "custom layout of style " + newStyle + " <div location=\"one\"></div>foobar";
-                               styleToTemplate.put(newStyle, template);
-                       }
-                       html = new HTMLPanel(template);
-                       // TODO Map locations
-                       add(html);
-               }
+               updateHTML(uidl, client);
                
                componentToWrapper.clear();
                
                for (Iterator i = uidl.getChildIterator(); i.hasNext();) {
                        UIDL uidlForChild = (UIDL) i.next();
-                       Widget child = client.getWidget(uidlForChild);
-                       //html.add(child);
-                       ((Paintable)child).updateFromUIDL(uidlForChild, client);
+                       if (uidlForChild.getTag().equals("location")) {
+                               String location = uidlForChild.getStringAttribute("name");
+                               Widget child = client.getWidget(uidlForChild.getChildUIDL(0));
+                               html.add(child,locationPrefix + location);
+                               ((Paintable)child).updateFromUIDL(uidlForChild.getChildUIDL(0), client);
+                               
+                       }
                }
        }
 
+       private void updateHTML(UIDL uidl, Client client) {
+               String newStyle = uidl.getStringAttribute("style");
+               if (currentStyle != null && currentStyle.equals(newStyle)) return;
+               
+                       String template = client.getResource("layout/"+newStyle+".html");
+                       if (template == null) {
+                               template = "Layout " + newStyle + " is missing";
+                       } else {
+                               currentStyle = newStyle;
+                       }
+                       html = new HTMLPanel(template);
+                       add(html);
+                       
+                       addUniqueIdsForLocations(html.getElement(), locationPrefix);
+       }
+
+       private native void addUniqueIdsForLocations(Element e, String idPrefix) /*-{
+               var divs = e.getElementsByTagName("div"); 
+               for (var i = 0; i < divs.length; i++) {
+                       var div = divs[i];
+                       var location = div.getAttribute("location");
+                       if (location != null) div.setAttribute("id",idPrefix + location);
+               }                       
+       }-*/;
+
        public void replaceChildComponent(Widget from, Widget to) {
                CaptionWrapper wrapper = (CaptionWrapper) componentToWrapper.get(from);
                if (wrapper != null) {