import java.awt.*;
import java.awt.event.*;

/**
 * IOFrame provides simple (single-line) text input and
 * multiple-line output.
 * 
 * @author David Matuszek 
 * @version September 18, 2003
 */
public class IOFrame extends Frame {
    private Label label = new Label("Input");
    private TextField input = new TextField();
    private TextArea output = new TextArea(15, 60);
    private Panel inputPanel = new Panel();
    private String inputString = "";
    
    /**
     * Constructor for a Frame with the given title.
     * 
     * @param title The title for this Frame.
     * @return The new Frame.
     */
    IOFrame(String title) {
        setTitle(title);
//       setFont(new Font("Verdana", 14, Font.PLAIN));
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                exit();
            }});
        
        inputPanel.setLayout(new BorderLayout());
        inputPanel.add(label, BorderLayout.NORTH);
        inputPanel.add(input, BorderLayout.SOUTH);
        
        add(inputPanel, BorderLayout.NORTH);
        add(output, BorderLayout.CENTER);
        
        input.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                inputString = input.getText();
            }});
        pack();
        setVisible(true);
    }
    
   /**
    * Displays the given String as a prompt, and waits for a response.
    * 
    * @param prompt A String to be used to request input from the user.
    * @return The user's response.
    */
   public String getString(String prompt) {
        label.setText(prompt);
        input.setText("");
        inputString = "";
        while ("".equals(inputString)) {
            try { Thread.sleep(50); }
            catch (InterruptedException e) {}
        }
        return inputString;
    }
    
   /**
    * Displays the given String as a prompt, and waits for a response
    * that can be interpreted as an integer. The user is re-prompted
    * with an error message until an int is provided.
    * 
    * @param prompt A String to be used to request input from the user.
    * @return The user's response.
    */
   public int getInt(String prompt) {
        String s = "";
        try {
            s = getString(prompt);
            return Integer.valueOf(s).intValue();
        }
        catch (NumberFormatException e) {
            return getInt("Not an integer:  \"" + s + "\"   Try again.");
        }
    }
        
   /**
    * Displays the given String as a prompt, and waits for a response
    * that can be interpreted as a double. The user is re-prompted
    * with an error message until a double is provided.
    * 
    * @param prompt A String to be used to request input from the user.
    * @return The user's response.
    */
    public double getDouble(String prompt) {
        String s = "";
        try {
            s = getString(prompt);
            return Double.valueOf(s).doubleValue();
        }
        catch (NumberFormatException e) {
            return getDouble("Not a double:  \"" + s + "\"   Try again.");
        }
    }
    
    /**
     * Displays a message in the output area of this Frame.
     * 
     * @param message The message to be displayed.
     */
    public void display(String message) {
        output.append(message);
    }
    
    /**
     * Displays a message in the output area of this Frame, and
     * ends the current line.
     * 
     * @param message The message to be displayed.
     */
    public void displayLine(String message) {
        display(message + '\n');
    }
    
    /**
     * Clears the output area.
     */
    public void clear() {
        output.setText("");
    }
    
    /**
     * Closes the output area and exits the program.
     */
    public void exit() {
        dispose();
        System.exit(0);
    }
}
