com.jivesoftware.spark.component.spelling
Class JTextComponentWordParser

java.lang.Object
  extended bycom.wintertree.ssce.StringWordParser
      extended bycom.jivesoftware.spark.component.spelling.JTextComponentWordParser
All Implemented Interfaces:
java.util.Enumeration, java.io.Serializable, com.wintertree.ssce.WordParser

public class JTextComponentWordParser
extends com.wintertree.ssce.StringWordParser

Parse words from a Swing JTextComponent. In addition to parsing, this class can also delete words, replace words, and detect doubled words. JTextComponentWordParser maintains a cursor which points to the start of the current word. The current word is always selected in the JTextComponent to show context. This class is part of the Sentry Java SDK examples.

The text in the JTextComponent is read when the JTextComponentWordParser is constructed. If the contents of the JTextComponent change, the updateText method must be called.

If the JTextComponent can contain text with HTML markups, extend this class from HTMLStringWordParser instead of StringWordParser.

Version:
$Id: JTextComponentWordParser.java 18715 2005-04-05 17:10:46 -0700 (Tue, 05 Apr 2005) bill $
See Also:
Serialized Form

Field Summary
protected  javax.swing.text.JTextComponent component
          The JTextComponent being parsed.
 
Fields inherited from class com.wintertree.ssce.StringWordParser
cachedWord, cursor, is1stWord, isHyphenDelimiter, nReplacements, nWords, subWordLength, theString
 
Constructor Summary
JTextComponentWordParser(javax.swing.text.JTextComponent component, boolean isHyphenDelim, boolean selectCurWord)
          Construct a JTextComponentWordParser to parse words from the contents of a JTextComponent.
 
Method Summary
 void deleteText(int numChars)
          Delete a specified number of characters from the text starting at the current cursor position.
 void highlightWord()
          Highlight the current word in the text area.
 void insertText(int pos, java.lang.String newText)
          Insert text at a specified position.
 void replaceWord(java.lang.String newWord)
          Replace the word at the current position with a new word.
 void updateText()
          Report that the contents of the TextArea have changed.
 
Methods inherited from class com.wintertree.ssce.StringWordParser
deleteWord, deleteWord, findWordStart, getCursor, getNumReplacements, getNumWords, getPrevWord, getWord, hasMoreElements, includeCharInWord, includeCharInWord, is1stWordChar, isDoubledWord, isFirstWord, isWordChar, nextElement, nextWord, setCursor, setText, setWordLength, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

component

protected javax.swing.text.JTextComponent component
The JTextComponent being parsed.

Constructor Detail

JTextComponentWordParser

public JTextComponentWordParser(javax.swing.text.JTextComponent component,
                                boolean isHyphenDelim,
                                boolean selectCurWord)
Construct a JTextComponentWordParser to parse words from the contents of a JTextComponent.

Parameters:
component - The JTextComponent to parse.
isHyphenDelim - true if a hyphen is to be considered a word delimiter, false if it's part of a word
selectCurWord - true if each word should be selected in the component to show context. Enabling this option degrades performance.
Method Detail

deleteText

public void deleteText(int numChars)
                throws java.util.NoSuchElementException
Delete a specified number of characters from the text starting at the current cursor position.

Parameters:
numChars - The number of characters to delete.
Throws:
java.util.NoSuchElementException - Attempt to delete beyond the end of the string.

highlightWord

public void highlightWord()
Highlight the current word in the text area.


insertText

public void insertText(int pos,
                       java.lang.String newText)
Insert text at a specified position.

Parameters:
pos - The position at which new text is to be inserted.
newText - The text to insert.

replaceWord

public void replaceWord(java.lang.String newWord)
                 throws java.util.NoSuchElementException
Replace the word at the current position with a new word.

Parameters:
newWord - The word to replace the word at the current position.
Throws:
java.util.NoSuchElementException - The cursor is positioned at the end of the string.

updateText

public void updateText()
Report that the contents of the TextArea have changed. This method must be called if the TextArea is edited by the user or updated by other software. Currently, it causes checking of the TextArea to restart. In future, it will call the 1.1 TextArea's getCaret method and check from that point.