KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > table > Table


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 JavaDoc;
8 import org.joshy.html.*;
9
10 public class Table {
11     List top_cells = new ArrayList();
12     //List rows = new ArrayList();
13
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 JavaDoc {
21         // for each tr
22
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 JavaDoc {
41         addRow(row,false, y);
42     }
43     
44     public void addFirstRow(Node row) throws Exception JavaDoc {
45         addRow(row,true, 0);
46     }
47     
48     public void addRow(Node row, boolean first_row, int y) throws Exception JavaDoc {
49         // for each td
50
//Row rw = new Row();
51
//rw.node = row;
52
//u.p("Table.addRow("+row+","+first_row+","+y+")");
53
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                 //u.p("adding: " + col_counter + " " + y);
59
// add the cell
60
Cell cl = null;
61                 if(first_row) {
62                     cl = addTopCell(cell,col_counter,y);
63                     //rw.addCell(cl);
64
} else {
65                     cl = addCell(cell,col_counter,y);
66                     //rw.addCell(cl);
67
}
68                 col_counter += cl.getColumnSpan();
69             }
70         }
71         first_row = false;
72         //rows.add(rw);
73
}
74
75     
76     public void addColumn(Element elem) {
77     }
78     
79     // add cells from the first row
80
public Cell addTopCell(Node node, int x, int y) throws Exception JavaDoc {
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 JavaDoc {
87         //u.p("addCell("+node+","+x+","+y+")");
88
if(node.getNodeType() != node.ELEMENT_NODE) {
89             throw new Exception JavaDoc("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     // calculate the widths
105
public void calculateWidths(int avail_width, Context c) {
106         //u.p("calculating columns from total space of: " + avail_width);
107
//u.p("total column width = " + total_cols);
108

109         // get number of columns and init array
110
int total_cols = getTotalColumnCount();
111         int[] widths = new int[total_cols];
112         
113         // loop over top cells looking for explict widths
114
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                 //u.p("got width: " + width);
120
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         //u.p("widths");
128
//u.p(widths);
129

130         // get number of unset columns
131
int unset_cols = 0;
132         for(int i=0; i<widths.length; i++) {
133             if(widths[i] <= 0) {
134                 unset_cols++;
135             }
136         }
137         //u.p("unset cols count = " + unset_cols);
138

139         
140         for(int i=0; i<total_cols; i++) {
141             //Cell cell = (Cell)top_cells.get(i);
142
if(widths[i] == 0) {
143                 widths[i] = avail_width/unset_cols;
144             }
145             //u.p("looking at: " + cell);
146
//u.p("set width to: " + widths[i]);
147
}
148         column_widths = widths;
149         //u.p("final widths");
150
//u.p(column_widths);
151
}
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     /*
167     public Iterator getRowIterator() {
168         return rows.iterator();
169     }
170     */

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         //u.p("calc column width: " + col + " " + span);
185
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