ParagraphSelection extends Selection { public Paragraph replaceWithParagraph(Paragraph paragraph) { this.sourceParagraph=paragraph; if (search instanceof TextNavigation) { TextNavigation textSearch = (TextNavigation) search; int leftLength = textSelection.getText().length(); int index = textSelection.getIndex(); boolean continued = false; if (textSearch != null && textSearch.getReplacedItem() != null && textSearch.getReplacedItem().getElement() == this.textSelection .getElement()) { continued = true; } else { textSearch.setHandlePageBreak(false); } prepareParagraphContainer(leftLength, index, continued); Selection.SelectionManager.unregisterItem(this.textSelection); if (textSearch != null) { textSearch.setReplacedItem(this.textSelection); OdfElement containerElement = paragraphContainer .getOdfElement(); String content = TextExtractor.getText(containerElement); TextSelection selected = TextSelection.newTextSelection( textSearch, this.textSelection.getText(), containerElement, content.length() - 1); textSearch.setSelectedItem(selected); } } return paragraphContainer; } ParagraphSelection(TextSelection selection); Paragraph replaceWithParagraph(Paragraph paragraph); @Override void cut(); @Override void pasteAtEndOf(Selection positionItem); @Override void pasteAtFrontOf(Selection positionItem);  }
@Test public void testReplaceWithParagraph() { search = null; search = new TextNavigation("SIMPLE", doc); int i = 0; while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); ParagraphSelection nextParagraphSelection=new ParagraphSelection(item); Paragraph paragraph=sourcedoc.getParagraphByIndex(0, true); nextParagraphSelection.replaceWithParagraph(paragraph); i++; } search = new TextNavigation("Hello1 from SIMPLE source document!", doc); int j = 0; while (search.hasNext()) { search.nextSelection(); j++; } Assert.assertTrue(i == j); search = new TextNavigation("Task1", doc); i = 0; while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); ParagraphSelection nextParagraphSelection=new ParagraphSelection(item); Paragraph paragraph=sourcedoc.getParagraphByIndex(1, true); nextParagraphSelection.replaceWithParagraph(paragraph); i++; } search = new TextNavigation("Hello2 from source document!", doc); j = 0; while (search.hasNext()) { search.nextSelection(); j++; } Assert.assertTrue(i == j); search = new TextNavigation("Container", doc); i = 0; while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); ParagraphSelection nextParagraphSelection=new ParagraphSelection(item); Paragraph paragraph=sourcedoc.getParagraphByIndex(2, true); nextParagraphSelection.replaceWithParagraph(paragraph); i++; } search = new TextNavigation("Hello3 from source document!", doc); j = 0; while (search.hasNext()) { search.nextSelection(); j++; } Assert.assertTrue(i == j); try { doc.save(ResourceUtilities.newTestOutputFile("TestParagraphSelectionResult.odt")); } catch (Exception e) { Logger.getLogger(ParagraphSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }