//StudentMarks6.java
//Loads student names and marks from a textfile chosen by the user and
//displays them in a JTextArea component on the screen.
//Extends StudentMarks5.java by activating the "Sort by Mark" button to
//permit the user to sort the displayed names and marks by descending mark.

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; //an "interface"
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

//New interface used:
import java.util.Comparator; //another "interface"

public class StudentMarks6 extends JFrame
{
    private ArrayList<String> studentList;
    private JTextArea display;
    private JPanel buttonPanel;
    private JButton fileButton;
    private JButton byNameButton;
    private JButton byMarkButton;
    private JButton quitButton;
    private Font font;

    public StudentMarks6(String title)
    {
        super(title);

        studentList = new ArrayList<String>();

        display = new JTextArea(15, 30);
        font = new Font("Monospaced", Font.PLAIN, 16);
        display.setFont(font);

        buttonPanel = new JPanel();
        fileButton = new JButton("Load File");
        byNameButton = new JButton("Sort by Name");
        byMarkButton = new JButton("Sort by Mark");
        quitButton = new JButton("Quit");

        ButtonHandler buttonHandler = new ButtonHandler();
        quitButton.addActionListener(buttonHandler);
        fileButton.addActionListener(buttonHandler);
        byNameButton.addActionListener(buttonHandler);

        //New code to connect the button handler to the Sort by Mark button:
        byMarkButton.addActionListener(buttonHandler);

        buttonPanel.add(fileButton);
        buttonPanel.add(byNameButton);
        buttonPanel.add(byMarkButton);
        buttonPanel.add(quitButton);

        add(new JScrollPane(display), BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    //Note that this method now has a parameter:
    public void getDataFromFile(String fileName)
    {
        display.setText("");
        studentList.clear();
        try
        {
            Scanner in = new Scanner(new File(fileName));
            String line;
            while (in.hasNext())
            {
                line = in.nextLine();
                display.append(line + "\n");

                //New: Student name and mark added to studentList.
                studentList.add(line);
            }
            in.close();
            display.setCaretPosition(0);
        }
        catch (FileNotFoundException e)
        {
            JOptionPane.showMessageDialog(null,
                "File not found.\nTry again.");
        }
    }

    class ButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == quitButton)
                System.exit(0);

            if (e.getSource() == fileButton)
            {
                String fileName = JOptionPane.showInputDialog(
                    "What is the name of the data file?");
                getDataFromFile(fileName);
            }

            if (e.getSource() == byNameButton)
            {
                display.setText("");
                ArrayList<String> tempStudentList =
                    new ArrayList<String>(studentList);
                Collections.sort(tempStudentList);
                Iterator<String> i = tempStudentList.iterator();
                while (i.hasNext()) display.append(i.next() + "\n");
                display.setCaretPosition(0);
            }

            //New: What to do if the user clicks the Sort by Mark button.
            if (e.getSource() == byMarkButton)
            {
                display.setText("");
                ArrayList<String> tempStudentList =
                    new ArrayList<String>(studentList);
                Collections.sort(tempStudentList, new MarkComparator());
                Iterator<String> i = tempStudentList.iterator();
                while (i.hasNext()) display.append(i.next() + "\n");
                display.setCaretPosition(0);
            }
        }
    }

    //New: The "Comparator" class that determines how the strings will be
    //sorted when an object of its type is used as the second parameter to
    //the ArrayList.sort() algorithm.
    class MarkComparator implements Comparator<String>
    {
        public int compare(String s1, String s2)
        {
            return -1 * s1.substring(18).compareTo(s2.substring(18));
        }
    }

    public static void main(String[] args)
    {
        StudentMarks6 app = new StudentMarks6("Record of Student Marks");
        app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        app.pack();
        app.setVisible(true);
    }
}

