From: Matti Tahvonen Date: Wed, 8 Aug 2007 09:44:52 +0000 (+0000) Subject: combined horizontal and vertical layouts X-Git-Tag: 6.7.0.beta1~6134 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=08748db0f3f9a8ac85b92b3352843d04bea07e80;p=vaadin-framework.git combined horizontal and vertical layouts svn changeset:1965/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/terminal/gwt/client/Caption.java b/src/com/itmill/toolkit/terminal/gwt/client/Caption.java index e60ea59566..06fca38534 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/Caption.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/Caption.java @@ -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"); } diff --git a/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java b/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java index 8ebffa6da0..907b8c4a47 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/DefaultWidgetFactory.java @@ -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 index 17935d5862..0000000000 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/IHorizontalLayout.java +++ /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 index 0000000000..720b75d803 --- /dev/null +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/IOrderedLayout.java @@ -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); + } + +} diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java index a28255cc66..c1d97ed2b2 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/IScrollTable.java @@ -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 index ab002dfe28..0000000000 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/IVerticalLayout.java +++ /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); - } - } - -}