TableSelection extends Selection { public Table replaceWithTable(Table table) { this.sourceTable=table; if (search instanceof TextNavigation) { int leftLength = textSelection.getText().length(); int index = textSelection.getIndex(); boolean continued = false; TextNavigation textSearch = (TextNavigation) search; if (textSearch != null && textSearch.getReplacedItem() != null && textSearch.getReplacedItem().getElement() == this.textSelection .getElement()) { continued = true; } else { textSearch.setHandlePageBreak(false); } preparetableContainer(leftLength, index, continued); Selection.SelectionManager.unregisterItem(this.textSelection); if (textSearch != null) { textSearch.setReplacedItem(this.textSelection); Paragraph lastParagraph = getLastParagraphInTable(tableContainer); OdfElement newStartPoint; if (lastParagraph != null) { newStartPoint = lastParagraph.getOdfElement(); } else { newStartPoint = tableContainer.getOdfElement(); } String content = TextExtractor.getText(newStartPoint); TextSelection selected = newTextSelection(textSearch, this.textSelection.getText(), newStartPoint, content.length() - 1); textSearch.setSelectedItem(selected); } } return tableContainer; } TableSelection(TextSelection selection); Table replaceWithTable(Table table); @Override void cut(); @Override void pasteAtEndOf(Selection positionItem); @Override void pasteAtFrontOf(Selection positionItem);  }
@Test public void testReplaceWithTable() { search = null; search = new TextNavigation("SIMPLE", doc); while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); TableSelection nextTableSelection=new TableSelection(item); Table table=sourcedoc.getTableByName("Table1"); Cell cell = table.getCellByPosition(0, 0); cell.setStringValue("SIMPLE"); Table newtable = nextTableSelection.replaceWithTable(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()); } search = new TextNavigation("Task1", doc); while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); TableSelection nextTableSelection=new TableSelection(item); Table table=sourcedoc.getTableByName("Table2"); Table newtable = nextTableSelection.replaceWithTable(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()); } search = new TextNavigation("RESS%>", doc); while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); TableSelection nextTableSelection=new TableSelection(item); Table table=sourcedoc.getTableByName("Table3"); Table newtable = nextTableSelection.replaceWithTable(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("TestTableSelectionResult.odt")); } catch (Exception e) { Logger.getLogger(TableSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }