/** * Class that stores text along with a color in which to display it. Optionally * supports spell-checking the word or not allowing Qs in it. */ class StyledWord { private StringBuffer text = new StringBuffer(); private Color color = new Color("black"); private Spellchecker spellchecker = null; private QRemover qremover = null; public StyledWord() { } public StyledWord(Spellchecker c) { spellchecker = c; } public StyledWord(QRemover n) { qremover = n; } public StyledWord(Spellchecker c, QRemover n) { spellchecker = c; qremover = n; } public Color getColor() { return color; } public void setColor(Color c) { color = c; } public String getText() { return new String(text); } public void addLetter(char c, int position) { text.insert(position, c); afterWordChange(); } public void deleteLetter(int position) { text.delete(position, position+1); afterWordChange(); } private void afterWordChange() { if (spellchecker != null) { spellchecker.performSpellcheck(this); } if (qremover != null) { qremover.removeQs(this); } } } /** Removes any Qs that were added. */ class QRemover { public void removeQs(StyledWord word) { int i = word.getText().indexOf('Q'); if (i != -1) word.deleteLetter(i); // NOTE: this will make another call to removeQs! } } /** Changes the color of a word to red if it isn't found in the dictionary. */ class Spellchecker { private Dictionary dictionary; public Spellchecker(String language) { dictionary = Dictionary.findDictionary(language); } public void performSpellcheck(StyledWord word) { if (dictionary.contains(word.getText())) { word.setColor(new Color("black")); } else { word.setColor(new Color("red")); } } }