1 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 33 public class Grid extends HTMLTable { 34 35 42 private static native void addRows(Element table, int rows, int columns) ; 55 56 59 protected int numColumns; 60 61 64 protected int numRows; 65 66 69 public Grid() { 70 super(); 71 setCellFormatter(new CellFormatter()); 72 setRowFormatter(new RowFormatter()); 73 setColumnFormatter(new ColumnFormatter()); 74 } 75 76 83 public Grid(int rows, int columns) { 84 this(); 85 resize(rows, columns); 86 } 87 88 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, " "); 99 return b; 100 } 101 102 106 public int getCellCount(int row) { 107 return numColumns; 108 } 109 110 115 public int getColumnCount() { 116 return numColumns; 117 } 118 119 122 public int getRowCount() { 123 return numRows; 124 } 125 126 133 public void resize(int rows, int columns) { 134 resizeColumns(columns); 135 resizeRows(rows); 136 } 137 138 144 public void resizeColumns(int columns) { 145 if (numColumns == columns) { 146 return; 147 } 148 if (columns < 0) { 149 throw new IndexOutOfBoundsException ("Cannot set number of columns to " 150 + columns); 151 } 152 153 if (numColumns > columns) { 154 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 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 177 public void resizeRows(int rows) { 178 if (numRows == rows) { 179 return; 180 } 181 if (rows < 0) { 182 throw new IndexOutOfBoundsException ("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 removeRow(--numRows); 192 } 193 } 194 } 195 196 199 protected Element createCell() { 200 Element td = super.createCell(); 201 202 DOM.setInnerHTML(td, " "); 205 return td; 206 } 207 208 215 protected void prepareCell(int row, int column) { 216 prepareRow(row); 218 if (column < 0) { 219 throw new IndexOutOfBoundsException ( 220 "Cannot access a column with a negative index: " + column); 221 } 222 223 if (column >= numColumns) { 224 throw new IndexOutOfBoundsException ("Column index: " + column 225 + ", Column size: " + numColumns); 226 } 227 } 228 229 235 protected void prepareColumn(int column) { 236 if (column < 0) { 238 throw new IndexOutOfBoundsException ( 239 "Cannot access a column with a negative index: " + column); 240 } 241 242 246 if (column >= numColumns) { 247 throw new IndexOutOfBoundsException ("Column index: " + column 248 + ", Column size: " + numColumns); 249 } 250 } 251 252 258 protected void prepareRow(int row) { 259 if (row < 0) { 261 throw new IndexOutOfBoundsException ( 262 "Cannot access a row with a negative index: " + row); 263 } 264 265 269 if (row >= numRows) { 270 throw new IndexOutOfBoundsException ("Row index: " + row + ", Row size: " 271 + numRows); 272 } 273 } 274 } 275 | Popular Tags |