TextSelection extends Selection { @Override public void pasteAtEndOf(Selection positionItem) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } int indexOfNew = 0; OdfElement newElement = positionItem.getElement(); if (positionItem instanceof TextSelection) { indexOfNew = ((TextSelection) positionItem).getIndex() + ((TextSelection) positionItem).getText().length(); newElement = ((TextSelection) positionItem).getContainerElement(); } OdfTextSpan textSpan = getSpan((OdfFileDom) positionItem.getElement().getOwnerDocument()); mIsInserted = false; insertOdfElement(textSpan, indexOfNew, newElement); adjustStyle(newElement, textSpan, null); SelectionManager.refreshAfterPasteAtEndOf(this, positionItem); } TextSelection(Navigation search, String text, OdfElement containerElement,
			int index); TextNavigation getTextNavigation(); static TextSelection newTextSelection(Navigation search,
			String text, OdfElement containerElement, int index); @Override OdfElement getElement(); OdfElement getContainerElement(); @Override int getIndex(); String getText(); @Override void cut(); void applyStyle(OdfStyleBase style); void replaceWith(String newText); Table replaceWith(Table newTable); Image replaceWith(Image newImage); Image replaceWith(URI imageUri); Field replaceWith(Field orgField); Paragraph replaceWith(Paragraph newParagraph); void replaceWith(TextDocument newTextDocument); TextSpanElement createSpanElement(); @Override void pasteAtFrontOf(Selection positionItem); @Override void pasteAtEndOf(Selection positionItem); void setSelectionReplaced(boolean b); boolean isSelectionReplaced(); void addHref(URL url); void addComment(String content, String creator); @Override String toString();  }
@Test public void testPasteAtEndOf() { search = null; search = new TextNavigation("delete", doc); TextSelection sel = null; TextNavigation search1 = new TextNavigation("change", doc); sel = (TextSelection) search1.nextSelection(); int i = 0; while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); i++; try { sel.pasteAtEndOf(item); } catch (InvalidNavigationException e) { Assert.fail(e.getMessage()); } } int j = 0; search = new TextNavigation("deletechange", doc); while (search.hasNext()) { search.nextSelection(); j++; } Assert.assertTrue(i == j); try { doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_COPYTO1)); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }