/* @(#)PhraseMaker.java  */
import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.regex.*;

// import java.text.*;

public class PhraseMaker
    extends DisplayPanel
{
    public static final String MAKE_PHRASE = "make.phrase";
    
    
    private static GraphicsEnvironment ge =
        GraphicsEnvironment.getLocalGraphicsEnvironment();
        
    private static String[] familyList =
        ge.getAvailableFontFamilyNames();
    
    private static String[] sizeList =
        { "8", "9", "10", "11", "12", "14", "16", "18", "20",
          "22", "24", "28", "32", "36" };
    
    private SimpleAction setTextFieldState = new SimpleAction() {
        public void perform() { setTextFieldState(); }
    };
    
    
    private DropdownView fontFamily =
        new DropdownView(familyList, "Times");
    
    
    private DropdownView fontSize =
        new DropdownView(sizeList, "14", 30, true);
    
    
    private BooleanView bold =
        new BooleanView("Bold", false);
    
    private BooleanView italic =
        new BooleanView("Italic", false);
    
    private ColorView foreColor = new ColorView(Colors.black, true);
    
    private ColorView backColor = new ColorView(Colors.white, true);
    
    
    private TextFieldView phrase =
        new TextFieldView("", 600);
    
    
    private TextPaintable phrasePaintable = null;
    
    
    private SimpleAction makePhrase =
        new SimpleAction("Make Phrase") {
            public void perform() {
                makePhrase();
            }
        };
    
    
    private TablePanel phrasePanel =
        new TablePanel(
            new Object[] { "Phrase", phrase },
            HORIZONTAL, 20, 20, CENTER);
    
    private TablePanel familyPanel =
        new TablePanel(
            new Object[] { "Font",   fontFamily },
            HORIZONTAL, 20, 20, CENTER);
    
    private TablePanel paramsPanel =
        new TablePanel(
            new Object[] { "Size", fontSize, bold, italic },
            HORIZONTAL, 20, 20, CENTER);
    
    private TablePanel foreColorPanel =
        new TablePanel(
            new Object[] { "Foreground Color", foreColor },
            HORIZONTAL, 20, 20, CENTER);
    
    private TablePanel backColorPanel =
        new TablePanel(
            new Object[] { "Background Color", backColor },
            HORIZONTAL, 20, 20, CENTER);
    
    private TablePanel actions =
        new TablePanel(
            new Object[] { makePhrase },
            HORIZONTAL, 5, 5, CENTER);
    
    private TablePanel mainPanel =
        new TablePanel(
            new Object[] {
                phrasePanel,
                familyPanel,
                paramsPanel,
                foreColorPanel,
                backColorPanel,
                actions },
            VERTICAL, 20, 20, CENTER);
    
    
    public PhraseMaker() {
        add(mainPanel);
        addActions();
        setTextFieldState();
    }
    
    
    public TextPaintable getCurrentPhrase() {
        return phrasePaintable;
    }
    
    
    public String getCurrentPhraseAsText() {
        return phrase.getText();
    }
    
    
    public Font readFontFromGUI() {
        String name = fontFamily.getViewState();
        
        int size = fontSize.demandInt();
        
        int style =
            (bold.  getBooleanValue() ? Font.BOLD   : 0) |
            (italic.getBooleanValue() ? Font.ITALIC : 0);
            
        return new Font(name, style, size);
    }
    
    
    public Color getForegroundColor() {
        return foreColor.getColor();
    }
    
    
    public Color getBackgroundColor() {
        return backColor.getColor();
    }
    
    
    private void makePhrase() {
        phrasePaintable = new TextPaintable
            (phrase.getText(),
             readFontFromGUI(),
             foreColor.getColor(),
             TextBounds.TIGHT,
             TextAnchor.CENTER_BASELINE,
             0, 0);
        
        // alert listeners that TextPaintable
        // has been created
        firePropertyChange(MAKE_PHRASE, 0, 1);
    }
    
    
    private void setTextFieldState() {
        phrase.setFont(readFontFromGUI());
        phrase.setForeground(foreColor.getColor());
        phrase.setBackground(backColor.getColor());
    }
    
    
    private void addActions() {
        // execute makePhrase if the user presses return
        // in the phrase text field
        phrase.addActionListener(makePhrase);
        
        // reflect changes in GUI settings immediately
        // in the phrase text field
        fontFamily.addActionListener(setTextFieldState);
        fontSize.addActionListener(setTextFieldState);
        bold.addActionListener(setTextFieldState);
        italic.addActionListener(setTextFieldState);
        
        foreColor.addAction(setTextFieldState);
        backColor.addAction(setTextFieldState);
    }
}
