import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;

public class TestUnweightedGraph {
    JFrame theFrame;
    JLabel filePrompt;
    JLabel startPrompt;
    JTextField fileField;
    JTextField startField;
    JPanel filePanel;
    JPanel pathsPanel;
    JScrollPane filePane;
    JScrollPane pathsPane;
    JButton graphButton;
    JButton pathsButton;
    JPanel buttonPanel;
    JTextArea outArea;
    JPanel outPanel;
    JScrollPane outPane;
    UnweightedGraph g;
    
    public TestUnweightedGraph() {
        theFrame = new JFrame("Unweighted Graph Example");
        theFrame.setSize(800,700);
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theFrame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,150,5));
        filePrompt = new JLabel("Enter the graph file name >");
        fileField = new JTextField("SampleGraph.data",15);
        startPrompt = new JLabel("Enter the start vertex >");
        startField = new JTextField("0",2);
        graphButton = new JButton("Compute Graph");
        pathsButton = new JButton("Display shortest paths from start vertex");
        filePanel = new JPanel();
        filePanel.add(filePrompt);
        filePanel.add(fileField);
        filePanel.add(graphButton);
        filePane = new JScrollPane(filePanel);
        pathsPanel = new JPanel();
        pathsPanel.add(startPrompt);
        pathsPanel.add(startField);
        pathsPanel.add(pathsButton);
        pathsPane = new JScrollPane(pathsPanel);
        outArea = new JTextArea(30,100);
        outArea.setFont(new Font("Courier",Font.BOLD,12));
        outPanel = new JPanel();
        outPanel.add(outArea);
        outPane = new JScrollPane(outPanel);
        theFrame.add(filePane);
        theFrame.add(pathsPane);
        theFrame.add(outPane);
        
        graphButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Scanner in;
                try {
                    String fileName = fileField.getText();
                    in = new Scanner(new File(fileName));
                    g = new UnweightedGraph(true);
                    while( in.hasNext() ) {
                        int vertex1 = in.nextInt();
                        int vertex2 = in.nextInt();
                        g.addEdge(vertex1, vertex2);
                    } // end while
                    outArea.setText(g+"\n");
                } catch(Exception exp) {
                    outArea.setText(exp.toString());
                } // end try-catch
            } // end actionPerformed
        });
        
        pathsButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int startVertex = Integer.parseInt(startField.getText());
                    outArea.setText(g+"\n");
                    outArea.append("A depth first search listing from " + startVertex + " is:\n");
                    outArea.append(g.depthFirstSearch(startVertex)+"\n");
                    outArea.append("A breadth first search listing from " + startVertex + " is:\n");
                    outArea.append(g.breadthFirstSearch(startVertex)+"\n");
                    g.allShortestPaths(startVertex);
                    for( int i = 0; i < g.numberOfVertices(); i++ ) {
                        outArea.append("A shortest path from "+ startVertex + " to "+ i + " is:\n");
                        outArea.append(g.shortestPath(i)+"\n");
                        System.out.println(g.shortestPathArray(i));
                    } // end for
                    startField.requestFocus();
                } catch(Exception exp) {
                    outArea.setText(exp.toString());
                } // end try-catch
            } // end actionPerformed
        });
        
    } // end constructor
    
    
    public static void main(String[] s) throws IOException {
        new TestUnweightedGraph().theFrame.setVisible(true);
    } // end main
    
}  // TestUnweightedGraph

