KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > FlexTable


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20
21 /**
22  * A flexible table that creates cells on demand. It can be jagged (that is,
23  * each row can contain a different number of cells) and individual cells can be
24  * set to span multiple rows or columns.
25  * <p>
26  * <img class='gallery' SRC='Table.png'/>
27  * </p>
28  * <p>
29  * <h3>Example</h3>
30  * {@example com.google.gwt.examples.FlexTableExample}
31  * </p>
32  */

33 public class FlexTable extends HTMLTable {
34
35   /**
36    * FlexTable-specific implementation of {@link HTMLTable.CellFormatter}. The
37    * formatter retrieved from {@link HTMLTable#getCellFormatter()} may be cast
38    * to this class.
39    */

40   public class FlexCellFormatter extends CellFormatter {
41
42     /**
43      * Gets the column span for the given cell. This is the number of logical
44      * columns covered by the cell.
45      *
46      * @param row the cell's row
47      * @param column the cell's column
48      * @return the cell's column span
49      * @throws IndexOutOfBoundsException
50      */

51     public int getColSpan(int row, int column) {
52       return DOM.getElementPropertyInt(getElement(row, column), "colSpan");
53     }
54
55     /**
56      * Gets the row span for the given cell. This is the number of logical rows
57      * covered by the cell.
58      *
59      * @param row the cell's row
60      * @param column the cell's column
61      * @return the cell's row span
62      * @throws IndexOutOfBoundsException
63      */

64     public int getRowSpan(int row, int column) {
65       return DOM.getElementPropertyInt(getElement(row, column), "rowSpan");
66     }
67
68     /**
69      * Sets the column span for the given cell. This is the number of logical
70      * columns covered by the cell.
71      *
72      * @param row the cell's row
73      * @param column the cell's column
74      * @param colSpan the cell's column span
75      * @throws IndexOutOfBoundsException
76      */

77     public void setColSpan(int row, int column, int colSpan) {
78       DOM.setElementPropertyInt(ensureElement(row, column), "colSpan", colSpan);
79     }
80
81     /**
82      * Sets the row span for the given cell. This is the number of logical rows
83      * covered by the cell.
84      *
85      * @param row the cell's row
86      * @param column the cell's column
87      * @param rowSpan the cell's row span
88      * @throws IndexOutOfBoundsException
89      */

90     public void setRowSpan(int row, int column, int rowSpan) {
91       DOM.setElementPropertyInt(ensureElement(row, column), "rowSpan", rowSpan);
92     }
93   }
94
95   private static native void addCells(Element table, int row, int num)/*-{
96     var rowElem = table.rows[row];
97     for(var i = 0; i < num; i++){
98       var cell = $doc.createElement("td");
99       rowElem.appendChild(cell);
100     }
101  }-*/
;
102
103   public FlexTable() {
104     super();
105     setCellFormatter(new FlexCellFormatter());
106     setRowFormatter(new RowFormatter());
107     setColumnFormatter(new ColumnFormatter());
108   }
109
110   /**
111    * Appends a cell to the specified row.
112    *
113    * @param row the row to which the new cell will be added
114    * @throws IndexOutOfBoundsException
115    */

116   public void addCell(int row) {
117     insertCell(row, getCellCount(row));
118   }
119
120   /**
121    * Gets the number of cells on a given row.
122    *
123    * @param row the row whose cells are to be counted
124    * @return the number of cells present
125    * @throws IndexOutOfBoundsException
126    */

127   public int getCellCount(int row) {
128     checkRowBounds(row);
129     return super.getDOMCellCount(getBodyElement(), row);
130   }
131
132   /**
133    * Explicitly gets the {@link FlexCellFormatter}. The results of
134    * {@link HTMLTable#getCellFormatter()} may also be downcast to a
135    * {@link FlexCellFormatter}.
136    *
137    * @return the FlexTable's cell formatter
138    */

139   public FlexCellFormatter getFlexCellFormatter() {
140     return (FlexCellFormatter) getCellFormatter();
141   }
142
143   /**
144    * Gets the number of rows.
145    *
146    * @return number of rows
147    */

148   public int getRowCount() {
149     return getDOMRowCount();
150   }
151
152   /**
153    * Inserts a cell into the FlexTable.
154    *
155    * @param beforeRow the cell's row
156    * @param beforeColumn the cell's column
157    */

158   public void insertCell(int beforeRow, int beforeColumn) {
159     super.insertCell(beforeRow, beforeColumn);
160   }
161
162   /**
163    * Inserts a row into the FlexTable.
164    *
165    * @param beforeRow the row to insert
166    */

167   public int insertRow(int beforeRow) {
168     return super.insertRow(beforeRow);
169   }
170
171   /**
172    * @see com.google.gwt.user.client.ui.HTMLTable#removeCell(int, int)
173    */

174   public void removeCell(int row, int col) {
175     super.removeCell(row, col);
176   }
177
178   /**
179    * Removes a number of cells from a row in the table.
180    *
181    * @param row the row of the cells to be removed
182    * @param column the column of the first cell to be removed
183    * @param num the number of cells to be removed
184    * @throws IndexOutOfBoundsException
185    */

186   public void removeCells(int row, int column, int num) {
187     for (int i = 0; i < num; i++) {
188       removeCell(row, column);
189     }
190   }
191
192   public void removeRow(int row) {
193     super.removeRow(row);
194   }
195
196   /**
197    * Ensure that the cell exists.
198    *
199    * @param row the row to prepare.
200    * @param column the column to prepare.
201    * @throws IndexOutOfBoundsException if the row is negative
202    */

203   protected void prepareCell(int row, int column) {
204     prepareRow(row);
205     if (column < 0) {
206       throw new IndexOutOfBoundsException JavaDoc(
207           "Cannot create a column with a negative index: " + column);
208     }
209
210     // Ensure that the requested column exists.
211
int cellCount = getCellCount(row);
212     int required = column + 1 - cellCount;
213     if (required > 0) {
214       addCells(getBodyElement(), row, required);
215     }
216   }
217
218   /**
219    * Ensure that the row exists.
220    *
221    * @param row The row to prepare.
222    * @throws IndexOutOfBoundsException if the row is negative
223    */

224   protected void prepareRow(int row) {
225     if (row < 0) {
226       throw new IndexOutOfBoundsException JavaDoc(
227           "Cannot create a row with a negative index: " + row);
228     }
229
230     // Ensure that the requested row exists.
231
int rowCount = getRowCount();
232     for (int i = rowCount; i <= row; i++) {
233       insertRow(i);
234     }
235   }
236 }
237
Popular Tags