OLD | NEW |
| (Empty) |
1 package autotest.tko; | |
2 | |
3 import autotest.tko.Spreadsheet.CellInfo; | |
4 | |
5 import com.google.gwt.core.client.GWT; | |
6 import com.google.gwt.dom.client.Element; | |
7 import com.google.gwt.user.client.DOM; | |
8 import com.google.gwt.user.client.ui.HTMLTable; | |
9 | |
10 | |
11 public class TableRenderer { | |
12 // min-width/min-height aren't supported in the hosted mode browser | |
13 public static final String SIZE_PREFIX = GWT.isScript() ? "min-" : ""; | |
14 private static final String NONCLICKABLE_CLASS = "spreadsheet-cell-nonclicka
ble"; | |
15 | |
16 protected String attributeString(String attribute, String value) { | |
17 if (value.equals("")) | |
18 return ""; | |
19 return " " + attribute + "=\"" + value + "\""; | |
20 } | |
21 | |
22 public void renderRowsAndAppend(HTMLTable tableObject, CellInfo[][] rows, | |
23 int startRow, int maxRows, boolean renderNul
l) { | |
24 StringBuffer htmlBuffer= new StringBuffer(); | |
25 htmlBuffer.append("<table><tbody>"); | |
26 for (int rowIndex = startRow; rowIndex < startRow + maxRows && rowIndex
< rows.length; | |
27 rowIndex++) { | |
28 CellInfo[] row = rows[rowIndex]; | |
29 htmlBuffer.append("<tr>"); | |
30 for (CellInfo cell : row) { | |
31 if (cell == null && renderNull) { | |
32 htmlBuffer.append("<td> </td>"); | |
33 } else if (cell != null) { | |
34 String tdAttributes = "", divAttributes = "", divStyle = ""; | |
35 if (cell.color != null) { | |
36 tdAttributes += attributeString("style", | |
37 "background-color: " + ce
ll.color + ";"); | |
38 } | |
39 if (cell.rowSpan > 1) { | |
40 tdAttributes += attributeString("rowspan", Integer.toStr
ing(cell.rowSpan)); | |
41 } | |
42 if (cell.colSpan > 1) { | |
43 tdAttributes += attributeString("colspan", Integer.toStr
ing(cell.colSpan)); | |
44 } | |
45 | |
46 if (cell.widthPx != null) { | |
47 divStyle += SIZE_PREFIX + "width: " + cell.widthPx + "px
; "; | |
48 } | |
49 if (cell.heightPx != null) { | |
50 divStyle += SIZE_PREFIX + "height: " + cell.heightPx + "
px; "; | |
51 } | |
52 if (!divStyle.equals("")) { | |
53 divAttributes += attributeString("style", divStyle); | |
54 } | |
55 if (cell.isEmpty()) { | |
56 divAttributes += attributeString("class", NONCLICKABLE_C
LASS); | |
57 } | |
58 | |
59 htmlBuffer.append("<td " + tdAttributes + ">"); | |
60 htmlBuffer.append("<div " + divAttributes + ">"); | |
61 htmlBuffer.append(cell.contents); | |
62 htmlBuffer.append("</div></td>"); | |
63 } | |
64 } | |
65 htmlBuffer.append("</tr>"); | |
66 } | |
67 htmlBuffer.append("</tbody></table>"); | |
68 | |
69 renderBody(tableObject, htmlBuffer.toString()); | |
70 } | |
71 | |
72 public void renderRows(HTMLTable tableObject, CellInfo[][] rows, boolean ren
derNull) { | |
73 TkoUtils.clearDomChildren(tableObject.getElement()); // remove existing
tbodies | |
74 renderRowsAndAppend(tableObject, rows, 0, rows.length, renderNull); | |
75 } | |
76 | |
77 public void renderRows(HTMLTable tableObject, CellInfo[][] rows) { | |
78 renderRows(tableObject, rows, true); | |
79 } | |
80 | |
81 private void renderBody(HTMLTable tableObject, String html) { | |
82 // render the table within a DIV | |
83 Element tempDiv = DOM.createDiv(); | |
84 tempDiv.setInnerHTML(html); | |
85 | |
86 // inject the new tbody into the existing table | |
87 Element newTable = tempDiv.getFirstChildElement(); | |
88 Element newBody = newTable.getFirstChildElement(); | |
89 tableObject.getElement().appendChild(newBody); | |
90 | |
91 setBodyElement(tableObject, newBody); | |
92 } | |
93 | |
94 /** | |
95 * A little hack to set the private member variable bodyElem of an HTMLTable
. | |
96 */ | |
97 protected native void setBodyElement(HTMLTable table, Element newBody) /*-{ | |
98 table.@com.google.gwt.user.client.ui.HTMLTable::bodyElem = newBody; | |
99 }-*/; | |
100 | |
101 } | |
OLD | NEW |