FieldSelection extends Selection { public ConditionField replaceWithConditionField(String condition, String trueText, String falseText) { int leftLength = textSelection.getText().length(); int index = textSelection.getIndex(); prepareSpanContainer(leftLength, index); textSelection.mMatchedText = ""; int offset = -leftLength; SelectionManager.refresh(textSelection.getContainerElement(), offset, index); return Fields.createConditionField(spanContainer, condition, trueText, falseText); } 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 testReplaceWithConditionField() { try { search = new TextNavigation("ReplaceConditionTarget", doc); VariableField simpleVariableField = Fields.createSimpleVariableField(doc, "test_con_variable"); OdfTextParagraph varParagraph = doc.newParagraph("test_con_variable:"); simpleVariableField.updateField("true", varParagraph); OdfContentDom contentDom = doc.getContentDom(); NodeList nodeList = contentDom.getElementsByTagName(TextConditionalTextElement.ELEMENT_NAME.getQName()); int i = nodeList.getLength(); OdfStylesDom styleDom = doc.getStylesDom(); nodeList = styleDom.getElementsByTagName(TextConditionalTextElement.ELEMENT_NAME.getQName()); i += nodeList.getLength(); while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); FieldSelection fieldSelection = new FieldSelection(item); fieldSelection.replaceWithConditionField("test_con_variable == \"true\"", "trueText", "falseText"); i++; } nodeList = contentDom.getElementsByTagName(TextConditionalTextElement.ELEMENT_NAME.getQName()); int j = nodeList.getLength(); nodeList = styleDom.getElementsByTagName(TextConditionalTextElement.ELEMENT_NAME.getQName()); j += nodeList.getLength(); Assert.assertEquals(j, i); doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_REPLACE)); } catch (Exception e) { Logger.getLogger(FieldSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }