newRow = createDefaultRow(columnCount, true);
mTableElement.appendChild(newRow);
} else {
newRow = (TableTableRowElement) OdfXMLFactory.newOdfElement((OdfFileDom) mTableElement.getOwnerDocument(),
OdfName.newName(OdfDocumentNamespace.TABLE, "table-row"));
TableTableCellElementBase cellElement = (TableTableCellElementBase) refRowElement.getFirstChild();
int i = 1;
while (cellElement != null && i <= columnCount) {
// covered element
String tableNameSpace = OdfDocumentNamespace.TABLE.getUri();
if (cellElement instanceof TableCoveredTableCellElement) {
TableCoveredTableCellElement coveredCellEle = (TableCoveredTableCellElement) cellElement;
// find cover cell element
TableTableRowElement aRowEle = (TableTableRowElement) (coveredCellEle.getParentNode()
.getPreviousSibling());
while (aRowEle != null) {
// the cover cell and the first covered cell must have
// the same column index.
TableTableCellElementBase coverCellEle = (TableTableCellElementBase) (aRowEle.getFirstChild());
int j = coverCellEle.getTableNumberColumnsRepeatedAttribute();
while (j < i) {
coverCellEle = (TableTableCellElementBase) (coverCellEle.getNextSibling());
if (coverCellEle instanceof TableTableCellElement) {
j += (coverCellEle.getTableNumberColumnsRepeatedAttribute() * (((TableTableCellElement) coverCellEle)
.getTableNumberColumnsSpannedAttribute()));
} else {
j += coverCellEle.getTableNumberColumnsRepeatedAttribute();
}
}
// find the cover cell, now start cell clone.
if (coverCellEle instanceof TableTableCellElement) {
TableTableCellElement newCellEle = (TableTableCellElement) (coverCellEle.cloneNode(true));
cleanCell(newCellEle);
newCellEle.removeAttributeNS(tableNameSpace, "number-rows-spanned");
newRow.appendChild(newCellEle);
// deal with the following covered cell, spread
// sheet need change these covered cell to cell.
if (mIsSpreadsheet) {
// update column repeated number.
int columnsSpannedNumber = newCellEle.getTableNumberColumnsSpannedAttribute();
newCellEle.removeAttributeNS(tableNameSpace, "number-columns-spanned");
int newColumnRepeatedNumber = newCellEle.getTableNumberColumnsRepeatedAttribute()
* columnsSpannedNumber;
if (newColumnRepeatedNumber > 1) {
newCellEle.setTableNumberColumnsRepeatedAttribute(newColumnRepeatedNumber);
} else {
newCellEle.removeAttributeNS(tableNameSpace, "number-columns-repeated");
}
// ignore the following covered cell of
// reference row.
// added by Daisy because of a bug in demo4
// cellElement is a covered cell. coverCellEle
// is its cover cell.
// below codes will count
// newColumnRepeatedNumber covered cell.
int tempi = newColumnRepeatedNumber;
while (tempi > 0) {
int iColumnRepeatedNumber = cellElement.getTableNumberColumnsRepeatedAttribute();
if (iColumnRepeatedNumber > tempi) {
// split covered cell
if (cellElement instanceof TableCoveredTableCellElement) {
cellElement.setTableNumberColumnsRepeatedAttribute(iColumnRepeatedNumber
- tempi);
TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement
.cloneNode(true);
cleanCell(newCoveredCellEle);
if (tempi > 1) {
newCoveredCellEle.setTableNumberColumnsRepeatedAttribute(tempi);
} else {
newCoveredCellEle.removeAttributeNS(
OdfDocumentNamespace.TABLE.getUri(), "number-columns-repeated");
}
refRowElement.insertBefore(newCoveredCellEle, cellElement);
cellElement = newCoveredCellEle;
}
}
tempi = tempi - cellElement.getTableNumberColumnsRepeatedAttribute();
i = i + cellElement.getTableNumberColumnsRepeatedAttribute();
if (!(cellElement instanceof TableCoveredTableCellElement) && (tempi > 0)){
Logger.getLogger(Table.class.getName()).log(Level.FINE, "Not covered cell was ignored");
}
cellElement = (TableTableCellElementBase) (cellElement.getNextSibling());
// while ((cellElement != null) &&
// (cellElement instanceof
// TableCoveredTableCellElement)) {
// cellElement = (TableTableCellElementBase)
// (cellElement.getNextSibling());
// }
}
// i += newColumnRepeatedNumber;
} else {
// clone the following covered cell of reference
// row.
// added by Daisy because of a bug in demo4
cellElement = (TableTableCellElementBase) cellElement.getNextSibling();
i += cellElement.getTableNumberColumnsRepeatedAttribute();
int newColumnSpanNumber = newCellEle.getTableNumberColumnsSpannedAttribute();
while ((cellElement != null) && (cellElement instanceof TableCoveredTableCellElement)
&& (newColumnSpanNumber > 1)) {
TableCoveredTableCellElement newCoveredCellElement = (TableCoveredTableCellElement) cellElement
.cloneNode(true);
cleanCell(newCoveredCellElement);
newRow.appendChild(newCoveredCellElement);
i += cellElement.getTableNumberColumnsRepeatedAttribute();
cellElement = (TableTableCellElementBase) cellElement.getNextSibling();
newColumnSpanNumber--;
}
}
break;
}
// continue find cover cell
Node preNode = aRowEle.getPreviousSibling();
if (preNode instanceof TableTableRowElement) {
aRowEle = (TableTableRowElement) preNode;
} else {
// </table:table-header-rows>
aRowEle = (TableTableRowElement) (preNode.getLastChild());
}
}
} else {
TableTableCellElement newCellEle = (TableTableCellElement) cellElement.cloneNode(true);
cleanCell(newCellEle);
newRow.appendChild(newCellEle);
Integer tableNumberColumnsRepeated = newCellEle.getTableNumberColumnsRepeatedAttribute();
Integer tableNumberColumnsSpanned = newCellEle.getTableNumberColumnsSpannedAttribute();
i += tableNumberColumnsRepeated * tableNumberColumnsSpanned;
cellElement = (TableTableCellElementBase) cellElement.getNextSibling();
if (tableNumberColumnsSpanned > 1) {
int j = 1;
if (mIsSpreadsheet) {
newCellEle.removeAttributeNS(tableNameSpace, "number-columns-spanned");
int newColumnRepeatedNumber = tableNumberColumnsRepeated * tableNumberColumnsSpanned;
if (newColumnRepeatedNumber > 1) {
newCellEle.setTableNumberColumnsRepeatedAttribute(newColumnRepeatedNumber);
} else {
newCellEle.removeAttributeNS(tableNameSpace, "number-columns-repeated");
}
// cellElement is not a covered cell.
// below codes will count
// (newColumnRepeatedNumber-1) covered cell.
int tempi = newColumnRepeatedNumber;
while (tempi > 1) {
int iColumnRepeatedNumber = cellElement.getTableNumberColumnsRepeatedAttribute();
if (iColumnRepeatedNumber > tempi + 1) {
// split covered cell
if (cellElement instanceof TableCoveredTableCellElement) {
cellElement.setTableNumberColumnsRepeatedAttribute(iColumnRepeatedNumber
- tempi + 1);
TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement
.cloneNode(true);
cleanCell(newCoveredCellEle);
newCoveredCellEle.setTableNumberColumnsRepeatedAttribute(tempi - 1);
refRowElement.insertBefore(newCoveredCellEle, cellElement);
cellElement = newCoveredCellEle;
}
}
tempi = tempi - cellElement.getTableNumberColumnsRepeatedAttribute();
if (!(cellElement instanceof TableCoveredTableCellElement) && (tempi > 1)){
Logger.getLogger(Table.class.getName()).log(Level.FINE, "Not covered cell was ignored");
}
cellElement = (TableTableCellElementBase) (cellElement.getNextSibling());
}
} else {
while ((j < tableNumberColumnsSpanned) && (cellElement != null)) {
int iColumnRepeatedNumber = cellElement.getTableNumberColumnsRepeatedAttribute();
if (iColumnRepeatedNumber > tableNumberColumnsSpanned - j) {
// split covered cell
if (cellElement instanceof TableCoveredTableCellElement) {
cellElement.setTableNumberColumnsRepeatedAttribute(iColumnRepeatedNumber
- tableNumberColumnsSpanned + j);
TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement
.cloneNode(true);
cleanCell(newCoveredCellEle);
newCoveredCellEle
.setTableNumberColumnsRepeatedAttribute(tableNumberColumnsSpanned - j);
refRowElement.insertBefore(newCoveredCellEle, cellElement);
cellElement = newCoveredCellEle;
}
}
TableTableCellElementBase newCoveredCellEle = (TableTableCellElementBase) cellElement
.cloneNode(true);
cleanCell(newCoveredCellEle);
newRow.appendChild(newCoveredCellEle);
j += newCoveredCellEle.getTableNumberColumnsRepeatedAttribute();
cellElement = (TableTableCellElementBase) cellElement.getNextSibling();
}
}
}
}