FieldSelection extends Selection { public void replaceWithVariableField(VariableField field) { int leftLength = textSelection.getText().length(); int index = textSelection.getIndex(); prepareSpanContainer(leftLength, index); field.displayField(spanContainer); textSelection.mMatchedText = field.getOdfElement().getTextContent(); int textLength = textSelection.mMatchedText.length(); int offset = textLength - leftLength; SelectionManager.refresh(textSelection.getContainerElement(), offset, index + textLength); } 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 testReplaceWithVariableField() { try { search = new TextNavigation("SelectionUserVariableTarget", doc); VariableField userVariableField = Fields.createUserVariableField(doc, "selection_user_variable", "test"); OdfContentDom contentDom = doc.getContentDom(); NodeList nodeList = contentDom.getElementsByTagName(TextUserFieldGetElement.ELEMENT_NAME.getQName()); int i = nodeList.getLength(); OdfStylesDom styleDom = doc.getStylesDom(); nodeList = styleDom.getElementsByTagName(TextUserFieldGetElement.ELEMENT_NAME.getQName()); i += nodeList.getLength(); while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); FieldSelection fieldSelection = new FieldSelection(item); fieldSelection.replaceWithVariableField(userVariableField); i++; } nodeList = contentDom.getElementsByTagName(TextUserFieldGetElement.ELEMENT_NAME.getQName()); int j = nodeList.getLength(); nodeList = styleDom.getElementsByTagName(TextUserFieldGetElement.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() + "'"); } }