/** Low Chee Tat 1001150277 **/

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 * Applet class.
 */
public class NFA extends Applet
{
	//Declare variable
	public String noOfStatesTemp;
	public String inputAlphabet;
    public String finalState;
	public int noOfStates;
	public int noOfInputAlphabet;
	public char[] getInputAlphabet;
	public String[][] nextStateValue;
	public String[][] Q;
	
	/**
	 * This method is called when the applet is created.
	 */
	public void init()
	{
		// Set initial window properties
		setSize(800,600);
		setBackground(Color.PINK);
		setLayout(new GridLayout(1,3));
		
		// Create panel
		JPanel groupAllButtons = new JPanel(new GridLayout(0,1));
		
		// Create label
		//JLabel programTitle = new JLabel("Nondeterministic Finite Automata");
		
		// Create button.
		JButton askNoOfStates = new JButton("State");
		JButton askInputAlphabet = new JButton("Input Alphabet");
		JButton askRelationship = new JButton("Relationship");
		JButton askForFun = new JButton("Check");
		JButton askFinalState = new JButton("Final state");
		JButton askWords = new JButton("Words");
		JButton showTransitionTable = new JButton("Transition Table");
		
		
		// Set panel's properties
		groupAllButtons.setBackground(Color.magenta);
		
		// Set label's properties
		//programTitle.setFont(new Font("TimeNewRoman", Font.BOLD, 40));
		//programTitle.setHorizontalAlignment(SwingConstants.CENTER);

		
		// Set button's properties
		
		askNoOfStates.setFont(new Font("SansSerif", Font.BOLD, 20));
		askInputAlphabet.setFont(new Font("SansSerif", Font.BOLD, 20));
		askRelationship.setFont(new Font("SansSerif", Font.BOLD, 20));
		askForFun.setFont(new Font("SansSerif", Font.BOLD, 20));
		askFinalState.setFont(new Font("SansSerif", Font.BOLD, 20));
		showTransitionTable.setFont(new Font("SansSerif", Font.BOLD, 20));
		
		
		
		// Add button to applet.
		groupAllButtons.add(askNoOfStates);
		groupAllButtons.add(askInputAlphabet);
		groupAllButtons.add(askRelationship);
		groupAllButtons.add(askForFun);
		groupAllButtons.add(askFinalState);
		groupAllButtons.add(showTransitionTable);
		
		//this.add(programTitle,"North");
		//this.add(groupAllButtons,"Center");
		
		this.add(new JLabel());
		this.add(groupAllButtons);
		this.add(new JLabel());
		
		
		
		
		// Add button listener.
		askNoOfStates.addActionListener
		(
			new ActionListener()
			{
				/**
				 * Called when button was pressed.
				 */
				public void actionPerformed(ActionEvent e)
				{
					noOfStatesTemp = JOptionPane.showInputDialog("Enter number of states:");
					
					noOfStates = Integer.parseInt(noOfStatesTemp);
									
					NFA.this.buttonPressed();
				}
			}
		);
	
		
		askInputAlphabet.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					inputAlphabet = JOptionPane.showInputDialog("Enter input alphabet:");
					
					noOfInputAlphabet = inputAlphabet.length();
					
					getInputAlphabet = inputAlphabet.toCharArray();
				}
			}
		);
	
		
		askForFun.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					askForFunFunc();
				}
			}
		);
		
		
		askFinalState.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					finalState = JOptionPane.showInputDialog("Enter final state:");
				}
			}
		);
					
	
	
		showTransitionTable.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					displayTransitionTable();
				}
			}
		);
	
		askRelationship.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					relationshipAmongStates();
				}
			}
		 );
	}
	
		
	public void displayTransitionTable()
	{
		//declare variables
		int j=0;
		int k=1;
		int q=1;
		int r=1;
		
		// Create a new frame
		final Frame toShowTransitionTable = new Frame("Transition Table");
		
		
		// Set frame's properties
		toShowTransitionTable.setSize(400,400);
		toShowTransitionTable.setLayout(new FlowLayout());
		
		
		// Create a table
		JTable transitionTable = new JTable((noOfStates+1),(noOfInputAlphabet+2));
		
		// Create a button to hide the frame window
		JButton hide = new JButton("Hide");
		
		// Define the “hide” behaviour
		hide.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					toShowTransitionTable.hide();
				}
			}
		);
		

		// Build the transition table
		
		transitionTable.setValueAt("Input", 0, 0);
		
		for(int i=0; i<noOfInputAlphabet; i++)
		{
			transitionTable.setValueAt(Character.toString(getInputAlphabet[i]), 0, ++j);
		}
			
		transitionTable.setValueAt("v", 0, (noOfInputAlphabet+1));
		
		for(int i=0; i<noOfStates ; i++)
		{
			transitionTable.setValueAt("Q"+i,k++,0);
		}
	
		for(int m=0;m<noOfStates;m++)
		{
			for(int n=0;n<=noOfInputAlphabet;n++)
			{
				transitionTable.setValueAt(nextStateValue[m][n],r,q++);
				
			}
			q=1;
			r++;
		}
		
		// Add all the components to the window
		toShowTransitionTable.add(transitionTable);
		toShowTransitionTable.add(hide);
		toShowTransitionTable.show();
	
	}
	
	// To acquire info on r/ship among each states
	public void relationshipAmongStates()
	{
		
		int k;
		int noOfInputAlphabetTemp;
		
		noOfInputAlphabetTemp=noOfInputAlphabet+1;
		nextStateValue = new String[noOfStates][noOfInputAlphabetTemp];
		
		for(int i=0;i<noOfStates;i++)
		{
			k=0;
			for(int j=0;j<noOfInputAlphabet;j++)
			{
				nextStateValue[i][j] = JOptionPane.showInputDialog("Q"+i+":If "+getInputAlphabet[j]+", Next State:?(eg Q and no.)");
				k=j;
			}
			nextStateValue[i][++k] = JOptionPane.showInputDialog("Q"+i+":If v, Next State:?(eg Q and no.)");
		}
			
	}
	
	public void askForFunFunc()
	{
		Q = new String[noOfStates][noOfStates];
				
		for(int i=0;i<noOfStates;i++)
		{
			for(int j=0;j<noOfStates;j++)
			{
				// To store alphabet in Q[i][j]
				Q[i][j] = JOptionPane.showInputDialog("Q"+i+" to "+"Q"+j);
			}
		}
	}
	
	
	/**
	 * Called when the button was pressed.
	 */
	protected void buttonPressed()
	{
		// Beep.
		Toolkit.getDefaultToolkit().beep();
	}
}


