TextSelection extends Selection { public void applyStyle(OdfStyleBase style) throws InvalidNavigationException { if (validate() == false) { throw new InvalidNavigationException("No matched string at this position"); } OdfElement parentElement = getContainerElement(); int leftLength = getText().length(); int index = mIndexInContainer; appendStyle(index, leftLength, parentElement, style); } 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 testApplyStyle() { search = null; search = new TextNavigation("delete", doc); OdfOfficeAutomaticStyles autoStyles = null; try { autoStyles = doc.getContentDom().getAutomaticStyles(); } catch (Exception e1) { Assert.fail("Failed with " + e1.getClass().getName() + ": '" + e1.getMessage() + "'"); } OdfStyleBase style = autoStyles.getStyle("T4", OdfStyleFamily.Text); Assert.assertNotNull(style); while (search.hasNext()) { TextSelection item = (TextSelection) search.nextSelection(); try { item.applyStyle(style); } catch (InvalidNavigationException e) { Assert.fail(e.getMessage()); } } try { doc.save(ResourceUtilities.newTestOutputFile(SAVE_FILE_STYLE)); } catch (Exception e) { Logger.getLogger(TextSelectionTest.class.getName()).log(Level.SEVERE, e.getMessage(), e); Assert.fail("Failed with " + e.getClass().getName() + ": '" + e.getMessage() + "'"); } }