TextSelection extends Selection { public void addComment(String content, String creator) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); OdfFileDom dom = (OdfFileDom) parentElement.getOwnerDocument(); OfficeAnnotationElement annotationElement = dom.newOdfElement(OfficeAnnotationElement.class); DcCreatorElement dcCreatorElement = annotationElement.newDcCreatorElement(); if (creator == null) { creator = System.getProperty("user.name"); } dcCreatorElement.setTextContent(creator); String dcDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()); DcDateElement dcDateElement = annotationElement.newDcDateElement(); dcDateElement.setTextContent(dcDate); TextPElement notePElement = annotationElement.newTextPElement(); TextSpanElement noteSpanElement = notePElement.newTextSpanElement(); OdfOfficeAutomaticStyles styles = null; if (dom instanceof OdfContentDom) { styles = ((OdfContentDom) dom).getAutomaticStyles(); } else if (dom instanceof OdfStylesDom) { styles = ((OdfStylesDom) dom).getAutomaticStyles(); } OdfStyle textStyle = styles.newStyle(OdfStyleFamily.Text); StyleTextPropertiesElement styleTextPropertiesElement = textStyle.newStyleTextPropertiesElement(null); styleTextPropertiesElement.setStyleFontNameAttribute("Tahoma"); styleTextPropertiesElement.setFoFontSizeAttribute("10pt"); styleTextPropertiesElement.setStyleFontNameAsianAttribute("Lucida Sans Unicode"); styleTextPropertiesElement.setStyleFontSizeAsianAttribute("12pt"); noteSpanElement.setStyleName(textStyle.getStyleNameAttribute()); noteSpanElement.setTextContent(content); insertOdfElement(annotationElement, mIndexInContainer, parentElement); int offset = content.length() + 1 + dcDate.length() + 1 + creator.length(); SelectionManager.refresh(getContainerElement(), offset, getIndex()); } 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();  }
@Test public void testAddComment() { try { TextDocument textDoc = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath(TEXT_COMMENT_FILE)); search = new TextNavigation("simpleODF", textDoc); int i=0; while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); item.addComment("simpleODF should be replaced by Simple ODF.", "devin-"+i); i++; } Assert.assertEquals(7, i); textDoc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_COMMENT)); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }