/** 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");

  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); 
  }

  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 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 disallow adding 'Q's to the text. */
class NoQsStyledWord {
  private StyledWord word = new StyledWord();

  public Color getColor() {
    return word.getColor();
  }

  public void setColor(Color c) {
    word.setColor(c);
  }

  public String getText() {
    return word.getText();
  }

  public void addLetter(char c, int position) {
    if (c != 'Q') {
      word.addLetter(c,position);
    }
  }

  public void deleteLetter(int position) {
    word.deleteLetter(position);
  }
}