@Test public void testReplacewithField() 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!"); VariableField variableField = Fields.createUserVariableField(sourcedoc, "test_simple_variable","testReplacewithField"); Assert.assertNotNull(variableField); TextSpanElement newTextSpanElement = sourcedoc.newParagraph("Update Variable Field:").newTextSpanElement(); variableField.updateField("simple variable content", newTextSpanElement); newTextSpanElement = sourcedoc.newParagraph("Show Variable Field:").newTextSpanElement(); variableField.displayField(newTextSpanElement); Field orgField = sourcedoc.getVariableFieldByName("test_simple_variable"); search = new TextNavigation("SIMPLE", doc); while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); try { Field newField = item.replaceWith(orgField); } catch (InvalidNavigationException e) { e.printStackTrace(); } } try { doc.save(ResourceUtilities.newTestOutputFile("TextSelectionReplacewithFieldResult.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();  }