]> source.dussan.org Git - vaadin-framework.git/commitdiff
combined horizontal and vertical layouts
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 8 Aug 2007 09:44:52 +0000 (09:44 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 8 Aug 2007 09:44:52 +0000 (09:44 +0000)
svn changeset:1965/svn branch:trunk

src/com/itmill/toolkit/terminal/gwt/client/Caption.java
src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java
src/com/itmill/toolkit/terminal/gwt/client/ui/IHorizontalLayout.java [deleted file]
src/com/itmill/toolkit/terminal/gwt/client/ui/IOrderedLayout.java [new file with mode: 0644]
src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java
src/com/itmill/toolkit/terminal/gwt/client/ui/IVerticalLayout.java [deleted file]

index e60ea59566b0abfd2e35048f131e3d01ccd4ddf9..06fca38534b61bf22667a8ec8a2be3f33a61581d 100644 (file)
@@ -7,7 +7,10 @@ import com.google.gwt.user.client.ui.Widget;
 
 public class Caption extends HTML {
        
-       public Caption()  {
+       private Widget owner;
+
+       public Caption(Widget component)  {
+               owner = component;
                setStyleName("i-caption");
        }
        
index 8ebffa6da012fe38b9a444d9f09b962c20f8b0fe..907b8c4a474d1b50080ccb779a5b660f4ef18cf5 100644 (file)
@@ -9,7 +9,6 @@ import com.itmill.toolkit.terminal.gwt.client.ui.IComponent;
 import com.itmill.toolkit.terminal.gwt.client.ui.ICustomLayout;
 import com.itmill.toolkit.terminal.gwt.client.ui.IEmbedded;
 import com.itmill.toolkit.terminal.gwt.client.ui.IGridLayout;
-import com.itmill.toolkit.terminal.gwt.client.ui.IHorizontalLayout;
 import com.itmill.toolkit.terminal.gwt.client.ui.ILabel;
 import com.itmill.toolkit.terminal.gwt.client.ui.ILink;
 import com.itmill.toolkit.terminal.gwt.client.ui.IOptionGroup;
@@ -27,7 +26,7 @@ import com.itmill.toolkit.terminal.gwt.client.ui.ITextualDate;
 import com.itmill.toolkit.terminal.gwt.client.ui.ITree;
 import com.itmill.toolkit.terminal.gwt.client.ui.ITwinColSelect;
 import com.itmill.toolkit.terminal.gwt.client.ui.IUnknownComponent;
-import com.itmill.toolkit.terminal.gwt.client.ui.IVerticalLayout;
+import com.itmill.toolkit.terminal.gwt.client.ui.IOrderedLayout;
 import com.itmill.toolkit.terminal.gwt.client.ui.IWindow;
 
 public class DefaultWidgetFactory implements WidgetFactory {
@@ -45,9 +44,9 @@ public class DefaultWidgetFactory implements WidgetFactory {
                        return new IWindow();
                else if ("orderedlayout".equals(tag)) {
                        if ("horizontal".equals(uidl.getStringAttribute("orientation")))
-                               return new IHorizontalLayout();
+                               return new IOrderedLayout(IOrderedLayout.ORIENTATION_HORIZONTAL);
                        else
-                               return new IVerticalLayout();
+                               return new IOrderedLayout();
                }
                else if ("label".equals(tag))
                        return new ILabel();
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IHorizontalLayout.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IHorizontalLayout.java
deleted file mode 100644 (file)
index 17935d5..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-package com.itmill.toolkit.terminal.gwt.client.ui;
-
-import java.util.HashMap;
-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;
-import com.itmill.toolkit.terminal.gwt.client.Paintable;
-import com.itmill.toolkit.terminal.gwt.client.UIDL;
-
-public class IHorizontalLayout extends HorizontalPanel implements Paintable, Layout {
-
-       private HashMap componentToWrapper = new HashMap();
-
-       public void updateFromUIDL(UIDL uidl, ApplicationConnection 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();
-                       Widget child = client.getWidget(uidlForChild);
-                       add(child);
-                       ((Paintable)child).updateFromUIDL(uidlForChild, client);
-               }
-       }
-       
-       public void replaceChildComponent(Widget from, Widget to) {
-               CaptionWrapper wrapper = (CaptionWrapper) componentToWrapper.get(from);
-               if (wrapper != null) {
-                       componentToWrapper.remove(from);
-                       from = wrapper;
-               }
-               int index = getWidgetIndex(from);
-               if (index >= 0) {
-                       remove(index);
-                       insert(to, index);
-               }
-       }
-
-       public boolean hasChildComponent(Widget component) {
-               return getWidgetIndex(component) >= 0 || componentToWrapper.get(component) != null;
-       }
-
-       public void updateCaption(Widget component, UIDL uidl) {
-               
-               CaptionWrapper wrapper = (CaptionWrapper) componentToWrapper.get(component);
-               if (Caption.isNeeded(uidl)) {
-                       if (wrapper == null) {
-                               int index = getWidgetIndex(component);
-                               remove(component);
-                               wrapper = new CaptionWrapper(component);
-                               insert(wrapper, index);
-                               componentToWrapper.put(component, wrapper);
-                       }
-                       wrapper.updateCaption(uidl);
-               } else {
-                       if (wrapper != null) { 
-                               int index = getWidgetIndex(wrapper);
-                               remove(wrapper);
-                               insert(wrapper.getWidget(), index);
-                               componentToWrapper.remove(component);
-                       }
-               }
-       }
-       
-}
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IOrderedLayout.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IOrderedLayout.java
new file mode 100644 (file)
index 0000000..720b75d
--- /dev/null
@@ -0,0 +1,270 @@
+package com.itmill.toolkit.terminal.gwt.client.ui;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Vector;
+
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Element;
+import com.google.gwt.user.client.ui.ComplexPanel;
+import com.google.gwt.user.client.ui.FlowPanel;
+import com.google.gwt.user.client.ui.VerticalPanel;
+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.Layout;
+import com.itmill.toolkit.terminal.gwt.client.Paintable;
+import com.itmill.toolkit.terminal.gwt.client.UIDL;
+
+public class IOrderedLayout extends ComplexPanel implements Paintable, Layout {
+       
+       public static final String CLASSNAME = "i-orderedlayout";
+
+       public static final int ORIENTATION_VERTICAL = 0;
+       public static final int ORIENTATION_HORIZONTAL = 1;
+       
+       int orientationMode = ORIENTATION_VERTICAL;
+
+       private HashMap componentToCaption = new HashMap();
+
+       private ApplicationConnection client;
+       private Element childContainer;
+       
+       public IOrderedLayout() {
+               orientationMode = ORIENTATION_VERTICAL;
+               constructDOM();
+               setStyleName(CLASSNAME);
+       }
+
+       public IOrderedLayout(int orientation) {
+               orientationMode = orientation;
+               constructDOM();
+       }
+       private void constructDOM() {
+               switch (orientationMode) {
+               case ORIENTATION_HORIZONTAL:
+                       Element table = DOM.createTable();
+                       Element tBody = DOM.createTBody();
+                       childContainer = DOM.createTR();
+                       DOM.appendChild(table, tBody);
+                       DOM.appendChild(tBody, childContainer);
+                       setElement(table);
+                       break;
+               default:
+                       childContainer = DOM.createDiv();
+                       setElement(childContainer);
+                       break;
+               }
+       }
+
+       
+       public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
+               
+               this.client = client;
+               
+               // Ensure correct implementation
+               if (client.updateComponent(this, uidl, false))
+                       return;
+
+               ArrayList uidlWidgets = new ArrayList();
+               for (Iterator it = uidl.getChildIterator(); it.hasNext();) {
+                       UIDL uidlForChild = (UIDL) it.next();
+                       Widget child = client.getWidget(uidlForChild);
+                       uidlWidgets.add(child);
+               }
+               
+               Vector oldWidgets = getPaintables();
+               
+               Iterator oldIt = oldWidgets.iterator();
+               Iterator newIt = uidlWidgets.iterator();
+               Iterator newUidl = uidl.getChildIterator();
+               
+               Widget oldChild = null;
+               while(newIt.hasNext()) {
+                       Widget child = (Widget) newIt.next();
+                       UIDL childUidl = (UIDL) newUidl.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();
+                                       // now oldChild is an instance of Paintable
+                                       if(uidlWidgets.contains(oldChild))
+                                               break;
+                                       else {
+                                               removePaintable((Paintable) oldChild);
+                                               oldChild = null;
+                                       }
+                               }
+                       }
+                       if(oldChild == null) {
+                               // we are adding components to layout
+                               add(child);
+                       } else if(child == oldChild) {
+                               // child already attached and updated
+                               oldChild = null;
+                       } else if(hasChildComponent(child)) {
+                               // current child has been moved, re-insert before current oldChild
+                               removeCaption(child);
+                               int index = getWidgetIndex(oldChild);
+                               if(componentToCaption.containsKey(oldChild))
+                                       index--;
+                               remove(child);
+                               this.insert(child, index);
+                       }
+                       ((Paintable)child).updateFromUIDL(childUidl, client);
+
+               }
+               // remove possibly remaining old Paintable object which were not updated 
+               while(oldIt.hasNext()) {
+                       oldChild = (Widget) oldIt.next();
+                       Paintable p = (Paintable) oldChild;
+                       if(!uidlWidgets.contains(p))
+                               removePaintable(p);
+               }
+       }
+       
+       private Vector getPaintables() {
+               Vector al = new Vector();
+               Iterator it = iterator();
+               while (it.hasNext()) {
+                       Widget w = (Widget) it.next();
+                       if (w instanceof Paintable)
+                               al.add(w);
+               }
+               return al;
+       }
+       
+       /**
+        * Removes Paintable from DOM and its reference from ApplicationConnection
+        */
+       public boolean removePaintable(Paintable p) {
+               Caption c = (Caption) componentToCaption.get(p);
+               if(c != null) {
+                       componentToCaption.remove(c);
+                       remove(c);
+               }
+               client.unregisterPaintable(p);
+               return remove((Widget) p);
+       }
+
+       /* (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) {
+               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) {
+                       remove(index);
+                       insert(to, index);
+               }
+       }
+
+       private void insert(Widget w, int beforeIndex) {
+               Element container;
+               if (w instanceof Caption) {
+                       // captions go into same container element as their
+                       // owners
+                       container = DOM.getParent(getWidget(beforeIndex).getElement());
+                       DOM.insertChild(container, w.getElement(), 0);
+                       insert(w, null, beforeIndex);
+               } else {
+                       container = createWidgetWrappper();
+                       DOM.insertChild(getChildContainer(), container, beforeIndex);
+                       insert(w, container, beforeIndex);
+               }
+       }
+       
+       /**
+        * @return Element 
+        *                              where widgets (and their wrappers) are contained 
+        */
+       private Element getChildContainer() {
+               return childContainer;
+       }
+       
+       /**
+        * creates an Element which will contain child widget
+        */
+       private Element createWidgetWrappper() {
+               switch (orientationMode) {
+               case ORIENTATION_HORIZONTAL:
+                       return DOM.createTD();
+               default:
+                       return DOM.createDiv();
+               }
+       }
+
+       public boolean hasChildComponent(Widget component) {
+               return getWidgetIndex(component) >= 0;
+       }
+
+       public void updateCaption(Widget component, UIDL uidl) {
+               
+               Caption c  = (Caption) componentToCaption.get(component);
+               
+               if (Caption.isNeeded(uidl)) {
+                       if (c == null) {
+                               int index = getWidgetIndex(component);
+                               c = new Caption(component);
+                               insert(c, index);
+                               componentToCaption.put(component, c);
+                       }
+                       c.updateCaption(uidl);
+               } else {
+                       if (c != null) { 
+                               remove(c);
+                               componentToCaption.remove(component);
+                       }
+               }
+       }
+       
+       public void removeCaption(Widget w) {
+               Caption c = (Caption) componentToCaption.get(w);
+               if(c != null) {
+                       this.remove(c);
+                       componentToCaption.remove(w);
+               }
+       }
+
+       public void add(Widget w) {
+               Element wrapper = createWidgetWrappper();
+               DOM.appendChild(getChildContainer(), wrapper);
+               super.add(w,wrapper);
+       }
+
+       public boolean remove(int index) {
+           return remove(getWidget(index));
+       }
+       
+       public boolean remove(Widget w) {
+               Element wrapper = DOM.getParent(w.getElement());
+               boolean removed = super.remove(w);
+               if(removed) {
+                       if (! (w instanceof Caption)) {
+                               DOM.removeChild(getChildContainer(), wrapper);
+                       }
+                       return true;
+               }
+               return false;
+       }
+
+       public Widget getWidget(int index) {
+               return getChildren().get(index);
+       }
+
+       public int getWidgetCount() {
+               return getChildren().size();
+       }
+
+       public int getWidgetIndex(Widget child) {
+               return getChildren().indexOf(child);
+       }
+
+}
index a28255cc66064aecff2dbe82448d1bc483b98cef..c1d97ed2b282b40efcad07c38d117116fbb45609 100644 (file)
@@ -36,7 +36,7 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
         * fraction of pageLenght which can be scrolled without 
         * making new request 
         */
-       private static final double CACHE_REACT_RATE = 1;
+       private static final double CACHE_REACT_RATE = 1.5;
        
        public static final char ALIGN_CENTER = 'c';
        public static final char ALIGN_LEFT = 'b';
@@ -1205,7 +1205,7 @@ public class IScrollTable extends Composite implements Paintable, ITable, Scroll
        }
        
        /**
-        * This Panel can only contain IScrollTAbleRow type of 
+        * This Panel can only contain IScrollTableRow type of 
         * widgets. This "simulates" very large table, keeping 
         * spacers which take room of unrendered rows.
         * 
diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IVerticalLayout.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IVerticalLayout.java
deleted file mode 100644 (file)
index ab002df..0000000
+++ /dev/null
@@ -1,160 +0,0 @@
-package com.itmill.toolkit.terminal.gwt.client.ui;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Vector;
-
-import com.google.gwt.user.client.ui.VerticalPanel;
-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.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 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;
-
-               ArrayList uidlWidgets = new ArrayList();
-               for (Iterator it = uidl.getChildIterator(); it.hasNext();) {
-                       UIDL uidlForChild = (UIDL) it.next();
-                       Widget child = client.getWidget(uidlForChild);
-                       uidlWidgets.add(child);
-               }
-               
-               Vector oldWidgets = getPaintables();
-               
-               Iterator oldIt = oldWidgets.iterator();
-               Iterator newIt = uidlWidgets.iterator();
-               Iterator newUidl = uidl.getChildIterator();
-               
-               Widget oldChild = null;
-               while(newIt.hasNext()) {
-                       Widget child = (Widget) newIt.next();
-                       UIDL childUidl = (UIDL) newUidl.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();
-                                       // now oldChild is an instance of Paintable
-                                       if(uidlWidgets.contains(oldChild))
-                                               break;
-                                       else {
-                                               removePaintable((Paintable) oldChild);
-                                               oldChild = null;
-                                       }
-                               }
-                       }
-                       if(oldChild == null) {
-                               // we are adding components to layout
-                               add(child);
-                       } else if(child == oldChild) {
-                               // child already attached and updated
-                               oldChild = null;
-                       } else if(hasChildComponent(child)) {
-                               // current child has been moved, re-insert before current oldChild
-                               removeCaption(child);
-                               int index = getWidgetIndex(oldChild);
-                               if(componentToCaption.containsKey(oldChild))
-                                       index--;
-                               this.insert(child, index);
-                       }
-                       ((Paintable)child).updateFromUIDL(childUidl, client);
-
-               }
-               // remove possibly remaining old Paintable object which were not updated 
-               while(oldIt.hasNext()) {
-                       oldChild = (Widget) oldIt.next();
-                       Paintable p = (Paintable) oldChild;
-                       if(!uidlWidgets.contains(p))
-                               removePaintable(p);
-               }
-       }
-       
-       private Vector getPaintables() {
-               Vector al = new Vector();
-               Iterator it = iterator();
-               while (it.hasNext()) {
-                       Widget w = (Widget) it.next();
-                       if (w instanceof Paintable)
-                               al.add(w);
-               }
-               return al;
-       }
-       
-       /**
-        * Removes Paintable from DOM and its reference from ApplicationConnection
-        */
-       public boolean removePaintable(Paintable p) {
-               Caption c = (Caption) componentToCaption.get(p);
-               if(c != null) {
-                       componentToCaption.remove(c);
-                       remove(c);
-               }
-               client.unregisterPaintable(p);
-               return remove((Widget) p);
-       }
-
-       /* (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) {
-               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) {
-                       remove(index);
-                       insert(to, index);
-               }
-       }
-
-       public boolean hasChildComponent(Widget component) {
-               return getWidgetIndex(component) >= 0;
-       }
-
-       public void updateCaption(Widget component, UIDL uidl) {
-               
-               Caption c  = (Caption) componentToCaption.get(component);
-               
-               if (Caption.isNeeded(uidl)) {
-                       if (c == null) {
-                               int index = getWidgetIndex(component);
-                               c = new Caption();
-                               insert(c, index);
-                               componentToCaption.put(component, c);
-                       }
-                       c.updateCaption(uidl);
-               } else {
-                       if (c != null) { 
-                               remove(c);
-                               componentToCaption.remove(component);
-                       }
-               }
-       }
-       
-       public void removeCaption(Widget w) {
-               Caption c = (Caption) componentToCaption.get(w);
-               if(c != null) {
-                       this.remove(c);
-                       componentToCaption.remove(w);
-               }
-       }
-       
-}