org.odftoolkit.simple.common.navigation
Class TextSelection

java.lang.Object
  extended by org.odftoolkit.simple.common.navigation.Selection
      extended by org.odftoolkit.simple.common.navigation.TextSelection
Direct Known Subclasses:
CellSelection

public class TextSelection
extends Selection

TextSelection describes a sub element in a paragraph element or a heading element. It is recognized by the container element, which type should be OdfTextParagraph or OdfTextHeading, the start index of text content in container element and the text content of this Selection.


Field Summary
 
Fields inherited from class org.odftoolkit.simple.common.navigation.Selection
search
 
Method Summary
 void addComment(String content, String creator)
          Add a comment to the selection.
 void addHref(URL url)
          Add a hypertext reference to the selection.
 void applyStyle(OdfStyleBase style)
          Apply a style to the selection so that the text style of this selection will append the specified style.
 TextSpanElement createSpanElement()
          Create a span element for this text selection.
 void cut()
          Delete the selection from the document the other matched selection in the same container element will be updated automatically because the start index of the following selections will be changed when the previous selection has been deleted.
 OdfElement getContainerElement()
          Get the paragraph element or heading element that contains this text.
 OdfElement getElement()
          Get the paragraph element or heading element that contains this TextSelection.
 int getIndex()
          Get the start index of the text content of its container element.
 String getText()
          Get the text content of this TextSelection.
 TextNavigation getTextNavigation()
           
 boolean isSelectionReplaced()
           
static TextSelection newTextSelection(Navigation search, String text, OdfElement containerElement, int index)
          Create a new TextSelection.
 void pasteAtEndOf(Selection positionItem)
          Paste this selection just after a specific selection.
 void pasteAtFrontOf(Selection positionItem)
          Paste this selection just before a specific selection.
protected  void refresh(int offset)
          A quick method to update the index of this Selection.
protected  void refreshAfterFrontalDelete(Selection deleteItem)
          When a selected item has been deleted, the Selections after this deleted Selection should be refreshed, as these Selections index have been changed.
protected  void refreshAfterFrontalInsert(Selection pasteItem)
          When a selected item has been inserted, the Selection after the inserted item should be refresh, as these Selections index have been changed.
 Field replaceWith(Field orgField)
          Replace the content with a Field
 Image replaceWith(Image newImage)
          Replace the text content of selection with a new Image.
 Paragraph replaceWith(Paragraph newParagraph)
          Replace the content with a paragraph, the paragraph can be in the same TextDocument or in a different Document.
 void replaceWith(String newText)
          Replace the text content of selection with a new string.
 Table replaceWith(Table newTable)
          Replace the text content of selection with a new Table.
 void replaceWith(TextDocument newTextDocument)
          Replace the content with a TextDocument with Styles.
 Image replaceWith(URI imageUri)
          Replace the text content of selection with a new Image.
 void setSelectionReplaced(boolean b)
           
 String toString()
          return a String Object representing this selection value the text content of the selection, start index in the container element and the text content of the container element will be provided.
 
Methods inherited from class org.odftoolkit.simple.common.navigation.Selection
getNavigation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

getTextNavigation

public TextNavigation getTextNavigation()

newTextSelection

public static TextSelection newTextSelection(Navigation search,
                                             String text,
                                             OdfElement containerElement,
                                             int index)
Create a new TextSelection.

Parameters:
text - the text content of this TextSelection
containerElement - the paragraph element or heading element that contains this TextSelection
index - the start index of the text content in container element
Since:
0.5.5

getElement

public OdfElement getElement()
Get the paragraph element or heading element that contains this TextSelection.

Overrides:
getElement in class Selection
Returns:
OdfElement the container element

getContainerElement

public OdfElement getContainerElement()
Get the paragraph element or heading element that contains this text.

Returns:
OdfElement

getIndex

public int getIndex()
Get the start index of the text content of its container element.

Overrides:
getIndex in class Selection
Returns:
index the start index of the text content of its container element

getText

public String getText()
Get the text content of this TextSelection.

Returns:
text the text content

cut

public void cut()
         throws InvalidNavigationException
Delete the selection from the document the other matched selection in the same container element will be updated automatically because the start index of the following selections will be changed when the previous selection has been deleted.

Specified by:
cut in class Selection
Throws:
InvalidNavigationException - if the selection is unavailable.

applyStyle

public void applyStyle(OdfStyleBase style)
                throws InvalidNavigationException
Apply a style to the selection so that the text style of this selection will append the specified style.

Parameters:
style - the style can be from the current document or user defined
Throws:
InvalidNavigationException - if the selection is unavailable.

replaceWith

public void replaceWith(String newText)
                 throws InvalidNavigationException
Replace the text content of selection with a new string.

Parameters:
newText - the replace text String
Throws:
InvalidNavigationException - if the selection is unavailable.

replaceWith

public Table replaceWith(Table newTable)
                  throws InvalidNavigationException
Replace the text content of selection with a new Table.

Parameters:
newTable - the replace Table
Returns:
Throws:
InvalidNavigationException - if the selection is unavailable.

replaceWith

public Image replaceWith(Image newImage)
                  throws InvalidNavigationException
Replace the text content of selection with a new Image.

Parameters:
newImage - the replace Image
Returns:
Throws:
InvalidNavigationException - if the selection is unavailable.

replaceWith

public Image replaceWith(URI imageUri)
                  throws InvalidNavigationException
Replace the text content of selection with a new Image.

Parameters:
imageUri - the replace Image URI
Returns:
the new Image in the TextDocument,the image name is set to "replace" + System.currentTimeMillis(), please update the name to others by yourself.
Throws:
InvalidNavigationException - if the selection is unavailable.

replaceWith

public Field replaceWith(Field orgField)
                  throws InvalidNavigationException
Replace the content with a Field

Parameters:
orgField - the reference Field to replace.
Returns:
the created field.
Throws:
InvalidNavigationException - if the selection is unavailable.

replaceWith

public Paragraph replaceWith(Paragraph newParagraph)
                      throws InvalidNavigationException
Replace the content with a paragraph, the paragraph can be in the same TextDocument or in a different Document.

Parameters:
newParagraph - the reference paragraph to replace.
Returns:
the replaced Paragraph.
Throws:
InvalidNavigationException - if the selection is unavailable.

replaceWith

public void replaceWith(TextDocument newTextDocument)
                 throws InvalidNavigationException
Replace the content with a TextDocument with Styles. Note: You need cache the TextNavigation.nextSelection item because after you replace currtenTextSelection with TextDocument, TextNavigation.nextSelection will search from the inserted Content, it will make you into a loop if the Search keyword also can be found in the new inserted Content.

The right way to use this replaceWithTextDocument(TextDocument textDocument) method should like this:

search = new TextNavigation("SIMPLE", doc);

TextSelection currtenTextSelection,nextTextSelection=null;

while (search.hasNext()) {

if(nextTextSelection!=null){

currtenTextSelection=nextTextSelection;

}else {

currtenTextSelection = (TextSelection) search.nextSelection();

}

nextTextSelection = (TextSelection) search.nextSelection();

if(currtenTextSelection!=null){

try {

nextTextSelection.replaceWithTextDocument(sourcedoc);

} catch (Exception e) {

e.printStackTrace();

}

}

}

if(nextTextSelection!=null){

try {

nextTextSelection.replaceWithTextDocument(sourcedoc);

} catch (Exception e) {

e.printStackTrace();

}

}

Parameters:
newTextDocument - the reference TextDocument to replace.
Throws:
InvalidNavigationException

createSpanElement

public TextSpanElement createSpanElement()
                                  throws InvalidNavigationException
Create a span element for this text selection.

Returns:
the created text span element for this selection
Throws:
InvalidNavigationException - if the selection is unavailable.
Since:
0.5.5

pasteAtFrontOf

public void pasteAtFrontOf(Selection positionItem)
                    throws InvalidNavigationException
Paste this selection just before a specific selection.

Specified by:
pasteAtFrontOf in class Selection
Parameters:
positionItem - a selection that is used to point out the position
Throws:
InvalidNavigationException - if the selection is unavailable.

pasteAtEndOf

public void pasteAtEndOf(Selection positionItem)
                  throws InvalidNavigationException
Paste this selection just after a specific selection.

Specified by:
pasteAtEndOf in class Selection
Parameters:
positionItem - a selection that is used to point out the position
Throws:
InvalidNavigationException - if the selection is unavailable.

setSelectionReplaced

public void setSelectionReplaced(boolean b)

isSelectionReplaced

public boolean isSelectionReplaced()

addHref

public void addHref(URL url)
             throws InvalidNavigationException
Add a hypertext reference to the selection.

Parameters:
url - the URL of this hypertext reference
Throws:
InvalidNavigationException - if the selection is unavailable.

addComment

public void addComment(String content,
                       String creator)
                throws InvalidNavigationException
Add a comment to the selection.

Parameters:
content - the content of this comment.
creator - the creator of this comment, if creator is null, the value of System.getProperty("user.name") will be used.
Throws:
InvalidNavigationException - if the selection is unavailable.
Since:
0.6.5

toString

public String toString()
return a String Object representing this selection value the text content of the selection, start index in the container element and the text content of the container element will be provided.

Overrides:
toString in class Object
Returns:
a String representation of the value of this TextSelection

refreshAfterFrontalDelete

protected void refreshAfterFrontalDelete(Selection deleteItem)
Description copied from class: Selection
When a selected item has been deleted, the Selections after this deleted Selection should be refreshed, as these Selections index have been changed.

Specified by:
refreshAfterFrontalDelete in class Selection
Parameters:
deleteItem - the deleted Selection

refreshAfterFrontalInsert

protected void refreshAfterFrontalInsert(Selection pasteItem)
Description copied from class: Selection
When a selected item has been inserted, the Selection after the inserted item should be refresh, as these Selections index have been changed.

Specified by:
refreshAfterFrontalInsert in class Selection
Parameters:
pasteItem - the inserted Selection

refresh

protected void refresh(int offset)
Description copied from class: Selection
A quick method to update the index of this Selection.

Specified by:
refresh in class Selection
Parameters:
offset - the offset that the index should be added.


Copyright © 2010-2017 The Apache Software Foundation. All Rights Reserved.