@Test public void testReplacewithTextDocument() throws Exception { doc = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_FILE)); TextDocument sourcedoc = TextDocument.newTextDocument(); sourcedoc.addParagraph("Hello1 from SIMPLE source document!"); sourcedoc.addParagraph("Hello2 from source document!"); sourcedoc.addParagraph("Hello3 from source document!"); 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) { try { currtenTextSelection.replaceWith(sourcedoc); i++; } catch (Exception e) { e.printStackTrace(); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } } } if (nextTextSelection != null) { try { nextTextSelection.replaceWith(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("TextSelectionReplacewithTextDocumentResult.odt")); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }
public void replaceWith(String newText) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; delete(index, leftLength, parentElement); OdfTextSpan textSpan = new OdfTextSpan((OdfFileDom) parentElement.getOwnerDocument()); textSpan.addContentWhitespace(newText); mIsInserted = false; insertOdfElement(textSpan, index, parentElement); optimize(parentElement); int offset = newText.length() - leftLength; SelectionManager.refresh(getContainerElement(), offset, index + getText().length()); mMatchedText = newText; }
TextSelection extends Selection { public void replaceWith(String newText) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; delete(index, leftLength, parentElement); OdfTextSpan textSpan = new OdfTextSpan((OdfFileDom) parentElement.getOwnerDocument()); textSpan.addContentWhitespace(newText); mIsInserted = false; insertOdfElement(textSpan, index, parentElement); optimize(parentElement); int offset = newText.length() - leftLength; SelectionManager.refresh(getContainerElement(), offset, index + getText().length()); mMatchedText = newText; } }
TextSelection extends Selection { public void replaceWith(String newText) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; delete(index, leftLength, parentElement); OdfTextSpan textSpan = new OdfTextSpan((OdfFileDom) parentElement.getOwnerDocument()); textSpan.addContentWhitespace(newText); mIsInserted = false; insertOdfElement(textSpan, index, parentElement); optimize(parentElement); int offset = newText.length() - leftLength; SelectionManager.refresh(getContainerElement(), offset, index + getText().length()); mMatchedText = newText; } TextSelection(Navigation search, String text, OdfElement containerElement,
			int index); }
TextSelection extends Selection { public void replaceWith(String newText) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; delete(index, leftLength, parentElement); OdfTextSpan textSpan = new OdfTextSpan((OdfFileDom) parentElement.getOwnerDocument()); textSpan.addContentWhitespace(newText); mIsInserted = false; insertOdfElement(textSpan, index, parentElement); optimize(parentElement); int offset = newText.length() - leftLength; SelectionManager.refresh(getContainerElement(), offset, index + getText().length()); mMatchedText = newText; } 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(); }
TextSelection extends Selection { public void replaceWith(String newText) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; delete(index, leftLength, parentElement); OdfTextSpan textSpan = new OdfTextSpan((OdfFileDom) parentElement.getOwnerDocument()); textSpan.addContentWhitespace(newText); mIsInserted = false; insertOdfElement(textSpan, index, parentElement); optimize(parentElement); int offset = newText.length() - leftLength; SelectionManager.refresh(getContainerElement(), offset, index + getText().length()); mMatchedText = newText; } 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();  }