/*---------------------------------------------------------------------------
File:         			MultipleInputTwoTapeWindow.java
Package:                        JFLAP Version 3.0
Author:			        Eric Gramond 
                                
Description of Contents:	Contains class MultipleInputTwoTapeWindow.
				
--------------------------------------------------------------------------*/

/* 
 * Susan H. Rodger, Magda Procopiuc, Octavian Procopiuc, Eric Gramond
 * Computer Science Department
 * Duke University
 * June 1998
 * Supported by National Science Foundation DUE-9596002 and DUE-9555084.
 *
 * Copyright (c) 1998
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by the author.  The name of the author may not be used to
 * endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */


package flap;

import java.awt.*;
import java.util.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;


/**
 * variant on the multiple input window. It has two input fields.
 * for TM2's
 */

public class  MultipleInputTwoTapeWindow extends MultipleInputWindow {

    protected Vector mySecondInputFields;

    /**
     * default constructor
     */

     public MultipleInputTwoTapeWindow(Environment parent, Color background) {
	 super(parent, background);
     }



      protected void buildWindow() {
	  mySecondInputFields = new Vector();
	TextField t, t2;
	Label l;
	ScrollPane topScroll = new ScrollPane();
	topScroll.setSize(310, 450);
	add(topScroll, "North");
       
	myScrollPanel = new Panel();
	myScrollPanel.setLayout(myGrid = new GridLayout(myNumRows+1,1));
	Panel p = new Panel(new FlowLayout());
	int j = 0;
        p.add(new Label("         Input1"));
	p.add(new Label("          Input2"));
	p.add(new Label("      Result"));
	myScrollPanel.add(p, j++);
	for(int i = 0; i < myNumRows; i++) {
	    t = new TextField(10);
	    t2 = new TextField(10);
	    l = new Label("            ");
	    myInputFields.addElement(t);
	    mySecondInputFields.addElement(t2);
	    myLabels.addElement(l);
	    p = new Panel(new FlowLayout());
	    p.add(t);
	    p.add(t2);
	    p.add(l);
	    myScrollPanel.add(p, j++);
	}
	topScroll.add(myScrollPanel);
    }

    
    /**
     * call stepRun on the highLighted textArea's input
     */
    protected void stepRun() {
 
	if(this.getFocusOwner() instanceof TextField) {
	    String aString = myParent.inputField.getText();
	    String bString = myParent.inputField2.getText();
	    int index;
	    
	    if(myInputFields.indexOf(this.getFocusOwner()) > 0) 
		index = myInputFields.indexOf(this.getFocusOwner());
	    else 
		index = mySecondInputFields.indexOf(this.getFocusOwner());
	  
	    myParent.inputField.setText(((TextField) myInputFields.elementAt(index)).getText());
	    myParent.inputField2.setText(((TextField) mySecondInputFields.elementAt(index)).getText());
	    myParent.stepRun();
	    myParent.inputField.setText(aString);
	    myParent.inputField2.setText(bString);
	}
    }

     /** 
      * add 4 more inputs to the window if 10 wasn't enough...
      */
    protected void addInputs() {
	TextField t, t2;
	Label l;
	myNumRows += 4;
	Panel p;
	myGrid.setRows(myNumRows+1);
	for(int i = 0; i < 4; i++) {
	    t = new TextField(10);
	    t2 = new TextField(10);
	    l = new Label("          ");
	    p = new Panel(new FlowLayout());
	    myInputFields.addElement(t);
	    mySecondInputFields.addElement(t);
	    myLabels.addElement(l);
	    p.add(t);
	    p.add(t2);
	    p.add(l);
	    myScrollPanel.add(p);
	}
	myScrollPanel.validate();
	pack();
    }

    /**
     * clear all input fields and labels
     */
    protected void clear() {
	for(int i=0; i < myNumRows; i++) {
	    ((Label) myLabels.elementAt(i)).setText("");
	    ((TextField) myInputFields.elementAt(i)).setText("");
	    ((TextField) mySecondInputFields.elementAt(i)).setText("");
	}
    }
  
    /**
     * test all the inputs. The ultimate purpose of the window
     */
    public void testInputs() {	
	String	error = myParent.a.checkMachine();
	String input, input2;
	Label theLabel;
	TextDialog dg;
	String pushed;
	String[] str = {"Yes", "No"};

	myParent.setEnabled(false);

	if(error!= null) {
	    OKDialog ok = new OKDialog(error, this, "Error");
	    ok.findResult();
	    return;
	}

	for(int i = 0; i<myNumRows; i++) {
	    theLabel = (Label) myLabels.elementAt(i);    
	    input = ((TextField) myInputFields.elementAt(i)).getText();
	    input2 = ((TextField) mySecondInputFields.elementAt(i)).getText();
	    input = input + " | " + input2;
	    myParent.a.setInput(input);
	    
	    myParent.a.init();
	    myParent.a.d.initialState.ss = State.NORMAL;
	loop:	while (true) {
	    switch (myParent.a.fastRun()) {
	    case Machine.NO_FINAL_STATE_MSG:
		theLabel.setText("Rejected");
		theLabel.setForeground(Color.red);
		break loop;
	    case Machine.MAXNCONFIGS_MSG:
		if (myParent.a.maxNConfigs < myParent.a.capacity) {
		    dg = new TextDialog("More than "+myParent.a.maxNConfigs
			     +" configurations\n have been generated and the input is\n"
			     +"not accepted yet. Continue?", str,
			     "Input " + i + " warning"); 
		    pushed = dg.findResult();
		    if (pushed.equals("Yes")) 
			myParent.a.maxNConfigs *= 2;   
		    if(pushed.equals("No")) {
			theLabel.setText("Overflow");
			theLabel.setForeground(Color.black);
			break loop;	
		    }
		} else {
		    theLabel.setText("Overflow");
		    theLabel.setForeground(Color.black);
		    break loop;
		}
		break; 
	    case Machine.ACCEPTED_MSG:
		theLabel.setText("Accepted");   
		theLabel.setForeground(Color.blue);
		break loop;
	    case Machine.NO_MORE_CONFIGS_MSG:
		theLabel.setText("Rejected");
		theLabel.setForeground(Color.red);
		break loop;
	    }  // end switch
	} // end while
	    myParent.a.d.repaint();	
	}
	myParent.setEnabled(true);
    }
}
