/** Class that stores text along with a color in which to display it. */ class StyledWord { private StringBuffer text = new StringBuffer(); private Color color = new Color("black"); private String url; // text will link to this url public Color getColor() { return color; } public void setColor(Color color) { this.color = color; } public String getLink() { return url; } public void setLink(String url) { this.url = url; } public String getText() { return new String(text); } public void addLetter(char c, int position) { text.insert(position, c); } public void deleteLetter(int position) { text.delete(position, position+1); } } /** * Wraps a StyledWord (composition) with a check to see whether the text is a * word in the dictionary. If not, it is drawn in red. */ class SpellcheckedStyledWord { private Dictionary dictionary; private StyledWord word = new StyledWord(); public SpellcheckedStyledWord(String language) { dictionary = Dictionary.findDictionary(language); } public Color getColor() { return word.getColor(); } public void setColor(Color c) { word.setColor(c); } public String getLink() { return word.getLink(); } public void setLink(String url) { word.setLink(url); } public String getText() { return word.getText(); } public void addLetter(char c, int position) { word.addLetter(c,position); performSpellcheck(); } public void deleteLetter(int position) { word.deleteLetter(position); performSpellcheck(); } private void performSpellcheck() { if (dictionary.contains(word.getText())) { word.setColor(new Color("black")); } else { word.setColor(new Color("red")); } } } /** Wraps a StyledWord (composition) to automatically add links to URLs. */ class LinkedStyledWord { private StyledWord word = new StyledWord(); public Color getColor() { return word.getColor(); } public void setColor(Color c) { word.setColor(c); } public String getLink() { return word.getLink(); } public void setLink(String url) { word.setLink(url); } public String getText() { return word.getText(); } public void addLetter(char c, int position) { word.addLetter(c,position); updateLink(); } public void deleteLetter(int position) { word.deleteLetter(position); updateLink(); } private void updateLink() { String text = word.getText(); word.setLink(text.startsWith("http://") ? text : null); } }