1 /** 2 * Class that stores text along with a color in which to display it. Optionally 3 * supports spell-checking the word and url highlighting. 4 */ 5 class StyledWord { 6 private StringBuffer text = new StringBuffer(); 7 private Color color = new Color("black"); 8 private String url; // text will link to this url 9 10 private Spellchecker spellchecker = null; 11 private LinkUpdater linkUpdater = null; 12 13 public StyledWord() { 14 } 15 16 public StyledWord(Spellchecker c) { 17 spellchecker = c; 18 } 19 20 public StyledWord(LinkUpdater u) { 21 linkUpdater = u; 22 } 23 24 public StyledWord(Spellchecker c, LinkUpdater u) { 25 spellchecker = c; 26 linkUpdater = u; 27 } 28 29 public Color getColor() { 30 return color; 31 } 32 33 public void setColor(Color c) { 34 color = c; 35 } 36 37 public String getLink() { 38 return url; 39 } 40 41 public void setLink(String url) { 42 this.url = url; 43 } 44 45 public String getText() { 46 return new String(text); 47 } 48 49 public void addLetter(char c, int position) { 50 text.insert(position, c); 51 afterWordChange(); 52 } 53 54 public void deleteLetter(int position) { 55 text.delete(position, position+1); 56 afterWordChange(); 57 } 58 59 private void afterWordChange() { 60 if (spellchecker != null) { 61 spellchecker.performSpellcheck(this); 62 } 63 if (linkUpdater != null) { 64 linkUpdater.updateLink(this); 65 } 66 } 67 } 68 69 /** Makes text that looks like a URL into a link. */ 70 class LinkUpdater { 71 public void updateLink(StyledWord word) { 72 String text = word.getText(); 73 word.setLink(text.startsWith("http://") ? text : null); 74 } 75 } 76 77 /** Changes the color of a word to red if it isn't found in the dictionary. */ 78 class Spellchecker { 79 private Dictionary dictionary; 80 81 public Spellchecker(String language) { 82 dictionary = Dictionary.findDictionary(language); 83 } 84 85 public void performSpellcheck(StyledWord word) { 86 if (dictionary.contains(word.getText())) { 87 word.setColor(new Color("black")); 88 } else { 89 word.setColor(new Color("red")); 90 } 91 } 92 } 93