Initial revision
[old-projects.git] / ekit / com / swabunga / spell / event / BasicSpellCheckEvent.java
1 package com.swabunga.spell.event;
2
3 import java.util.*;
4
5 /** This event is fired off by the SpellChecker and is passed to the
6 * registered SpellCheckListeners
7 *
8 * @author Jason Height (jheight@chariot.net.au)
9 */
10 class BasicSpellCheckEvent implements SpellCheckEvent {
11 /**The list holding the suggested Word objects for the misspelt word*/
12 private List suggestions;
13 /**The misspelt word*/
14 private String invalidWord;
15 /**The action to be done when the event returns*/
16 private short action = INITIAL;
17 /**Contains the word to be replaced if the action is REPLACE or REPLACEALL*/
18 private String replaceWord = null;
19
20 private String context;
21 private int startPosition;
22
23
24 /**Consructs the SpellCheckEvent
25 * @param String invalidWord The word that is misspelt
26 * @param List suggestions A list of Word objects that are suggested to replace the currently mispelt word
27 * @param WordTokenizer tokenizer The reference to the tokenizer that caused this
28 * event to fire.
29 */
30 public BasicSpellCheckEvent(String invalidWord, List suggestions, WordTokenizer tokenizer) {
31 this.invalidWord = invalidWord;
32 this.suggestions = suggestions;
33 this.context = tokenizer.getContext();
34 this.startPosition = tokenizer.getCurrentWordPosition();
35 }
36
37 /** Returns the list of suggested Word objects*/
38 public List getSuggestions() {
39 return suggestions;
40 }
41
42 /** Returns the currently misspelt word*/
43 public String getInvalidWord() {
44 return invalidWord;
45 }
46
47 public String getWordContext() {
48 //JMH TBD
49 return null;
50 }
51
52 /** Returns the start position of the misspelt word in the context*/
53 public int getWordContextPosition() {
54 return startPosition;
55 }
56
57 public short getAction() {
58 return action;
59 }
60
61 public String getReplaceWord() {
62 return replaceWord;
63 }
64
65 /** Set the action to replace the currently misspelt word with the new word
66 * @param String newWord The word to replace the currently misspelt word
67 * @param boolean replaceAll If set to true, the SpellChecker will replace all
68 * further occurances of the misspelt word without firing a SpellCheckEvent.
69 */
70 public void replaceWord(String newWord, boolean replaceAll) {
71 if (action != INITIAL)
72 throw new IllegalStateException("The action can can only be set once");
73 if (replaceAll)
74 action = REPLACEALL;
75 else action = REPLACE;
76 replaceWord = newWord;
77 }
78
79 /** Set the action it ignore the currently misspelt word.
80 * @param boolean ignoreAll If set to true, the SpellChecker will replace all
81 * further occurances of the misspelt word without firing a SpellCheckEvent.
82 */
83 public void ignoreWord(boolean ignoreAll) {
84 if (action != INITIAL)
85 throw new IllegalStateException("The action can can only be set once");
86 if (ignoreAll)
87 action = IGNOREALL;
88 else action = IGNORE;
89 }
90
91 /** Set the action to add a new word into the dictionary. This will also replace the
92 * currently misspelt word.
93 */
94 public void addToDictionary(String newWord) {
95 if (action != INITIAL)
96 throw new IllegalStateException("The action can can only be set once");
97 action = ADDTODICT;
98 replaceWord = newWord;
99 }
100
101 public void cancel() {
102 if (action != INITIAL)
103 throw new IllegalStateException("The action can can only be set once");
104 action = CANCEL;
105 }
106 }