1 package org.joshy.html.table; 2 3 import org.joshy.u; 4 import java.util.*; 5 import org.w3c.dom.*; 6 import org.joshy.html.box.*; 7 import java.awt.Rectangle ; 8 import org.joshy.html.*; 9 10 public class Table { 11 List top_cells = new ArrayList(); 12 private int[] column_widths; 14 private CellGrid grid; 15 public Table() { 16 this.grid = new CellGrid(); 17 } 18 19 20 public void addTable(Element elem) throws Exception { 21 NodeList rows = elem.getChildNodes(); 23 boolean first_row = true; 24 int row_count = 0; 25 for(int i=0; i<rows.getLength(); i++) { 26 Node row = rows.item(i); 27 if(row.getNodeName().equals("tr")) { 28 if(first_row) { 29 addFirstRow(row); 30 first_row = false; 31 } else { 32 addRow(row,row_count); 33 } 34 row_count++; 35 } 36 37 } 38 } 39 40 public void addRow(Node row, int y) throws Exception { 41 addRow(row,false, y); 42 } 43 44 public void addFirstRow(Node row) throws Exception { 45 addRow(row,true, 0); 46 } 47 48 public void addRow(Node row, boolean first_row, int y) throws Exception { 49 NodeList cells = row.getChildNodes(); 54 int col_counter = 0; 55 for(int j=0; j<cells.getLength(); j++) { 56 Node cell = cells.item(j); 57 if(cell.getNodeName().equals("td") || cell.getNodeName().equals("th")) { 58 Cell cl = null; 61 if(first_row) { 62 cl = addTopCell(cell,col_counter,y); 63 } else { 65 cl = addCell(cell,col_counter,y); 66 } 68 col_counter += cl.getColumnSpan(); 69 } 70 } 71 first_row = false; 72 } 74 75 76 public void addColumn(Element elem) { 77 } 78 79 public Cell addTopCell(Node node, int x, int y) throws Exception { 81 Cell cl = addCell(node, x, y); 82 top_cells.add(cl); 83 return cl; 84 } 85 86 public Cell addCell(Node node, int x, int y) throws Exception { 87 if(node.getNodeType() != node.ELEMENT_NODE) { 89 throw new Exception ("this isn't an element" + node); 90 } 91 Element cell = (Element)node; 92 Cell cl = new Cell(); 93 cl.node = node; 94 if(cell.hasAttribute("colspan")) { 95 cl.col_span = Integer.parseInt(cell.getAttribute("colspan")); 96 } 97 if(cell.hasAttribute("rowspan")) { 98 cl.row_span = Integer.parseInt(cell.getAttribute("rowspan")); 99 } 100 grid.addCell(x,y, cl.col_span,cl.row_span,cl); 101 return cl; 102 } 103 104 public void calculateWidths(int avail_width, Context c) { 106 109 int total_cols = getTotalColumnCount(); 111 int[] widths = new int[total_cols]; 112 113 int col_count = 0; 115 for(int i=0; i<top_cells.size(); i++) { 116 Cell cell = (Cell)top_cells.get(i); 117 if(c.css.hasProperty((Element)cell.node,"width",false)) { 118 int width = (int)c.css.getFloatProperty((Element)cell.node,"width",false); 119 for(int j=col_count; j<col_count+cell.col_span; j++) { 121 widths[j] = width/cell.col_span; 122 avail_width -= width/cell.col_span; 123 } 124 } 125 col_count += cell.col_span; 126 } 127 130 int unset_cols = 0; 132 for(int i=0; i<widths.length; i++) { 133 if(widths[i] <= 0) { 134 unset_cols++; 135 } 136 } 137 139 140 for(int i=0; i<total_cols; i++) { 141 if(widths[i] == 0) { 143 widths[i] = avail_width/unset_cols; 144 } 145 } 148 column_widths = widths; 149 } 152 153 int[] getWidths() { 154 return column_widths; 155 } 156 157 int getTotalColumnCount() { 158 int total_cols = 0; 159 Iterator it = top_cells.iterator(); 160 while(it.hasNext()) { 161 Cell cell = (Cell)it.next(); 162 total_cols += cell.col_span; 163 } 164 return total_cols; 165 } 166 171 public CellGrid getCellGrid() { 172 return grid; 173 } 174 175 public int calcColumnX(int col) { 176 int x = 0; 177 for(int i=0; i<col; i++) { 178 x += column_widths[i]; 179 } 180 return x; 181 } 182 183 public int calcColumnWidth(int col, int span) { 184 int x = 0; 186 for(int i=col; i<col+span; i++) { 187 x += column_widths[i]; 188 } 189 return x; 190 } 191 } 192 193 | Popular Tags |