]> source.dussan.org Git - vaadin-framework.git/commitdiff
caption and child rendering logick changes
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 6 Aug 2007 12:57:39 +0000 (12:57 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 6 Aug 2007 12:57:39 +0000 (12:57 +0000)
svn changeset:1958/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/Caption.java [new file with mode: 0644]
src/com/itmill/toolkit/terminal/gwt/client/CaptionWrapper.java
src/com/itmill/toolkit/terminal/gwt/client/ui/ICustomLayout.java
src/com/itmill/toolkit/terminal/gwt/client/ui/IHorizontalLayout.java
src/com/itmill/toolkit/terminal/gwt/client/ui/IVerticalLayout.java
src/com/itmill/toolkit/terminal/gwt/client/ui/IWindow.java

diff --git a/src/com/itmill/toolkit/terminal/gwt/client/Caption.java b/src/com/itmill/toolkit/terminal/gwt/client/Caption.java
new file mode 100644 (file)
index 0000000..e60ea59
--- /dev/null
@@ -0,0 +1,30 @@
+package com.itmill.toolkit.terminal.gwt.client;
+
+import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.Label;
+import com.google.gwt.user.client.ui.VerticalPanel;
+import com.google.gwt.user.client.ui.Widget;
+
+public class Caption extends HTML {
+       
+       public Caption()  {
+               setStyleName("i-caption");
+       }
+       
+       public void updateCaption(UIDL uidl) {
+               String c = uidl.getStringAttribute("caption");
+               // TODO Description and error messages
+               if (c == null) {
+               } else {
+                       setText(c);
+               }               
+               setVisible(!uidl.getBooleanAttribute("invisible"));
+       }
+       
+       public static boolean isNeeded(UIDL uidl) {
+               if (uidl.getStringAttribute("caption") != null) return true;
+               // TODO Description and error messages
+               return false;
+       }
+       
+}
index e609798301800f4a06c26db9458a68f275f99890..8b4cb683a5bf449c08a25e971788238d94de8e1c 100644 (file)
@@ -32,12 +32,6 @@ public class CaptionWrapper extends VerticalPanel {
                setVisible(!uidl.getBooleanAttribute("invisible"));
        }
        
-       public static boolean isNeeded(UIDL uidl) {
-               if (uidl.getStringAttribute("caption") != null) return true;
-               // TODO Description and error messages
-               return false;
-       }
-       
        public Widget getWidget() {
                return widget;
        }
index b03c1b3d0314824449f92d8514bf443ddd1d22a5..54cf26cfc5c1c28af7548292f45fee1c97fd3917 100644 (file)
@@ -8,6 +8,7 @@ import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.ui.ComplexPanel;
 import com.google.gwt.user.client.ui.Widget;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
+import com.itmill.toolkit.terminal.gwt.client.Caption;
 import com.itmill.toolkit.terminal.gwt.client.CaptionWrapper;
 import com.itmill.toolkit.terminal.gwt.client.Layout;
 import com.itmill.toolkit.terminal.gwt.client.Paintable;
@@ -267,7 +268,7 @@ public class ICustomLayout extends ComplexPanel implements Paintable, Layout {
        /** Update caption for given widget */
        public void updateCaption(Widget component, UIDL uidl) {
                CaptionWrapper wrapper = (CaptionWrapper) widgetToCaptionWrapper.get(component);
-               if (CaptionWrapper.isNeeded(uidl)) {
+               if (Caption.isNeeded(uidl)) {
                        if (wrapper == null) {
                                String loc = getLocation(component);
                                super.remove(component);
index d0ef747f31fb6453b4f02ab2329b42ee032bcdfe..17935d5862a2846b123947f28c21df444964d23d 100644 (file)
@@ -5,6 +5,7 @@ import java.util.Iterator;
 
 import com.google.gwt.user.client.ui.HorizontalPanel;
 import com.google.gwt.user.client.ui.Widget;
+import com.itmill.toolkit.terminal.gwt.client.Caption;
 import com.itmill.toolkit.terminal.gwt.client.CaptionWrapper;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
 import com.itmill.toolkit.terminal.gwt.client.Layout;
@@ -53,7 +54,7 @@ public class IHorizontalLayout extends HorizontalPanel implements Paintable, Lay
        public void updateCaption(Widget component, UIDL uidl) {
                
                CaptionWrapper wrapper = (CaptionWrapper) componentToWrapper.get(component);
-               if (CaptionWrapper.isNeeded(uidl)) {
+               if (Caption.isNeeded(uidl)) {
                        if (wrapper == null) {
                                int index = getWidgetIndex(component);
                                remove(component);
index 4888f16b4be4be2726c5066bf05eaeae82cd2192..05457f97df9d8b103bf2ba42b00628495861d2c4 100644 (file)
 package com.itmill.toolkit.terminal.gwt.client.ui;
 
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.user.client.ui.Widget;
-import com.itmill.toolkit.terminal.gwt.client.CaptionWrapper;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
+import com.itmill.toolkit.terminal.gwt.client.Caption;
 import com.itmill.toolkit.terminal.gwt.client.Layout;
 import com.itmill.toolkit.terminal.gwt.client.Paintable;
 import com.itmill.toolkit.terminal.gwt.client.UIDL;
 
 public class IVerticalLayout extends VerticalPanel implements Paintable, Layout {
 
-       private HashMap componentToWrapper = new HashMap();
+       private HashMap componentToCaption = new HashMap();
+
+       private ApplicationConnection client;
        
        public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
                
+               this.client = client;
+               
                // Ensure correct implementation
                if (client.updateComponent(this, uidl, false))
                        return;
 
-               // TODO Should update instead of just redraw
-               clear();
-               componentToWrapper.clear();
-               
-               for (Iterator i = uidl.getChildIterator(); i.hasNext();) {
-                       UIDL uidlForChild = (UIDL) i.next();
+               ArrayList uidlWidgets = new ArrayList();
+               for (Iterator it = uidl.getChildIterator(); it.hasNext();) {
+                       UIDL uidlForChild = (UIDL) it.next();
                        Widget child = client.getWidget(uidlForChild);
-                       add(child);
                        ((Paintable)child).updateFromUIDL(uidlForChild, client);
+                       uidlWidgets.add(child);
                }
+               
+               ArrayList oldWidgets = getPaintables();
+               
+               Iterator oldIt = oldWidgets.iterator();
+               Iterator newIt = uidlWidgets.iterator();
+               
+               Widget oldChild = null;
+               while(newIt.hasNext()) {
+                       Widget child = (Widget) newIt.next();
+                       if(oldChild == null && oldIt.hasNext()) {
+                               // search for next old Paintable which still exists in layout
+                               // and delete others
+                               while(oldIt.hasNext()) {
+                                       oldChild = (Widget) oldIt.next();
+                                       // if faced a caption, bypass it
+                                       if (oldChild instanceof Caption) {
+                                               oldChild = (Widget) oldIt.next();
+                                       }
+                                       // now oldChild is an instance of Paintable
+                                       if(uidlWidgets.contains(oldChild))
+                                               break;
+                                       else {
+                                               client.unregisterPaintable((Paintable) oldChild);
+                                               remove(oldChild);
+                                               oldChild = null;
+                                       }
+                               }
+                       }
+                       if(oldChild == null) {
+                               // we are adding components to layout
+                               add(child);
+                               continue;
+                       }
+                       if(child == oldChild) {
+                               // child already attached and updated
+                               if(oldIt.hasNext())
+                                       oldChild = (Widget) oldIt.next();
+                               continue;
+                       }
+                       if(hasChildComponent(child)) {
+                               // some components have been moved or removed
+                               // detach them temporarely
+                               while(oldChild != child) {
+                                       remove(oldChild);
+                                       oldChild = (Widget) oldIt.next();
+                               }
+                       } else {
+                               this.insert(child, this.getWidgetIndex(oldChild));
+                       }
+               }
+               // remove possibly remaining old children which were not updated ~ removed
+               while(oldIt.hasNext())
+                       remove((Widget) oldIt.next());
+       }
+       
+       private ArrayList getPaintables() {
+               ArrayList al = new ArrayList();
+               Iterator it = iterator();
+               while (it.hasNext()) {
+                       Widget w = (Widget) it.next();
+                       if (w instanceof Paintable)
+                               al.add(w);
+               }
+               return al;
+       }
+       
+       public boolean remove(Widget w) {
+               if (w instanceof Paintable) {
+                       Caption c = (Caption) componentToCaption.get(w);
+                       if(c != null) {
+                               componentToCaption.remove(c);
+                               remove(c);
+                       }
+                       client.unregisterPaintable((Paintable) w);
+               }
+               return super.remove(w);
        }
 
+       /* (non-Javadoc)
+        * @see com.itmill.toolkit.terminal.gwt.client.Layout#replaceChildComponent(com.google.gwt.user.client.ui.Widget, com.google.gwt.user.client.ui.Widget)
+        */
        public void replaceChildComponent(Widget from, Widget to) {
-               CaptionWrapper wrapper = (CaptionWrapper) componentToWrapper.get(from);
-               if (wrapper != null) {
-                       componentToWrapper.remove(from);
-                       from = wrapper;
+               client.unregisterPaintable((Paintable) from);
+               Caption c = (Caption) componentToCaption.get(from);
+               if (c != null) {
+                       remove(c);
+                       componentToCaption.remove(c);
                }
                int index = getWidgetIndex(from);
                if (index >= 0) {
@@ -47,27 +129,25 @@ public class IVerticalLayout extends VerticalPanel implements Paintable, Layout
        }
 
        public boolean hasChildComponent(Widget component) {
-               return getWidgetIndex(component) >= 0 || componentToWrapper.get(component) != null;
+               return getWidgetIndex(component) >= 0;
        }
 
        public void updateCaption(Widget component, UIDL uidl) {
                
-               CaptionWrapper wrapper = (CaptionWrapper) componentToWrapper.get(component);
-               if (CaptionWrapper.isNeeded(uidl)) {
-                       if (wrapper == null) {
+               Caption c  = (Caption) componentToCaption.get(component);
+               
+               if (Caption.isNeeded(uidl)) {
+                       if (c == null) {
                                int index = getWidgetIndex(component);
-                               remove(component);
-                               wrapper = new CaptionWrapper(component);
-                               insert(wrapper, index);
-                               componentToWrapper.put(component, wrapper);
+                               c = new Caption();
+                               insert(c, index);
+                               componentToCaption.put(component, c);
                        }
-                       wrapper.updateCaption(uidl);
+                       c.updateCaption(uidl);
                } else {
-                       if (wrapper != null) { 
-                               int index = getWidgetIndex(wrapper);
-                               remove(wrapper);
-                               insert(wrapper.getWidget(), index);
-                               componentToWrapper.remove(component);
+                       if (c != null) { 
+                               remove(c);
+                               componentToCaption.remove(component);
                        }
                }
        }
index c6a271f4a6952bd373fcd92f4fb05904f6f2be49..5e468ed1a7f05b5839c83f82ebacab90993f60d3 100644 (file)
@@ -1,21 +1,38 @@
 package com.itmill.toolkit.terminal.gwt.client.ui;
 
+import com.google.gwt.user.client.ui.SimplePanel;
+import com.google.gwt.user.client.ui.Widget;
 import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
 import com.itmill.toolkit.terminal.gwt.client.Paintable;
 import com.itmill.toolkit.terminal.gwt.client.UIDL;
 
-public class IWindow extends IVerticalLayout implements Paintable {
+public class IWindow extends SimplePanel implements Paintable {
        
        private String theme;
        
+       private Paintable layout;
+       
        public String getTheme() {
                return theme;
        }
        
        public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
                theme = uidl.getStringAttribute("theme");
-               super.updateFromUIDL( uidl,  client);
                com.google.gwt.user.client.Window.setTitle(uidl.getStringAttribute("caption"));
+               UIDL childUidl = uidl.getChildUIDL(0);
+               Paintable lo = (Paintable) client.getWidget(childUidl);
+               if(layout != null) {
+                       if(layout != lo) {
+                               // remove old
+                               client.unregisterPaintable(layout);
+                               // add new
+                               setWidget((Widget) lo);
+                               layout = lo;
+                       }
+               } else {
+                       setWidget((Widget) lo);
+               }
+               lo.updateFromUIDL(childUidl, client);
        }
 
 }