Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Description: The purpose of this class is to build the GUI for the program. It gives the user the option to select a
- * file, list the contents of the file in alphabetical order with the number of occurrences of each word in the file,
- * the total number of words in the file, and the number of unique words in the file. It also allows you to add a file
- * to continue to build the BST.
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @see GUI
- */
- import java.awt.BorderLayout;
- import java.awt.Container;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.util.List;
- import java.util.Scanner;
- import javax.swing.*;
- class GUI extends JFrame {
- private final JTextField file = new JTextField();
- private final JTextField directory = new JTextField();
- private final JTextArea readMe = new JTextArea(
- "--------------------------------------------------READ ME--------------------------------------------------"+
- "\n"+
- "\n\u2022To see total number of words in tree, along with the total number of unique words" +
- "\nthe total number of unique words, browse for file and select the 'Words' button. "+
- "\n"
- +"\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
- +"\n'Alphabetical Order' button."
- +"\n"
- +"\n\u2022To add a file, select the 'Add File' button."
- );
- private final JTextArea results = new JTextArea();
- private final JButton browse = new JButton("Browse");
- private final JButton words = new JButton("Words");
- private final JButton alpha = new JButton("Alphabetical Order");
- private final JButton anotherFile = new JButton("Add Another File");
- /**
- * The purpose of this method is to build the components of the GUI. It builds the JTextAreas, buttons, panels
- * and listeners, and adds them to the Panels.
- *@author Daniel Henrichs
- *@version 1.0
- *@since 2019-07-30
- */
- public GUI() {
- JPanel panel1 = new JPanel();
- JPanel panel2 = new JPanel();
- Container cp = getContentPane();
- browse.addActionListener(new Listeners());
- words.addActionListener(new Listeners());
- alpha.addActionListener(new Listeners());
- anotherFile.addActionListener(new Listeners());
- directory.setEditable(false);
- file.setEditable(false);
- readMe.setEditable(false);
- results.setEditable(false);
- panel2 = new JPanel();
- panel1 = new JPanel();
- panel1.setLayout(new GridLayout(2, 1));
- panel2.setLayout(new GridLayout(3, 2));
- panel1.add(readMe);
- JScrollPane sp = new JScrollPane(results, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
- panel1.add(sp);
- panel2.add(new JLabel("File Name:"));
- panel2.add(file);
- panel2.add(browse);
- panel2.add(words);
- panel2.add(alpha);
- panel2.add(anotherFile);
- cp.add(panel1, BorderLayout.NORTH);
- cp.add(panel2, BorderLayout.SOUTH);
- }
- /**
- *
- * The purpose of this class is to listed for the actions done by the buttons on the GUI, and respond accordingly.
- * each button has a specific action, and with each action it calls methods in the BTSFunction class. This class
- * allows the buttons to add a file, sort the content of that file, count the words and unique words, and add another
- * file to the BST.
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @throws IOException
- */
- class Listeners implements ActionListener {
- public void actionPerformed(ActionEvent evt) {
- File file = new File(directory.getText() + "\\" + GUI.this.file.getText());
- BSTFunctions bstf = new BSTFunctions();
- if (evt.getSource().equals(browse)) {
- JFileChooser pickFile = new JFileChooser();
- pickFile.setMultiSelectionEnabled(true);
- int dialog = pickFile.showOpenDialog(GUI.this);
- if (dialog == JFileChooser.APPROVE_OPTION) {
- GUI.this.file.setText(pickFile.getSelectedFile().getName());
- directory.setText(pickFile.getCurrentDirectory().toString());
- }
- if (dialog == JFileChooser.CANCEL_OPTION) {
- GUI.this.file.setText("You pressed cancel");
- directory.setText("");
- }
- } else if (evt.getSource().equals(words)) {
- try {
- Scanner scanner = new Scanner(file);
- while (scanner.hasNext()) {
- bstf.insert(bstf.ROOT, scanner.next().toLowerCase().trim(), 1);
- }
- bstf.wordCount(bstf.ROOT);
- readMe.setText("--------------------------------------------------READ ME--------------------------------------------------" +
- "\n" +
- "\n\u2022To see total number of words in tree, along with the total number of unique words" +
- "\nthe total number of unique words, browse for file and select the 'Words' button. " +
- "\n"
- + "\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
- + "\n'Alphabetical Order' button."
- + "\n"
- + "\n\u2022To add a file, select the 'Add File' button."
- +("\n")
- +("\n"));
- results.setText("--------------------------------------------------RESULTS--------------------------------------------------");
- results.append("\nTotal Number of words: " + bstf.nodecount);
- results.append("\nTotal Number of unique words: " + bstf.uc);
- results.append("\n");
- } catch (IOException e1) {
- results.setText("--------------------------------------------------WARNING--------------------------------------------------");
- results.append("\n");
- results.append("\n\u2022YOU MUST SELECT A FILE TO CONTINUE");
- }
- } else if (evt.getSource().equals(alpha)) {
- try {
- Scanner scanner = new Scanner(file);
- while (scanner.hasNext()) {
- bstf.insert(bstf.ROOT, scanner.next().toLowerCase().trim(), 1);
- }
- readMe.setText("--------------------------------------------------READ ME--------------------------------------------------" +
- "\n" +
- "\n\u2022To see total number of words in tree, along with the total number of unique words" +
- "\nthe total number of unique words, browse for file and select the 'Words' button. " +
- "\n"
- + "\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
- + "\n'Alphabetical Order' button."
- + "\n"
- + "\n\u2022To add a file, select the 'Add File' button."
- +("\n")
- +("\n"));
- List<String> result = bstf.listInOrder(bstf.ROOT);
- for(String str : result) { //iterate
- //System.out.print(str +" ");
- results.append(str + " ");//append into text area
- }
- scanner.close();
- } catch (IOException e1) {
- results.setText("--------------------------------------------------WARNING--------------------------------------------------");
- results.append("\n");
- results.append("\n\u2022YOU MUST SELECT A FILE TO CONTINUE");
- }
- } else if (evt.getSource().equals(anotherFile)) {
- JFileChooser pickFile = new JFileChooser();
- int dialog = pickFile.showOpenDialog(GUI.this);
- if (dialog == JFileChooser.APPROVE_OPTION) {
- GUI.this.file.setText(pickFile.getSelectedFile().getName());
- directory.setText(pickFile.getCurrentDirectory().toString());
- }
- if (dialog == JFileChooser.CANCEL_OPTION) {
- GUI.this.file.setText("You pressed cancel");
- directory.setText("");
- }
- try {
- Scanner scanner = new Scanner(file);
- while (scanner.hasNext()) {
- bstf.insert(bstf.ROOT, scanner.next().toLowerCase().trim(), 1);
- }
- bstf.wordCount(bstf.ROOT);
- bstf.listInOrder(bstf.ROOT);
- readMe.setText("--------------------------------------------------READ ME--------------------------------------------------" +
- "\n" +
- "\n\u2022To see total number of words in tree, along with the total number of unique words" +
- "\nthe total number of unique words, browse for file and select the 'Words' button. " +
- "\n"
- + "\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
- + "\n'Alphabetical Order' button."
- + "\n"
- + "\n\u2022To add a file, select the 'Add File' button."
- +("\n")
- +("\n"));
- scanner.close();
- } catch (IOException e1) {
- results.setText("--------------------------------------------------WARNING--------------------------------------------------");
- results.append("\n");
- results.append("\n\u2022YOU MUST SELECT A FILE TO CONTINUE");
- }
- }
- }
- }
- /**
- *The purpose of this method is to set states of the frame for the GUI. It sets the close operation, size, location
- * and visibility.
- * @author Daniel Henrichs
- * @version 1.0
- * @since 2019-07-30
- * @param frame
- */
- public static void run(JFrame frame) {
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(510,475);
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement