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