/*
 * TestIO.java
 *
 * Created on March 8, 2002, 3:59 PM
 */

/**
 *
 * @author  David Matuszek
 */
public class TestIO extends javax.swing.JFrame {

    
    /** Creates new form TestIO */
    public TestIO() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        panel4 = new java.awt.Panel();
        textField1 = new java.awt.TextField();
        textField2 = new java.awt.TextField();
        textField3 = new java.awt.TextField();
        
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        
        panel4.setLayout(new java.awt.GridLayout(3, 1));
        
        textField1.setText("Questions go here");
        textField1.setEditable(false);
        textField1.setEnabled(false);
        panel4.add(textField1);
        
        textField2.setText("User types answer here");
        textField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField2ActionPerformed(evt);
            }
        });
        
        panel4.add(textField2);
        
        textField3.setText("Program displays user's answer here");
        textField3.setEditable(false);
        textField3.setEnabled(false);
        panel4.add(textField3);
        
        getContentPane().add(panel4, java.awt.BorderLayout.CENTER);
        
        pack();
    }//GEN-END:initComponents

    private void textField2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField2ActionPerformed
        // Add your handling code here:
        String input = textField2.getText();
        provideInput(input);
    }//GEN-LAST:event_textField2ActionPerformed

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        TestIO myTest = new TestIO();
        myTest.show();
        myTest.converse();
    }
    
    public void converse() {
        String s;
        textField1.setText("Type something in the field below:");
        s = waitForInput();
        textField3.setText("Here's what I read: " + s);
        textField1.setText("Try it again:");
        s = waitForInput();
        textField3.setText("Here's what I read: " + s);
    }

    public boolean available = false;
    public String inputText;
    
    public synchronized String waitForInput() {
        while (! available) {
            try { wait(); }
            catch (InterruptedException e) {}
        }
        available = false;
        notifyAll();
        return inputText;
    }
    
    public synchronized void provideInput(String theInput) {
        while (available) {
            try { wait(); }
            catch (InterruptedException e) {}
        }
        inputText = theInput;
        available = true;
        notifyAll();
    }
        
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Panel panel4;
    private java.awt.TextField textField1;
    private java.awt.TextField textField2;
    private java.awt.TextField textField3;
    // End of variables declaration//GEN-END:variables

}
