String[] columnlabels = new String[columncount];
for (int i = 0; i < columncount; i++) {
columnlabels[i] = "ColumnHeader" + i;
}
Table table = Table.newTable(sourcedoc, rowlabels, columnlabels, data);
table.setTableName(tablename);
String tablename2 = "Table2";
int rowcount2 = 10, columncount2 = 4;
double[][] data2 = new double[rowcount2][columncount2];
for (int i = 0; i < rowcount2; i++) {
for (int j = 0; j < columncount2; j++) {
data2[i][j] = Math.random();
}
}
String[] rowlabels2 = new String[rowcount2];
for (int i = 0; i < rowcount2; i++) {
rowlabels2[i] = "RowHeader" + i;
}
String[] columnlabels2 = new String[columncount2];
for (int i = 0; i < columncount2; i++) {
columnlabels2[i] = "ColumnHeader" + i;
}
Table table2 = Table.newTable(sourcedoc, rowlabels2, columnlabels2, data2);
table2.setTableName(tablename2);
String tablename3 = "Table3";
int rownumber3 = 5;
int clmnumber3 = 3;
Table table1 = Table.newTable(sourcedoc, rownumber3, clmnumber3);
table1.setTableName(tablename3);
search = null;
// 6 Simple, at the middle of original Paragraph, split original
// Paragraph, insert before the second Paragraph.
search = new TextNavigation("SIMPLE", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table1");
Cell cell = table.getCellByPosition(0, 0);
cell.setStringValue("SIMPLE");
Table newtable = item.replaceWith(table);
Assert.assertNotNull(newtable);
Assert.assertEquals(1, newtable.getHeaderColumnCount());
Assert.assertEquals(1, newtable.getHeaderRowCount());
Assert.assertEquals(7 + 1, newtable.getRowCount());
Assert.assertEquals(5 + 1, newtable.getColumnCount());
cell = newtable.getCellByPosition(1, 1);
Assert.assertEquals("string", cell.getValueType());
}
// 2 Task1, #1 at the start of original Paragraph, #2 replace original
// Paragraph
search = new TextNavigation("Task1", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table2");
Table newtable = item.replaceWith(table);
Cell cell = newtable.getCellByPosition(0, 0);
cell.setStringValue("From Source Table2");
Assert.assertNotNull(newtable);
Assert.assertEquals(1, newtable.getHeaderColumnCount());
Assert.assertEquals(1, newtable.getHeaderRowCount());
Assert.assertEquals(10 + 1, newtable.getRowCount());
Assert.assertEquals(4 + 1, newtable.getColumnCount());
cell = newtable.getCellByPosition(1, 1);
Assert.assertEquals("float", cell.getValueType());
}
// 1 RESS%>, #1 at the end of original Paragraph,
search = new TextNavigation("RESS%>", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table3");
Table newtable = item.replaceWith(table);
Cell cell = newtable.getCellByPosition(0, 0);
cell.setStringValue("From Source Table3");
Assert.assertNotNull(newtable);
Assert.assertEquals(5, newtable.getRowCount());
Assert.assertEquals(3, newtable.getColumnCount());
}
try {
doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithTableResult.odt"));
} catch (Exception e) {
Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e);
Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'");
}
doc = (TextDocument) Document.loadDocument(ResourceUtilities
.getAbsolutePath(TEST_FILE));
search = new TextNavigation("<<target>>", doc);
int i = 0;
try {
while (search.hasNext()) {
i++;
TextSelection item = (TextSelection) search.nextSelection();
table = sourcedoc.getTableByName("Table1");
Table newtable = item.replaceWith(table);
Assert.assertNotNull(newtable);
Assert.assertEquals(1, newtable.getHeaderColumnCount());
Assert.assertEquals(1, newtable.getHeaderRowCount());
Assert.assertEquals(7 + 1, newtable.getRowCount());
Assert.assertEquals(5 + 1, newtable.getColumnCount());
Cell cell = newtable.getCellByPosition(1, 1);
Assert.assertEquals("string", cell.getValueType());
}
doc.save(ResourceUtilities
.newTestOutputFile("TextSelectionReplacewithTableResult-BasicCases.odt"));
verifyReplaceWithTableByBasicCases("TextSelectionReplacewithTableResult-BasicCases.odt");