001: /*
002: * MyGWT Widget Library
003: * Copyright(c) 2007, MyGWT.
004: * licensing@mygwt.net
005: *
006: * http://mygwt.net/license
007: */
008: package net.mygwt.ui.client.widget;
009:
010: import net.mygwt.ui.client.MyDOM;
011:
012: import com.google.gwt.user.client.DOM;
013: import com.google.gwt.user.client.Element;
014: import com.google.gwt.user.client.ui.Widget;
015: import com.google.gwt.user.client.ui.WidgetHelper;
016:
017: /**
018: * A layout controls the position and size of the children of a
019: * <code>Container</code>.
020: *
021: * @see WidgetContainer#setLayout(Layout)
022: */
023: public abstract class Layout {
024:
025: protected WidgetContainer container;
026:
027: /**
028: * Lays out the children of the specified container according to this layout.
029: * <p>
030: * This method positions and sizes the children of a container using the
031: * layout algorithm encoded by this layout. The position of the container is
032: * not altered by this method.
033: * </p>
034: *
035: * @param container a container panel using this layout
036: */
037: public void layout(WidgetContainer container) {
038: this .container = container;
039: Element target = container.getLayoutTarget();
040:
041: // physical attach
042: onLayout(container, target);
043:
044: // set parents & attach
045: int count = container.getWidgetCount();
046: for (int i = 0; i < count; i++) {
047: Widget w = container.getWidget(i);
048: if (w.getParent() != container) {
049: w.removeFromParent();
050: WidgetHelper.setParent(w, container);
051: }
052: if (container.isAttached() && !w.isAttached()) {
053: WidgetHelper.doAttach(w);
054: }
055: }
056: }
057:
058: protected boolean isValidParent(Element elem, Element parent) {
059: if (DOM.compare(DOM.getParent(elem), parent)) {
060: return true;
061: }
062: return false;
063: }
064:
065: protected void onLayout(WidgetContainer container, Element target) {
066: renderAll(container, target);
067: }
068:
069: protected void renderAll(Container container, Element target) {
070: int count = container.getWidgetCount();
071: for (int i = 0; i < count; i++) {
072: Widget w = (Widget) container.getWidget(i);
073:
074: if (!isValidParent(w.getElement(), target)) {
075: renderWidget(w, i, target);
076: }
077:
078: }
079: }
080:
081: protected void renderWidget(Widget widget, int index, Element target) {
082: DOM.insertChild(target, widget.getElement(), index);
083: }
084:
085: protected void setBounds(Widget w, int x, int y, int width,
086: int height) {
087: if (w instanceof Component) {
088: ((Component) w).setBounds(x, y, width, height);
089: } else {
090: MyDOM.setBounds(w.getElement(), x, y, width, height, true);
091: }
092: }
093:
094: protected void setSize(Widget w, int width, int height) {
095: if (w instanceof Component) {
096: ((Component) w).setSize(width, height);
097: } else {
098: MyDOM.setSize(w.getElement(), width, height, true);
099: }
100: }
101:
102: }
|