KickJava   Java API By Example, From Geeks To Geeks.

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


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 rectangular grid that can contain text, html, or a child
23  * {@link com.google.gwt.user.client.ui.Widget} within its cells. It must be
24  * resized explicitly to the desired number of rows and columns.
25  * <p>
26  * <img class='gallery' SRC='Table.png'/>
27  * </p>
28  * <p>
29  * <h3>Example</h3>
30  * {@example com.google.gwt.examples.GridExample}
31  * </p>
32  */

33 public class Grid extends HTMLTable {
34
35   /**
36    * Native method to add rows into a table with a given number of columns.
37    *
38    * @param table the table element
39    * @param rows number of rows to add
40    * @param columns the number of columns per row
41    */

42   private static native void addRows(Element table, int rows, int columns) /*-{
43      var td = $doc.createElement("td");
44      td.innerHTML = "&nbsp;";
45      var row = $doc.createElement("tr");
46      for(var cellNum = 0; cellNum < columns; cellNum++) {
47        var cell = td.cloneNode(true);
48        row.appendChild(cell);
49      }
50      table.appendChild(row);
51      for(var rowNum = 1; rowNum < rows; rowNum++) {
52        table.appendChild(row.cloneNode(true));
53      }
54    }-*/
;
55
56   /**
57    * Number of columns in the current grid.
58    */

59   protected int numColumns;
60
61   /**
62    * Number of rows in the current grid.
63    */

64   protected int numRows;
65
66   /**
67    * Constructor for <code>Grid</code>.
68    */

69   public Grid() {
70     super();
71     setCellFormatter(new CellFormatter());
72     setRowFormatter(new RowFormatter());
73     setColumnFormatter(new ColumnFormatter());
74   }
75
76   /**
77    * Constructs a grid with the requested size.
78    *
79    * @param rows the number of rows
80    * @param columns the number of columns
81    * @throws IndexOutOfBoundsException
82    */

83   public Grid(int rows, int columns) {
84     this();
85     resize(rows, columns);
86   }
87
88   /**
89    * Replaces the contents of the specified cell with a single space.
90    *
91    * @param row the cell's row
92    * @param column the cell's column
93    * @throws IndexOutOfBoundsException
94    */

95   public boolean clearCell(int row, int column) {
96     Element td = getCellFormatter().getElement(row, column);
97     boolean b = super.internalClearCell(td, false);
98     DOM.setInnerHTML(td, "&nbsp;");
99     return b;
100   }
101
102   /**
103    * Return number of columns. For grid, row argument is ignored as all grids
104    * are rectangular.
105    */

106   public int getCellCount(int row) {
107     return numColumns;
108   }
109
110   /**
111    * Gets the number of columns in this grid.
112    *
113    * @return the number of columns
114    */

115   public int getColumnCount() {
116     return numColumns;
117   }
118
119   /**
120    * Return number of rows.
121    */

122   public int getRowCount() {
123     return numRows;
124   }
125
126   /**
127    * Resizes the grid.
128    *
129    * @param rows the number of rows
130    * @param columns the number of columns
131    * @throws IndexOutOfBoundsException
132    */

133   public void resize(int rows, int columns) {
134     resizeColumns(columns);
135     resizeRows(rows);
136   }
137
138   /**
139    * Resizes the grid to the specified number of columns.
140    *
141    * @param columns the number of columns
142    * @throws IndexOutOfBoundsException
143    */

144   public void resizeColumns(int columns) {
145     if (numColumns == columns) {
146       return;
147     }
148     if (columns < 0) {
149       throw new IndexOutOfBoundsException JavaDoc("Cannot set number of columns to "
150           + columns);
151     }
152
153     if (numColumns > columns) {
154       // Fewer columns. Remove extraneous cells.
155
for (int i = 0; i < numRows; i++) {
156         for (int j = numColumns - 1; j >= columns; j--) {
157           removeCell(i, j);
158         }
159       }
160     } else {
161       // More columns. add cells where necessary.
162
for (int i = 0; i < numRows; i++) {
163         for (int j = numColumns; j < columns; j++) {
164           insertCell(i, j);
165         }
166       }
167     }
168     numColumns = columns;
169   }
170
171   /**
172    * Resizes the grid to the specified number of rows.
173    *
174    * @param rows the number of rows
175    * @throws IndexOutOfBoundsException
176    */

177   public void resizeRows(int rows) {
178     if (numRows == rows) {
179       return;
180     }
181     if (rows < 0) {
182       throw new IndexOutOfBoundsException JavaDoc("Cannot set number of rows to "
183           + rows);
184     }
185     if (numRows < rows) {
186       addRows(getBodyElement(), rows - numRows, numColumns);
187       numRows = rows;
188     } else {
189       while (numRows > rows) {
190         // Fewer rows. Remove extraneous ones.
191
removeRow(--numRows);
192       }
193     }
194   }
195
196   /**
197    * Creates a new, empty cell.
198    */

199   protected Element createCell() {
200     Element td = super.createCell();
201
202     // Add a non-breaking space to the TD. This ensures that the cell is
203
// displayed.
204
DOM.setInnerHTML(td, "&nbsp;");
205     return td;
206   }
207
208   /**
209    * Checks that a cell is a valid cell in the table.
210    *
211    * @param row the cell's row
212    * @param column the cell's column
213    * @throws IndexOutOfBoundsException
214    */

215   protected void prepareCell(int row, int column) {
216     // Ensure that the indices are not negative.
217
prepareRow(row);
218     if (column < 0) {
219       throw new IndexOutOfBoundsException JavaDoc(
220           "Cannot access a column with a negative index: " + column);
221     }
222
223     if (column >= numColumns) {
224       throw new IndexOutOfBoundsException JavaDoc("Column index: " + column
225           + ", Column size: " + numColumns);
226     }
227   }
228
229   /**
230    * Checks that the column index is valid.
231    *
232    * @param column The column index to be checked
233    * @throws IndexOutOfBoundsException if the column is negative
234    */

235   protected void prepareColumn(int column) {
236     // Ensure that the indices are not negative.
237
if (column < 0) {
238       throw new IndexOutOfBoundsException JavaDoc(
239           "Cannot access a column with a negative index: " + column);
240     }
241
242     /**
243      * Grid does not lazily create cells, so simply ensure that the requested
244      * column and column are valid
245      */

246     if (column >= numColumns) {
247       throw new IndexOutOfBoundsException JavaDoc("Column index: " + column
248           + ", Column size: " + numColumns);
249     }
250   }
251
252   /**
253    * Checks that the row index is valid.
254    *
255    * @param row The row index to be checked
256    * @throws IndexOutOfBoundsException if the row is negative
257    */

258   protected void prepareRow(int row) {
259     // Ensure that the indices are not negative.
260
if (row < 0) {
261       throw new IndexOutOfBoundsException JavaDoc(
262           "Cannot access a row with a negative index: " + row);
263     }
264
265     /**
266      * Grid does not lazily create cells, so simply ensure that the requested
267      * row and column are valid
268      */

269     if (row >= numRows) {
270       throw new IndexOutOfBoundsException JavaDoc("Row index: " + row + ", Row size: "
271           + numRows);
272     }
273   }
274 }
275
Popular Tags