TextDocumentSelection extends Selection { public void replaceWithTextDocument(TextDocument textDocument) throws Exception { this.sourceDocument = textDocument; int leftLength = textSelection.getText().length(); int index = textSelection.getIndex(); prepareParagraphContainer(leftLength, index); } TextDocumentSelection(TextSelection selection); void replaceWithTextDocument(TextDocument textDocument); @Override void cut(); @Override void pasteAtEndOf(Selection positionItem); @Override void pasteAtFrontOf(Selection positionItem);  }
@Test public void testReplaceWithTextDocument() { search = null; int i = 0; search = new TextNavigation("SIMPLE", doc); TextSelection currtenTextSelection, nextTextSelection = null; while (search.hasNext()) { if (nextTextSelection != null) { currtenTextSelection = nextTextSelection; } else { currtenTextSelection = (TextSelection) search.nextSelection(); } nextTextSelection = (TextSelection) search.nextSelection(); if (currtenTextSelection != null) { TextDocumentSelection nextParagraphSelection = new TextDocumentSelection(currtenTextSelection); try { nextParagraphSelection.replaceWithTextDocument(sourcedoc); i++; } catch (Exception e) { e.printStackTrace(); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } } } if (nextTextSelection != null) { TextDocumentSelection nextParagraphSelection = new TextDocumentSelection(nextTextSelection); try { nextParagraphSelection.replaceWithTextDocument(sourcedoc); i++; } catch (Exception e) { e.printStackTrace(); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } } search = new TextNavigation("Hello1 from SIMPLE source document!", doc); int j = 0; while (search.hasNext()) { search.nextSelection(); j++; } Assert.assertTrue(i == j); try { doc.save(ResourceUtilities.newTestOutputFile("TestTextDocumentSelectionResult.odt")); } catch (Exception e) { Logger.getLogger(TextDocumentSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }