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.layout;
009:
010: import net.mygwt.ui.client.widget.Component;
011: import net.mygwt.ui.client.widget.Layout;
012: import net.mygwt.ui.client.widget.WidgetContainer;
013:
014: import com.google.gwt.user.client.DOM;
015: import com.google.gwt.user.client.Element;
016: import com.google.gwt.user.client.ui.Widget;
017:
018: /**
019: * <code>Layout</code> that positions its children using normal HTML layout
020: * behavior. The margin and spacing can be specified. The layout method will be
021: * called to any child widgets that are containers. Recalculate will be called
022: * on any child widgets that are components.
023: */
024: public class FlowLayout extends Layout {
025:
026: private int margin = 0;
027: private int spacing = 0;
028:
029: /**
030: * Creates a new layout instance.
031: */
032: public FlowLayout() {
033:
034: }
035:
036: /**
037: * Creates a new layout instance with the given margin.
038: *
039: * @param margin the margin
040: */
041: public FlowLayout(int margin) {
042: this .margin = margin;
043: }
044:
045: /**
046: * Returns the margin.
047: *
048: * @return the margin
049: */
050: public int getMargin() {
051: return margin;
052: }
053:
054: /**
055: * Returns the spacing value.
056: *
057: * @return the spacing in pixels
058: */
059: public int getSpacing() {
060: return spacing;
061: }
062:
063: /**
064: * Sets the number of pixels of margin that will be placed along the edges of
065: * the layout. The default value is 0.
066: *
067: * @param margin the margin
068: */
069: public void setMargin(int margin) {
070: this .margin = margin;
071: }
072:
073: /**
074: * Sets the amount of space between widgets.
075: *
076: * @param spacing the spacing value
077: */
078: public void setSpacing(int spacing) {
079: this .spacing = spacing;
080: }
081:
082: protected void onLayout(WidgetContainer container, Element target) {
083: super .onLayout(container, target);
084: if (margin != 0) {
085: DOM.setIntStyleAttribute(target, "margin", margin);
086: }
087: }
088:
089: protected void renderWidget(Widget widget, int index, Element target) {
090: super .renderWidget(widget, index, target);
091: DOM
092: .setStyleAttribute(widget.getElement(), "position",
093: "static");
094:
095: if (index != 0 && spacing > 0) {
096: DOM.setIntStyleAttribute(widget.getElement(), "marginTop",
097: spacing);
098: DOM.setIntStyleAttribute(widget.getElement(),
099: "marginRight", spacing);
100: }
101:
102: if (widget instanceof WidgetContainer) {
103: ((WidgetContainer) widget).layout();
104: } else if (widget instanceof Component) {
105: ((Component) widget).recalculate();
106: }
107: }
108:
109: }
|