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.MyDOM;
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>TableLayout</code> allows you to easily render content into an HTML
020: * table. The total number of columns can be specified.
021: * <p>
022: * Rather than explicitly creating and nesting rows and columns as you would in
023: * HTML, you simply specify the total column count and start adding widgets in
024: * their natural order from left to right, top to bottom. The layout will
025: * automatically figure out, based on the column count how to position each
026: * panel within the table.
027: * </p>
028: */
029: public class TableLayout extends Layout {
030:
031: int columns = 1;
032: int cellPadding = 0;
033: int cellSpacing = 0;
034: int border = 0;
035:
036: private Element table, tbody;
037: private int currentColumn;
038: private int currentRow;
039:
040: /**
041: * Creates a new table layout.
042: */
043: public TableLayout() {
044:
045: }
046:
047: /**
048: * Creates a new table layout.
049: *
050: * @param columns the number of columns
051: */
052: public TableLayout(int columns) {
053: this .columns = columns;
054: }
055:
056: /**
057: * Returns the border.
058: *
059: * @return the border
060: */
061: public int getBorder() {
062: return border;
063: }
064:
065: /**
066: * Returns the table's cell padding.
067: *
068: * @return the cell padding
069: */
070: public int getCellPadding() {
071: return cellPadding;
072: }
073:
074: /**
075: * Returns the table's cell spacing.
076: *
077: * @return the cell spacing
078: */
079: public int getCellSpacing() {
080: return cellSpacing;
081: }
082:
083: /**
084: * Returns the number of columns.
085: *
086: * @return the number of columns
087: */
088: public int getColumns() {
089: return columns;
090: }
091:
092: /**
093: * Sets the table's border property. Default value is 0.
094: *
095: * @param border the border in pixels
096: */
097: public void setBorder(int border) {
098: this .border = border;
099: }
100:
101: /**
102: * Sets the table's cellpadding property. Default value is 0.
103: *
104: * @param cellPadding the cell padding
105: */
106: public void setCellPadding(int cellPadding) {
107: this .cellPadding = cellPadding;
108: }
109:
110: /**
111: * @param cellSpacing
112: */
113: public void setCellSpacing(int cellSpacing) {
114: this .cellSpacing = cellSpacing;
115: }
116:
117: /**
118: * Sets the number of columns. Default value is 1.
119: *
120: * @param columns the number of columns
121: */
122: public void setColumns(int columns) {
123: this .columns = columns;
124: }
125:
126: protected Element getNextCell(Widget widget) {
127: TableLayoutData data = (TableLayoutData) container
128: .getLayoutData(widget);
129: if (data == null) {
130: data = new TableLayoutData();
131: container.setLayoutData(widget, data);
132: }
133:
134: Element td = DOM.createTD();
135: Element row;
136:
137: if (currentColumn != 0 && (currentColumn % columns == 0)) {
138: row = getRow(++currentRow);
139: currentColumn += data.colspan != -1 ? data.colspan : 1;
140: } else {
141: row = getRow(currentRow);
142: currentColumn += data.colspan != -1 ? data.colspan : 1;
143: }
144: if (data.colspan != 1) {
145: DOM.setElementPropertyInt(td, "colSpan", data.colspan);
146: }
147:
148: if (data.padding > 0) {
149: DOM.setIntStyleAttribute(td, "padding", data.padding);
150: }
151: if (data.style != null) {
152: MyDOM.setStyleName(td, data.style);
153: }
154: if (data.align != null) {
155: DOM.setElementProperty(td, "align", data.align);
156: }
157: if (data.valign != null) {
158: DOM.setStyleAttribute(td, "verticalAlign", data.valign);
159: }
160: if (data.height != null) {
161: DOM.setElementProperty(td, "height", data.height);
162: }
163: if (data.width != null) {
164: DOM.setElementProperty(td, "width", data.width);
165: }
166: DOM.appendChild(row, td);
167: return td;
168: }
169:
170: protected Element getRow(int index) {
171: Element row = DOM.getChild(tbody, index);
172: if (row == null) {
173: row = DOM.createTR();
174: DOM.appendChild(tbody, row);
175: }
176: return row;
177: }
178:
179: protected boolean isValidParent(Element elem, Element parent) {
180: return false;
181: }
182:
183: protected void onLayout(WidgetContainer container, Element target) {
184: currentColumn = 0;
185: currentRow = 0;
186:
187: MyDOM.removeChildren(target);
188: table = DOM.createTable();
189: DOM.setElementPropertyInt(table, "cellPadding", cellPadding);
190: DOM.setElementPropertyInt(table, "cellSpacing", cellSpacing);
191:
192: if (border > 0) {
193: DOM.setElementPropertyInt(table, "border", border);
194: }
195:
196: tbody = DOM.createTBody();
197: DOM.appendChild(table, tbody);
198: DOM.appendChild(target, table);
199:
200: renderAll(container, target);
201: }
202:
203: protected void renderWidget(Widget widget, int index, Element target) {
204: DOM.appendChild(getNextCell(widget), widget.getElement());
205: }
206:
207: }
|