FieldSelection extends Selection { public void replaceWith(String newText) throws InvalidNavigationException { textSelection.replaceWith(newText); } FieldSelection(TextSelection selection); Field replaceWithSimpleField(Field.FieldType fieldType); void replaceWithReferenceField(ReferenceField field, ReferenceField.DisplayType type); void replaceWithVariableField(VariableField field); ConditionField replaceWithConditionField(String condition, String trueText, String falseText); ConditionField replaceWithHiddenTextField(String condition, String text); void applyStyle(OdfStyleBase style); @Override void cut(); void replaceWith(String newText); @Override void pasteAtEndOf(Selection positionItem); @Override void pasteAtFrontOf(Selection positionItem);  }
@Test public void testReplaceWith() { try { search = new TextNavigation("ReplaceDateTarget", doc); int i =0; while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); FieldSelection fieldSelection = new FieldSelection(item); fieldSelection.replaceWith("hello world."); i++; } doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_REPLACE)); TextDocument doc1 = (TextDocument) Document.loadDocument(ResourceUtilities.getAbsolutePath(SAVE_FILE_REPLACE)); search = new TextNavigation("ReplaceDateTarget", doc1); if (search.hasNext()) { Assert.fail(); } search = new TextNavigation("hello world.", doc1); int j =0; while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); item.replaceWith("hi world."); j++; } Assert.assertEquals(i, j); } catch (Exception e) { Logger.getLogger(FieldSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }