1 package org.joshy.html; 2 3 4 import java.awt.Point ; 9 import java.awt.Rectangle ; 10 11 import org.joshy.html.box.*; 12 import org.joshy.html.util.*; 13 14 import org.joshy.u; 15 import org.joshy.x; 16 17 import org.w3c.dom.Element ; 18 import org.w3c.dom.Node ; 19 import org.w3c.dom.NodeList ; 20 21 22 38 39 47 48 public class TableLayout 49 extends BoxLayout { 50 51 private static final int fudge = 0; 53 public Box createBox(Context c, Node node) { 54 55 Box box = new TableBox(0, 0, 0, 0); 56 box.node = node; 57 58 return box; 59 } 60 61 62 63 64 66 public Box layout(Context c, Element elem) { 67 68 TableBox table = (TableBox)createBox(c, elem); 69 70 getMargin(c, table); 72 getPadding(c, table); 73 getBorder(c, table); 74 float border_spacing = c.css.getFloatProperty(elem, "border-spacing"); 75 table.spacing = new Point ((int)border_spacing, (int)border_spacing); 76 int fixed_width = c.getExtents().width; 79 80 int orig_fixed_width = fixed_width; 82 fixed_width -= table.margin.left + table.border.left + table.padding.left + 83 table.spacing.x + table.padding.right + table.border.right + table.margin.right; 84 85 int col_count = getColumnCount(elem); 87 88 int[] col_widths = new int[col_count]; 90 91 for (int i = 0; i < col_count; i++) { 93 col_widths[i] = -1; 94 } 95 96 int leftover_width = (int)fixed_width - 0; 98 99 leftover_width -= calculateColumnWidths(c,elem,col_widths); 101 102 distributeRemainingColumnWidth(c,col_widths,leftover_width,table); 104 105 table.width = fixed_width; 107 108 layoutTableRows(c, table, elem, col_widths, orig_fixed_width); 109 return table; 110 } 111 112 protected int calculateColumnWidths(Context c, Element elem, int[] col_widths) { 113 int total_width = 0; 114 115 Element tr = x.child(elem, "tr"); 116 NodeList nl = elem.getChildNodes(); 117 118 int count = 0; 120 121 for (int i = 0; i < nl.getLength(); i++) { 122 123 if (nl.item(i).getNodeName().equals("td") || 124 nl.item(i).getNodeName().equals("th")) { 125 126 Element td = (Element )nl.item(i); 127 u.p("got td: " + td + " " + i + " count = " + count); 128 129 if (c.css.hasProperty(td, "width", false)) { 131 132 if (count > col_widths.length) { 134 u.p("elem = "); 135 136 } 138 139 col_widths[count] = (int)c.css.getFloatProperty(td, 140 "width"); 141 142 total_width += col_widths[count]; 143 } 144 145 count++; 146 } 147 } 148 return total_width; 149 } 150 151 public void distributeRemainingColumnWidth(Context c, int[]col_widths, int leftover_width, TableBox table) { 152 153 int unset_count = 0; 155 156 for (int i = 0; i < col_widths.length; i++) { 157 158 if (col_widths[i] == -1) { 159 unset_count++; 160 } 161 } 162 163 if (leftover_width > 0) { 165 166 for (int i = 0; i < col_widths.length; i++) { 168 169 if (col_widths[i] == -1) { 171 col_widths[i] = (leftover_width - 172 table.spacing.x * col_widths.length) / unset_count; 173 } 174 } 175 } 176 } 177 178 protected void layoutTableRows(Context c, TableBox table, Element elem, int[] col_widths, int orig_fixed_width) { 179 180 RowBox prev_row = new RowBox(0, 0, 0, 0); 182 prev_row.y = table.margin.top + table.border.top + 183 table.padding.top - fudge; 184 185 NodeList rows = elem.getChildNodes(); 187 for (int i = 0; i < rows.getLength(); i++) { 188 Node row = rows.item(i); 189 if (row.getNodeName().equals("tr")) { 190 191 prev_row = layoutRow(c,row,prev_row,table,col_widths); 192 } 193 } 194 195 196 table.height = prev_row.y + prev_row.height + table.spacing.y + 197 table.padding.bottom + table.border.bottom + 198 table.margin.bottom; 199 table.width = orig_fixed_width; 200 } 201 202 203 public RowBox layoutRow(Context c, Node row, RowBox prev_row, TableBox table, int[] col_widths) { 204 RowBox rowbox = new RowBox(0, 0, 0, 0); 206 rowbox.node = row; 207 208 CellBox prev_cell = new CellBox(0, 0, 0, 0); 210 211 NodeList cells = row.getChildNodes(); 213 int col_counter = 0; 214 for (int j = 0; j < cells.getLength(); j++) { 215 Node cell = cells.item(j); 216 if (cell.getNodeName().equals("td") || 217 cell.getNodeName().equals("th")) { 218 219 if (col_counter >= col_widths.length) { 221 u.p("WARNING: too many cells on this row"); 222 continue; 223 } 224 225 prev_cell = layoutCell(c, cell, prev_cell, rowbox, table, col_widths[col_counter]); 226 227 col_counter++; 229 230 } 232 } 233 234 rowbox.x = +table.margin.left + table.border.left + 236 table.padding.left; 237 238 rowbox.y = prev_row.y + prev_row.height + table.spacing.y + 240 fudge; 241 242 rowbox.width = table.width; 244 245 for (int k = 0; k < rowbox.cells.size(); k++) { 247 ((CellBox)rowbox.cells.get(k)).height = rowbox.height; 248 } 249 250 table.rows.add(rowbox); 251 252 return rowbox; 254 } 255 256 public CellBox layoutCell(Context c, Node cell, CellBox prev_cell, RowBox rowbox, TableBox table, int cellwidth) { 257 CellBox cellbox = new CellBox(0, 0, cellwidth, 0); 258 259 cellbox.node = cell; 261 getBorder(c, cellbox); 262 getMargin(c, cellbox); 263 getPadding(c, cellbox); 264 265 cellbox.x = prev_cell.x + prev_cell.width + 267 table.spacing.x + fudge; 268 269 cellbox.y = 0; 271 272 cellbox.height = 50; 274 275 Rectangle oe = c.getExtents(); 276 277 c.setExtents(new Rectangle (c.getExtents().x, 279 c.getExtents().y, 280 cellbox.width, 100)); 281 282 Layout layout = LayoutFactory.getLayout(cell); 284 Box cell_contents = layout.layout(c, (Element )cellbox.node); 285 cellbox.sub_box = cell_contents; 286 287 c.setExtents(oe); 289 290 cellbox.height = cell_contents.height; 293 294 rowbox.height = Math.max(cellbox.height, rowbox.height); 297 rowbox.cells.add(cellbox); 298 299 return cellbox; 300 } 301 302 303 304 308 public void paint(Context c, Box box) { 309 310 Rectangle oldBounds = new Rectangle (c.getExtents()); 313 314 paintBackground(c, box); 317 paintComponent(c, box); 318 319 paintBorder(c, box); 321 322 oldBounds.y = oldBounds.y + box.height; 324 c.setExtents(oldBounds); 325 } 326 327 public void paintComponent(Context c, Box box) { 328 paintTable(c, (TableBox)box); 329 } 330 331 public void paintChildren(Context c, Box box) { 332 } 333 334 protected void paintTable(Context c, TableBox table) { 335 c.getGraphics().translate(table.x, table.y); 336 c.getGraphics().translate( 337 table.margin.left + table.border.left + table.padding.left, 338 table.margin.top + table.border.top + table.padding.top); 339 340 for (int i = 0; i < table.rows.size(); i++) { 342 343 RowBox row = (RowBox)table.rows.get(i); 344 345 Rectangle oe = c.getExtents(); 347 348 c.setExtents(new Rectangle (oe.x + row.x, oe.y + row.y, oe.width, 350 oe.height)); 351 c.getGraphics().translate(row.x, row.y); 352 353 paintRow(c, row); 355 356 c.getGraphics().translate(-row.x, -row.y); 358 c.setExtents(oe); 359 } 360 361 c.getGraphics().translate( 362 -table.margin.left - table.border.left - table.padding.left, 363 -table.margin.top - table.border.top - table.padding.top); 364 c.getGraphics().translate(-table.x, -table.y); 365 366 } 368 369 protected void paintRow(Context c, RowBox row) { 370 371 for (int i = 0; i < row.cells.size(); i++) { 375 376 CellBox cell = (CellBox)row.cells.get(i); 377 Rectangle oe = c.getExtents(); 378 c.setExtents(new Rectangle (cell.x, cell.y, oe.width, oe.height)); 379 paintCell(c, cell); 380 c.setExtents(oe); 381 } 382 } 383 384 protected void paintCell(Context c, CellBox cell) { 385 if(cell.isReal()) { 386 Rectangle oe = c.getExtents(); 387 c.getGraphics().translate(oe.x, oe.y); 388 c.setExtents(new Rectangle (0, 0, cell.width, cell.height)); 389 390 Layout layout = LayoutFactory.getLayout(cell.node); 391 u.p("doing cell: " + cell); 392 layout.paint(c, cell.sub_box); 393 394 c.getGraphics().translate(-oe.x, -oe.y); 395 c.setExtents(oe); 396 397 } 398 } 399 400 401 402 403 404 405 406 407 private int getColumnCount(Element tb) { 408 409 int count = 0; 410 NodeList nl = tb.getChildNodes(); 411 412 for (int i = 0; i < nl.getLength(); i++) { 413 414 Node row = nl.item(i); 415 416 if (row.getNodeName().equals("tr")) { 417 418 NodeList cells = row.getChildNodes(); 419 420 for (int j = 0; j < cells.getLength(); j++) { 421 422 Node cell = cells.item(j); 423 424 if (cell.getNodeName().equals("td") || 425 cell.getNodeName().equals("th")) { 426 count++; 427 } 428 } 429 430 return count; 432 } 433 } 434 435 return count; 436 } 437 } 438 | Popular Tags |