|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.jivesoftware.spark.component.spelling.BackgroundChecker
Monitor JTextComponents in the background for misspelled words. This class is part of the Sentry Java SDK examples.
By default, misspelled words are indicated to the user by a red zigzag underline. The color of the underline is customizable; the type of highlighting can be changed by providing an alternate HighlightPainter. Misspelled words are un-highlighted as they are corrected. BackgroundChecker also supports a popup menu that contains suggested replacements for misspelled words plus, optionally, "Ignore All" and "Add" items.
To monitor a JTextComponent for spelling errors, call the BackgroundChecker constructor and pass the component to be monitored. Note that the BackgroundChecker object must remain in scope as long as the monitored JTextComponent remains in scope. Background checking can be temporarily turned off by calling the stop method, and restarted via the resume method.
To display a popup menu over a misspelled word, catch awt.event.MouseEvents. When MouseEvent.isPopupTrigger returns true, call BackgroundChecker.isInMisspelledWord to determine if the popup menu was requested over a misspelled word. If so, call createPopupMenu to generate a popup menu appropriate for the misspelled word.
Nested Class Summary | |
protected class |
BackgroundChecker.ZigZagHighlightPainter
HighlightPainter that underlines highlighted text with a zig-zag line. |
Field Summary | |
protected boolean |
busy
True if we're processing an event. |
protected int |
caretPos
The current caret position. |
protected javax.swing.text.JTextComponent |
component
The text component being monitored. |
boolean |
debug
Set to true to enable diagnostics. |
protected javax.swing.text.Highlighter.HighlightPainter |
highlightPainter
Painter used to highlight misspelled words. |
protected java.util.Vector |
misspelledWords
Collection of misspelled words detected in the text component. |
protected com.wintertree.ssce.PropSpellingSession |
sentry
Sentry spelling session used to check spelling. |
protected com.wintertree.ssce.EditableLexicon |
userDictionary
The user dictionary words will be added to when the Add Word action is performed. |
Constructor Summary | |
BackgroundChecker(javax.swing.text.JTextComponent component,
com.wintertree.ssce.PropSpellingSession sentry)
Construct a BackgroundChecker to begin monitoring a text component. |
|
BackgroundChecker(javax.swing.text.JTextComponent component,
com.wintertree.ssce.PropSpellingSession sentry,
javax.swing.text.Highlighter.HighlightPainter highlightPainter)
Construct a BackgroundChecker to begin monitoring a text component. |
Method Summary | |
void |
addMisspelledWord(java.lang.String word,
int offset)
Indicate that a word in the text component's text is misspelled. |
void |
caretUpdate(javax.swing.event.CaretEvent ev)
Catch events relating to change in caret position in the monitored text component. |
void |
changedUpdate(javax.swing.event.DocumentEvent junk)
Required by the DocumentListener interface. |
boolean |
checkWord(java.lang.String word,
int offset)
Check the spelling of a word at a given offset in the text componet's text. |
javax.swing.JPopupMenu |
createPopupMenu(int x,
int y,
int maxSuggestions,
java.lang.String ignoreAllLabel,
java.lang.String addLabel,
java.lang.String noSuggestionsLabel)
Create a popup menu containing items related to the word under a specified point. |
boolean |
fillPopupMenu(javax.swing.JPopupMenu menu,
int x,
int y,
int maxSuggestions,
java.lang.String ignoreAllLabel,
java.lang.String addLabel,
java.lang.String noSuggestionsLabel)
Populate a popup menu with items related to the word under a specified point. |
protected com.jivesoftware.spark.component.spelling.BackgroundChecker.MisspelledWord |
findMisspelledWord(int offset)
Locate a misspelled word with which a specified offset intersects. |
protected com.jivesoftware.spark.component.spelling.BackgroundChecker.MisspelledWord |
findMisspelledWord(int start,
int end)
Locate a misspelled word which intersects with specified start and end offsets. |
protected int |
getWordAt(int offset,
java.lang.StringBuffer word)
Obtain the word in the text component's text at a given offset. |
java.awt.Color |
getZigZagColor()
Obtain the color used to mark misspelled words if the default zig-zag highlight painter is used. |
void |
insertUpdate(javax.swing.event.DocumentEvent ev)
Catch events reporting text insertion in the text component. |
protected boolean |
isInMisspelledWord(int offset)
Determine if an offset is within a known misspelled word. |
protected boolean |
isInMisspelledWord(int start,
int end)
Determine if a range is within a known misspelled word. |
boolean |
isInMisspelledWord(java.awt.Point pt)
Determine if a specified point is within a misspelled word. |
protected boolean |
isInWord(int x,
int y)
Determine if a specified point is within a word in the text component. |
protected boolean |
isWordChar(char c)
Determine if a character can be part of a word. |
protected void |
onInsertText(int offset,
int nChars)
Respond to insertion of multiple characters in the text component. |
void |
recheckAll()
Check or recheck the contents of the text component. |
protected void |
recheckAll(java.lang.String word)
Recheck all occurrences of a given word. |
void |
removeMisspelledWord(int offset)
Indicate that a word considered to be misspelled is not misspelled. |
void |
removeUpdate(javax.swing.event.DocumentEvent ev)
Catch events reporting text deletion in the text component. |
void |
resume()
Resume monitoring the text component after monitoring was stopped via the stop() method. |
void |
setUserDictionary(com.wintertree.ssce.EditableLexicon lex)
Specify which lexicon words should be added to when the Add Word action is performed. |
void |
setZigZagColor(java.awt.Color c)
Change the color used to underline misspelled words. |
void |
stop()
Stop monitoring the text component. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected boolean busy
protected javax.swing.text.JTextComponent component
protected int caretPos
protected java.util.Vector misspelledWords
public boolean debug
protected javax.swing.text.Highlighter.HighlightPainter highlightPainter
protected com.wintertree.ssce.PropSpellingSession sentry
protected com.wintertree.ssce.EditableLexicon userDictionary
Constructor Detail |
public BackgroundChecker(javax.swing.text.JTextComponent component, com.wintertree.ssce.PropSpellingSession sentry, javax.swing.text.Highlighter.HighlightPainter highlightPainter)
component
- The JTextComponent to monitorsentry
- The spelling session to use when checking spellinghighlightPainter
- The painter used to highlight misspelled words.
If null, an internal highlight painter which draws red zig-zag
lines below misspelled words is used. Method setZigZagColor can be
used to change the color of the zig-zag line.public BackgroundChecker(javax.swing.text.JTextComponent component, com.wintertree.ssce.PropSpellingSession sentry)
component
- The JTextComponent to monitorsentry
- The spelling session to use when checking spellingMethod Detail |
public void addMisspelledWord(java.lang.String word, int offset) throws javax.swing.text.BadLocationException
word
- The misspelled wordoffset
- Offset of the misspelled word in the text.
javax.swing.text.BadLocationException
- The supplied offset is outside the
range of text contained by the text component.public void caretUpdate(javax.swing.event.CaretEvent ev)
caretUpdate
in interface javax.swing.event.CaretListener
ev
- Information about the event.public void changedUpdate(javax.swing.event.DocumentEvent junk)
changedUpdate
in interface javax.swing.event.DocumentListener
junk
- Not used.public boolean checkWord(java.lang.String word, int offset)
word
- The word to checkoffset
- Offset of the word in the text component's text.
public javax.swing.JPopupMenu createPopupMenu(int x, int y, int maxSuggestions, java.lang.String ignoreAllLabel, java.lang.String addLabel, java.lang.String noSuggestionsLabel)
Selecting a suggested replacement invokes a ReplaceWordAction. Selecting the "Ignore All" item invokes an IgnoreAllAction. Selecting the "Add" item invokes an AddWordAction.
If the specified point is not over a word, null is returned.
Note that this method creates a popup menu whether the indicated word is misspelled or not. Use the isInMisspelledWord method to determine if the point is contained within a misspelled word.
x
- The x coordinate of a point within the word.y
- The y coordinate of a point within the word.maxSuggestions
- The maximum number of suggested replacement words
to display in the menu.ignoreAllLabel
- The text to associate with the Ignore All action
(typically "Ignore All"). If null, the Ignore All item will not appear
in the menu.addLabel
- The text to associate with the Add Word action
(typically "Add"). If null, the Add item will not appear in the menu.
The Add item will also not appear in the menu if the userDictionary
property is null.noSuggestionsLabel
- The text to place in a disabled menu item
if no suggestions could be found for the word (typically "(No
suggestions"). If null, no suggestion items will be added to the popup
menu if no suggestions could be found.
public boolean fillPopupMenu(javax.swing.JPopupMenu menu, int x, int y, int maxSuggestions, java.lang.String ignoreAllLabel, java.lang.String addLabel, java.lang.String noSuggestionsLabel)
Selecting a suggested replacement invokes a ReplaceWordAction. Selecting the "Ignore All" item invokes an IgnoreAllAction. Selecting the "Add" item invokes an AddWordAction.
If the specified point is not over a word, no items are added to the menu. In this situation, false is returned.
Note that this method adds items to the popup menu whether the indicated word is misspelled or not. Use the isInMisspelledWord method to determine if the point is contained within a misspelled word.
menu
- The popup menu to populate.x
- The x coordinate of a point within the word.y
- The y coordinate of a point within the word.maxSuggestions
- The maximum number of suggested replacement words
to display in the menu.ignoreAllLabel
- The text to associate with the Ignore All action
(typically "Ignore All"). If null, the Ignore All item will not appear
in the menu.addLabel
- The text to associated with the Add Word action
(typically "Add"). If null, the Add item will not appear in the menu.
The Add item will also not appear in the menu if the userDictionary
property is null.noSuggestionsLabel
- The text to place in a disabled menu item
if no suggestions could be found for the word (typically "(No
suggestions"). If null, no suggestion items will be added to the popup
menu if no suggestions could be found.
public java.awt.Color getZigZagColor()
public void insertUpdate(javax.swing.event.DocumentEvent ev)
insertUpdate
in interface javax.swing.event.DocumentListener
ev
- Information about the event.public boolean isInMisspelledWord(java.awt.Point pt)
pt
- The point to test.
public void recheckAll()
public void removeMisspelledWord(int offset)
offset
- Offset of the word in the text.public void removeUpdate(javax.swing.event.DocumentEvent ev)
removeUpdate
in interface javax.swing.event.DocumentListener
ev
- Information about the event.public void resume()
public void setUserDictionary(com.wintertree.ssce.EditableLexicon lex)
lex
- The user dictionary words will be added to. If null, adding
of words will be disabled.public void setZigZagColor(java.awt.Color c)
c
- The color to use when drawing zig-zag lines below misspelled
words.public void stop()
protected com.jivesoftware.spark.component.spelling.BackgroundChecker.MisspelledWord findMisspelledWord(int start, int end)
start
- The offset of the beginning of the range from the
start of the text component's text.end
- The offset of the end of the range from the start of the
text component's text.
protected com.jivesoftware.spark.component.spelling.BackgroundChecker.MisspelledWord findMisspelledWord(int offset)
offset
- The offset from the beginning of the text component's
text where the misspelled word is located.
protected int getWordAt(int offset, java.lang.StringBuffer word)
offset
- The offset in the text component's text where the word
should be located.word
- The word obtained from the text.
protected boolean isInMisspelledWord(int start, int end)
start
- The offset of the beginning of the range from the
start of the text component's text.end
- The offset of the end of the range from the start of the
text component's text.
protected boolean isInMisspelledWord(int offset)
offset
- The offset to test from the start of the text component's
text.
protected boolean isInWord(int x, int y)
x
- The x coordinate to test.y
- The y coordinate to test.
protected boolean isWordChar(char c)
c
- The character to test.
protected void onInsertText(int offset, int nChars)
offset
- The offset of the first character inserted from the start
of text in the component.nChars
- The number of characters inserted.protected void recheckAll(java.lang.String word)
word
- The word to recheck.
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |