TableTableElement newTEle = (TableTableElement) OdfXMLFactory.newOdfElement(dom, OdfName.newName(
OdfDocumentNamespace.TABLE, "table"));
String tablename = getUniqueTableName(container);
newTEle.setTableNameAttribute(tablename);
// create style
OdfStyle tableStyle = styles.newStyle(OdfStyleFamily.Table);
String stylename = tableStyle.getStyleNameAttribute();
tableStyle.setProperty(StyleTablePropertiesElement.Width, tableWidth + "in");
tableStyle.setProperty(StyleTablePropertiesElement.Align, DEFAULT_TABLE_ALIGN);
if (marginLeft != 0) {
tableStyle.setProperty(StyleTablePropertiesElement.MarginLeft, (new DecimalFormat("#0.##")
.format(marginLeft) + Unit.CENTIMETER.abbr()).replace(",", "."));
}
if (marginRight != 0) {
tableStyle.setProperty(StyleTablePropertiesElement.MarginRight, (new DecimalFormat("#0.##")
.format(marginRight) + Unit.CENTIMETER.abbr()).replace(",", "."));
}
newTEle.setStyleName(stylename);
// 2. create column elements
// 2.0 create column style
OdfStyle columnStyle = styles.newStyle(OdfStyleFamily.TableColumn);
String columnStylename = columnStyle.getStyleNameAttribute();
// for spreadsheet document, no need compute column width.
if (isTextDocument) {
columnStyle.setProperty(StyleTableColumnPropertiesElement.ColumnWidth, IN_FORMAT.format(tableWidth
/ numCols)
+ "in");
columnStyle.setProperty(StyleTableColumnPropertiesElement.RelColumnWidth, Math
.round(DEFAULT_REL_TABLE_WIDTH / numCols)
+ "*");
}
// 2.1 create header column elements
if (headerColumnNumber > 0) {
TableTableHeaderColumnsElement headercolumns = (TableTableHeaderColumnsElement) OdfXMLFactory
.newOdfElement(dom, OdfName.newName(OdfDocumentNamespace.TABLE, "table-header-columns"));
TableTableColumnElement headercolumn = (TableTableColumnElement) OdfXMLFactory.newOdfElement(dom, OdfName
.newName(OdfDocumentNamespace.TABLE, "table-column"));
if (headerColumnNumber > 1) {
headercolumn.setTableNumberColumnsRepeatedAttribute(headerColumnNumber);
} else {
headercolumn.removeAttributeNS(OdfDocumentNamespace.TABLE.getUri(), "number-columns-repeated");
}
headercolumns.appendChild(headercolumn);
newTEle.appendChild(headercolumns);
headercolumn.setStyleName(columnStylename);
}
// 2.2 create common column elements
TableTableColumnElement columns = (TableTableColumnElement) OdfXMLFactory.newOdfElement(dom, OdfName.newName(
OdfDocumentNamespace.TABLE, "table-column"));
int tableNumberColumnsRepeatedValue = numCols - headerColumnNumber;
if (tableNumberColumnsRepeatedValue > 1) {
columns.setTableNumberColumnsRepeatedAttribute(tableNumberColumnsRepeatedValue);
}
columns.setStyleName(columnStylename);
newTEle.appendChild(columns);
// 3. create row elements
// 3.0 create 4 kinds of styles
OdfStyle lefttopStyle = null, leftbottomStyle = null, righttopStyle = null, rightbottomStyle = null;
if (isTextDocument) {
lefttopStyle = styles.newStyle(OdfStyleFamily.TableCell);
setLeftTopBorderStyleProperties(lefttopStyle);
leftbottomStyle = styles.newStyle(OdfStyleFamily.TableCell);
setLeftBottomBorderStylesProperties(leftbottomStyle);
righttopStyle = styles.newStyle(OdfStyleFamily.TableCell);
setRightTopBorderStyleProperties(righttopStyle);
rightbottomStyle = styles.newStyle(OdfStyleFamily.TableCell);
setRightBottomBorderStylesProperties(rightbottomStyle);
}
// 3.1 create header row elements
if (headerRowNumber > 0) {
TableTableHeaderRowsElement headerrows = (TableTableHeaderRowsElement) OdfXMLFactory.newOdfElement(dom,
OdfName.newName(OdfDocumentNamespace.TABLE, "table-header-rows"));
for (int i = 0; i < headerRowNumber; i++) {
TableTableRowElement aRow = (TableTableRowElement) OdfXMLFactory.newOdfElement(dom, OdfName.newName(
OdfDocumentNamespace.TABLE, "table-row"));
for (int j = 0; j < numCols; j++) {
TableTableCellElement aCell = (TableTableCellElement) OdfXMLFactory.newOdfElement(dom, OdfName
.newName(OdfDocumentNamespace.TABLE, "table-cell"));
if (isTextDocument) {
if ((j + 1 == numCols) && (i == 0)) {
aCell.setStyleName(righttopStyle.getStyleNameAttribute());
} else if (i == 0) {
aCell.setStyleName(lefttopStyle.getStyleNameAttribute());
} else if ((j + 1 == numCols) && (i > 0)) {
aCell.setStyleName(rightbottomStyle.getStyleNameAttribute());
} else {
aCell.setStyleName(leftbottomStyle.getStyleNameAttribute());
}
}
aRow.appendChild(aCell);
}
headerrows.appendChild(aRow);
}
newTEle.appendChild(headerrows);
}
// 3.2 create common row elements
for (int i = headerRowNumber; i < numRows; i++) {
TableTableRowElement aRow = (TableTableRowElement) OdfXMLFactory.newOdfElement(dom, OdfName.newName(
OdfDocumentNamespace.TABLE, "table-row"));
for (int j = 0; j < numCols; j++) {
TableTableCellElement aCell = (TableTableCellElement) OdfXMLFactory.newOdfElement(dom, OdfName.newName(
OdfDocumentNamespace.TABLE, "table-cell"));
if (isTextDocument) {
if ((j + 1 == numCols) && (i == 0)) {
aCell.setStyleName(righttopStyle.getStyleNameAttribute());
} else if (i == 0) {
aCell.setStyleName(lefttopStyle.getStyleNameAttribute());
} else if ((j + 1 == numCols) && (i > 0)) {
aCell.setStyleName(rightbottomStyle.getStyleNameAttribute());
} else {
aCell.setStyleName(leftbottomStyle.getStyleNameAttribute());
}