KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > TableLayout2


1 /*
2 todo:
3
4 recombine the tablelayout and tablelayout2
5 move more of the calculation code into the table package
6 make a set of tests in addition to the demo
7     no span
8     col span
9     row span
10     col and row span
11     col span contents that are too big
12     row span contents that are too big
13     col and row span contents that are too big
14 implement row height growing based on row spanned contents
15
16 investigate margin collapsing
17 support captions, headers, and footers
18
19
20
21
22
23  - joshy
24 */

25
26 package org.joshy.html;
27
28 import java.awt.Point JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30
31 import java.util.*;
32
33 import org.joshy.html.box.*;
34 import org.joshy.html.table.*;
35
36 import org.joshy.u;
37
38 import org.w3c.dom.*;
39
40
41 public class TableLayout2
42     extends TableLayout {
43     public Box createBox(Context c, Node node) {
44
45         TableBox table = new TableBox();
46         table.node = node;
47
48         // set up the box properties
49
getMargin(c, table);
50         getPadding(c, table);
51         getBorder(c, table);
52
53         return table;
54     }
55
56     public Box layout(Context c, Element elem) {
57
58         try {
59
60             //u.p("\n====\nLayout");
61
// create the table box
62
TableBox table_box = (TableBox)createBox(c, elem);
63
64             // set up the border spacing
65
float border_spacing = c.css.getFloatProperty(elem,
66                                                           "border-spacing");
67             table_box.spacing = new Point JavaDoc((int)border_spacing,
68                                           (int)border_spacing);
69
70             // set up the width
71
int fixed_width = c.getExtents().width;
72
73             if (c.css.hasProperty(elem, "width", false)) {
74                 fixed_width = (int)c.css.getFloatProperty(elem, "width", false);
75             }
76
77             int orig_fixed_width = fixed_width;
78
79             //subtract off the margin, border, and padding
80
fixed_width -= table_box.margin.left + table_box.border.left +
81                 table_box.padding.left + table_box.spacing.x +
82                 table_box.padding.right + table_box.border.right + table_box.margin.right;
83
84             // create the table
85
Table table = new Table();
86             table.addTable(elem);
87
88             //calculate the widths
89
table.calculateWidths(fixed_width, c);
90
91             //pull out the boxes
92
Box bx = calculateBoxes(fixed_width, table_box, c, table);
93             bx.width += table_box.margin.left + table_box.border.left + table_box.padding.left
94                 + table_box.margin.right + table_box.border.right + table_box.padding.right;
95             bx.height += table_box.margin.top + table_box.border.top + table_box.padding.top +
96                 table_box.margin.bottom + table_box.border.bottom + table_box.padding.bottom;
97
98             //bx.width
99
return bx;
100         } catch (Exception JavaDoc ex) {
101             u.p(ex);
102
103             return null;
104         }
105     }
106     
107     
108     public Box calculateBoxes(int avail_width, TableBox box, Context c, Table table) {
109
110         box.width = avail_width;
111         box.height = 100;
112         box.x = 5;
113         box.y = 5;
114         
115         // create a dummy prev row
116
RowBox prev_row = new RowBox(0,0,0,0);
117         int max_width = 0;
118         
119         // loop throw the rows
120
CellGrid grid = table.getCellGrid();
121         for(int y=0; y<grid.getHeight(); y++) {
122             // create a new row box for this row
123
RowBox row_box = new RowBox(0,0,0,0);
124             //row_box.node = row.node;
125
box.rows.add(row_box);
126             
127             int row_height = 0;
128             int column_count = 0;
129             
130             // loop through the cells
131
for(int x=0; x<grid.getWidth(); x++) {
132                 u.p("x = " + x);
133                 //u.p("grid width = " + grid.getWidth());
134
if(grid.isReal(x,y)) {
135                     u.p("it's real");
136                     
137                     Cell cell = grid.getCell(x,y);
138                     
139                     // create a new cell box for this cell
140
CellBox cell_box = new CellBox(0,0,10,10);
141                     cell.cb = cell_box;
142                     cell_box.rb = row_box;
143                     // set the x coord based on the current column
144
cell_box.x = table.calcColumnX(column_count);
145                     // set the width
146
//u.p("column count = " + column_count + " col span = " + cell.col_span);
147
cell_box.width = table.calcColumnWidth(column_count, cell.col_span);
148                     cell_box.node = cell.node;
149                     
150                     // do the internal layout
151
// save the old extents and create new with smaller width
152
Rectangle JavaDoc oe = c.getExtents();
153                         c.setExtents(new Rectangle JavaDoc(c.getExtents().x,c.getExtents().y,
154                             cell_box.width, 100));
155                         // do child layout
156
Layout layout = LayoutFactory.getLayout(cell.node);
157                         Box cell_contents = layout.layout(c,(Element)cell_box.node);
158                         cell_box.sub_box = cell_contents;
159                         cell_box.height = cell_box.sub_box.height;
160                         column_count += cell.col_span;
161                         // restore old extents
162
c.setExtents(oe);
163                     
164                     
165                     // y is relative to the rowbox so it's just 0
166
cell_box.y = 0;
167                     // add the cell to the row
168
row_box.cells.add(cell_box);
169                     
170                                     
171                     // if this is a non row spanning cell then
172
// adjust the row height to fit this cell
173
if(cell.row_span == 1) {
174                         if(cell_box.height > row_box.height) {
175                             row_box.height = cell_box.height;
176                         }
177                     }
178                     row_box.width += cell_box.width;
179                     
180                 } else {
181                     u.p("it's virtual");
182                     Cell cell = grid.getCell(x,y);
183                     // create a virtual cell box for this cell
184
CellBox cell_box = CellBox.createVirtual(cell.cb);
185                     // skip doing layout
186
row_box.cells.add(cell_box);
187                     // skip adjusting the row height for now
188
// set row height based on real cell contents
189
// set row width based on real cell contents
190
}
191                 //u.p("looping");
192
}
193             //u.p("loop done");
194

195             // move the row to the right y position
196
row_height = 0;
197             row_box.y = prev_row.y + prev_row.height;
198             prev_row = row_box;
199             // adjust the max width
200
if(row_box.width > max_width) {
201                 max_width = row_box.width;
202             }
203             
204             // adjust the height of each cell in this row to be the height of
205
// the row
206
for(int k=0; k<row_box.cells.size(); k++) {
207                 CellBox cb = (CellBox)row_box.cells.get(k);
208                 if(cb.isReal()) {
209                     cb.height = row_box.height;
210                     cb.sub_box.height = row_box.height;
211                 } else {
212                     // adjusting height based on virtual
213
//u.p("adjusting height based on virtual");
214
CellBox real = cb.getReal();
215                     //u.p("the real cb = " + real);
216
RowBox orig_row = real.rb;
217                     //u.p("orig row = " + orig_row);
218
RowBox cur_row = row_box;
219                     //u.p("cur row = " + cur_row);
220

221                     real.height = cur_row.y - orig_row.y + cur_row.height;
222                     real.sub_box.height = real.height;
223                     //u.p("now real = " + real);
224
}
225                 u.p("cell = " + cb);
226             }
227             
228             
229         }
230         
231         box.height = prev_row.y + prev_row.height;
232         box.width = max_width;
233         return box;
234         
235     }
236
237 }
238
239 /* to support row spanning
240 as we go across each row we have to figure out if the current cell
241 is spanned to the one above or not. first we need a growable grid
242 object to manage the cells.
243
244 addCell(x,y,col_span,row_span)
245 getWidth()
246 getHeight()
247 isReal(x,y)
248 //isVirtual(x,y)
249 //getColSpan(x,y)
250 //getRowSpan(x,y)
251 //getRealCell(x,y)
252
253 loop through all cells and add them
254 calc the column widths
255 for each row
256     for each cell
257         if isReal()
258             add to row_box
259             do internal layout
260             set x based on column widths
261             set y based on row
262             set w based on contents and column widths
263             set h based on row height
264         if is virtual()
265             update w based on column
266             update h based on row heights between orig row and this row
267
268 */

269
270
271
Popular Tags