textTablePropertyStore = oldTable.getPropertyStore();
}
int columnStart = 0;
int rowStart = 0;
if (cellCloneServices.length == 0) {
CloneException cloneException = new CloneException(
"No range selected.");
throw cloneException;
}
int columnArrayLength = cellCloneServices[0].length;
if (position.getType() == null) {
CloneException cloneException = new CloneException(
"No valid type for the position.");
throw cloneException;
}
if (ITextRange.class.isAssignableFrom(position.getType())) {
ITextRange range = (ITextRange) position.getDestinationObject();
if (textTableService == null)
textTableService = textDocument.getTextTableService();
newTable = (ITextTable) textTableService.constructTextTable(
rowCount, columnCount);
newTable.getProperties().setRepeatHeadline(
textTablePropertyStore.repeatHeadline());
if (textContentService == null)
textContentService = textDocument.getTextService()
.getTextContentService();
if (range != null) {
textContentService.insertTextContent(range, newTable);
newTable.getProperties().setTableColumnSeparators(
textTablePropertyStore.getTableColumnSeparators());
} else {
CloneException cloneException = new CloneException(
"No destination selected..");
throw cloneException;
}
} else if (ITextTable.class.isAssignableFrom(position.getType())) {
ITextTable range = (ITextTable) position.getDestinationObject();
TextTableService tableService = new TextTableService(
textDocument);
newTable = (ITextTable) tableService.constructTextTable(
rowCount, columnCount);
newTable.getProperties().setRepeatHeadline(
textTablePropertyStore.repeatHeadline());
if (range != null) {
if (textContentService == null)
textContentService = textDocument.getTextService()
.getTextContentService();
textContentService.insertTextContentAfter(newTable, range);
newTable.getProperties().setTableColumnSeparators(
textTablePropertyStore.getTableColumnSeparators());
} else {
CloneException cloneException = new CloneException(
"No destination selected..");
throw cloneException;
}
} else if (TextTableCell.class.isAssignableFrom(position.getType())) {
TextTableCell textTableCell = ((TextTableCell) position
.getDestinationObject());
newTable = textTableCell.getTextTable();
ITextTableCellName cellName = textTableCell.getName();
columnStart = cellName.getColumnIndex();
rowStart = cellName.getRowIndex();
int newTableColumnCount = newTable.getColumnCount();
int newTableRowCount = newTable.getRowCount();
if (columnStart + columnArrayLength > newTableColumnCount) {
CloneException cloneException = new CloneException(
"Too much columns selected.");
throw cloneException;
}
if ((rowStart + rowCount) * newTableColumnCount > ITextTable.MAX_CELLS_IN_TABLE) {
CloneException cloneException = new CloneException(
"The sumbitted range is too big.");
throw cloneException;
}
if (rowStart + rowCount > newTableRowCount) {
newTable.addRow(rowStart, (rowStart + rowCount)
- newTableRowCount);
}
} else if (TextTableCellRange.class.isAssignableFrom(position
.getType())) {
TextTableCellRange tableCellRange = ((TextTableCellRange) position
.getDestinationObject());
int tmpColumnCount = tableCellRange.getColumnCount();
int tmpRowCount = tableCellRange.getRowCount();
if (tmpColumnCount == 0 || tmpRowCount == 0) {
CloneException cloneException = new CloneException(
"No range selected.");
throw cloneException;
}
newTable = tableCellRange.getCell(0, 0).getTextTable();
;
ITextTableCellRangeName rangeName = tableCellRange
.getRangeName();
columnStart = rangeName.getRangeStartColumnIndex();
rowStart = rangeName.getRangeStartRowIndex();
if (tmpColumnCount != columnCount || tmpRowCount != rowCount) {
CloneException cloneException = new CloneException(
"The selected range is not valid.");
throw cloneException;
}
} else if (TextTableRow.class.isAssignableFrom(position.getType())) {
TextTableRow textTableRow = ((TextTableRow) position
.getDestinationObject());
ITextTableCellRange tableCellRange = textTableRow
.getCellRange();
int tmpColumnCount = tableCellRange.getColumnCount();
int tmpRowCount = tableCellRange.getRowCount();
if (tmpColumnCount == 0 || tmpRowCount == 0) {
CloneException cloneException = new CloneException(
"No range selected.");
throw cloneException;
}
newTable = tableCellRange.getCell(0, 0).getTextTable();
;
ITextTableCellRangeName rangeName = tableCellRange
.getRangeName();
columnStart = rangeName.getRangeStartColumnIndex();
rowStart = rangeName.getRangeStartRowIndex();
if (tmpColumnCount != columnCount || tmpRowCount != rowCount) {
CloneException cloneException = new CloneException(
"The selected range is not valid.");
throw cloneException;
}
} else if (TextTableColumn.class.isAssignableFrom(position
.getType())) {
// TODO fill with logic
} else {
CloneException cloneException = new CloneException(
"No range selected.");
throw cloneException;
}
if (newTable != null) {
int rowArrayLength = cellCloneServices.length;
for (int rows = 0; rows < rowArrayLength; rows++) {
for (int columns = 0; columns < columnArrayLength; columns++) {
ICloneService cellClone = cellCloneServices[rows][columns];
ITextTableCell textTableCell = newTable.getCell(
columnStart + columns, rowStart + rows);
CloneDestinationPosition destinationCell = new CloneDestinationPosition(
textTableCell, textTableCell.getClass());
cellClone.cloneToPositionNoReturn(destinationCell,
adoptContent, propertyKeysContainer);
}
}
if (generateReturnValue) {
ITextTableCellRange textTableCellRange = newTable
.getCellRange(columnStart, rowStart,
columnArrayLength, rowArrayLength);
return new ClonedObject(textTableCellRange,
textTableCellRange.getClass());
} else
return null;
} else {
CloneException cloneException = new CloneException(
"No range selected.");
throw cloneException;
}
} catch (Exception exception) {
CloneException cloneException = new CloneException(exception
.getMessage());
cloneException.initCause(exception);
throw cloneException;
}
}