Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.83 KB | None | 0 0
  1. /**
  2. * Description: The purpose of this class is to build the GUI for the program. It gives the user the option to select a
  3. * file, list the contents of the file in alphabetical order with the number of occurrences of each word in the file,
  4. * 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
  5. * to continue to build the BST.
  6. * @author Daniel Henrichs
  7. * @version 1.0
  8. * @since 2019-07-30
  9. * @see GUI
  10. */
  11.  
  12. import java.awt.BorderLayout;
  13. import java.awt.Container;
  14. import java.awt.GridLayout;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.util.List;
  20. import java.util.Scanner;
  21. import javax.swing.*;
  22.  
  23. class GUI extends JFrame {
  24. private final JTextField file = new JTextField();
  25. private final JTextField directory = new JTextField();
  26. private final JTextArea readMe = new JTextArea(
  27. "--------------------------------------------------READ ME--------------------------------------------------"+
  28. "\n"+
  29. "\n\u2022To see total number of words in tree, along with the total number of unique words" +
  30. "\nthe total number of unique words, browse for file and select the 'Words' button. "+
  31. "\n"
  32. +"\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
  33. +"\n'Alphabetical Order' button."
  34. +"\n"
  35. +"\n\u2022To add a file, select the 'Add File' button."
  36.  
  37. );
  38. private final JTextArea results = new JTextArea();
  39. private final JButton browse = new JButton("Browse");
  40. private final JButton words = new JButton("Words");
  41. private final JButton alpha = new JButton("Alphabetical Order");
  42. private final JButton anotherFile = new JButton("Add Another File");
  43.  
  44. /**
  45. * The purpose of this method is to build the components of the GUI. It builds the JTextAreas, buttons, panels
  46. * and listeners, and adds them to the Panels.
  47. *@author Daniel Henrichs
  48. *@version 1.0
  49. *@since 2019-07-30
  50. */
  51. public GUI() {
  52. JPanel panel1 = new JPanel();
  53. JPanel panel2 = new JPanel();
  54. Container cp = getContentPane();
  55.  
  56.  
  57. browse.addActionListener(new Listeners());
  58. words.addActionListener(new Listeners());
  59. alpha.addActionListener(new Listeners());
  60. anotherFile.addActionListener(new Listeners());
  61.  
  62. directory.setEditable(false);
  63. file.setEditable(false);
  64. readMe.setEditable(false);
  65. results.setEditable(false);
  66.  
  67.  
  68.  
  69. panel2 = new JPanel();
  70. panel1 = new JPanel();
  71. panel1.setLayout(new GridLayout(2, 1));
  72. panel2.setLayout(new GridLayout(3, 2));
  73. panel1.add(readMe);
  74. JScrollPane sp = new JScrollPane(results, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  75. panel1.add(sp);
  76. panel2.add(new JLabel("File Name:"));
  77. panel2.add(file);
  78. panel2.add(browse);
  79. panel2.add(words);
  80. panel2.add(alpha);
  81. panel2.add(anotherFile);
  82. cp.add(panel1, BorderLayout.NORTH);
  83. cp.add(panel2, BorderLayout.SOUTH);
  84. }
  85.  
  86.  
  87. /**
  88. *
  89. * The purpose of this class is to listed for the actions done by the buttons on the GUI, and respond accordingly.
  90. * each button has a specific action, and with each action it calls methods in the BTSFunction class. This class
  91. * allows the buttons to add a file, sort the content of that file, count the words and unique words, and add another
  92. * file to the BST.
  93. * @author Daniel Henrichs
  94. * @version 1.0
  95. * @since 2019-07-30
  96. * @throws IOException
  97. */
  98. class Listeners implements ActionListener {
  99.  
  100. public void actionPerformed(ActionEvent evt) {
  101. File file = new File(directory.getText() + "\\" + GUI.this.file.getText());
  102. BSTFunctions bstf = new BSTFunctions();
  103. if (evt.getSource().equals(browse)) {
  104. JFileChooser pickFile = new JFileChooser();
  105. pickFile.setMultiSelectionEnabled(true);
  106. int dialog = pickFile.showOpenDialog(GUI.this);
  107. if (dialog == JFileChooser.APPROVE_OPTION) {
  108. GUI.this.file.setText(pickFile.getSelectedFile().getName());
  109. directory.setText(pickFile.getCurrentDirectory().toString());
  110. }
  111. if (dialog == JFileChooser.CANCEL_OPTION) {
  112. GUI.this.file.setText("You pressed cancel");
  113. directory.setText("");
  114. }
  115. } else if (evt.getSource().equals(words)) {
  116. try {
  117. Scanner scanner = new Scanner(file);
  118. while (scanner.hasNext()) {
  119. bstf.insert(bstf.ROOT, scanner.next().toLowerCase().trim(), 1);
  120. }
  121. bstf.wordCount(bstf.ROOT);
  122. readMe.setText("--------------------------------------------------READ ME--------------------------------------------------" +
  123. "\n" +
  124. "\n\u2022To see total number of words in tree, along with the total number of unique words" +
  125. "\nthe total number of unique words, browse for file and select the 'Words' button. " +
  126. "\n"
  127. + "\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
  128. + "\n'Alphabetical Order' button."
  129. + "\n"
  130. + "\n\u2022To add a file, select the 'Add File' button."
  131. +("\n")
  132. +("\n"));
  133. results.setText("--------------------------------------------------RESULTS--------------------------------------------------");
  134. results.append("\nTotal Number of words: " + bstf.nodecount);
  135. results.append("\nTotal Number of unique words: " + bstf.uc);
  136. results.append("\n");
  137.  
  138. } catch (IOException e1) {
  139. results.setText("--------------------------------------------------WARNING--------------------------------------------------");
  140. results.append("\n");
  141. results.append("\n\u2022YOU MUST SELECT A FILE TO CONTINUE");
  142. }
  143.  
  144. } else if (evt.getSource().equals(alpha)) {
  145. try {
  146. Scanner scanner = new Scanner(file);
  147. while (scanner.hasNext()) {
  148. bstf.insert(bstf.ROOT, scanner.next().toLowerCase().trim(), 1);
  149. }
  150.  
  151. readMe.setText("--------------------------------------------------READ ME--------------------------------------------------" +
  152. "\n" +
  153. "\n\u2022To see total number of words in tree, along with the total number of unique words" +
  154. "\nthe total number of unique words, browse for file and select the 'Words' button. " +
  155. "\n"
  156. + "\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
  157. + "\n'Alphabetical Order' button."
  158. + "\n"
  159. + "\n\u2022To add a file, select the 'Add File' button."
  160. +("\n")
  161. +("\n"));
  162. List<String> result = bstf.listInOrder(bstf.ROOT);
  163. for(String str : result) { //iterate
  164. //System.out.print(str +" ");
  165. results.append(str + " ");//append into text area
  166. }
  167. scanner.close();
  168. } catch (IOException e1) {
  169. results.setText("--------------------------------------------------WARNING--------------------------------------------------");
  170. results.append("\n");
  171. results.append("\n\u2022YOU MUST SELECT A FILE TO CONTINUE");
  172. }
  173. } else if (evt.getSource().equals(anotherFile)) {
  174. JFileChooser pickFile = new JFileChooser();
  175. int dialog = pickFile.showOpenDialog(GUI.this);
  176. if (dialog == JFileChooser.APPROVE_OPTION) {
  177. GUI.this.file.setText(pickFile.getSelectedFile().getName());
  178. directory.setText(pickFile.getCurrentDirectory().toString());
  179. }
  180. if (dialog == JFileChooser.CANCEL_OPTION) {
  181. GUI.this.file.setText("You pressed cancel");
  182. directory.setText("");
  183. }
  184. try {
  185. Scanner scanner = new Scanner(file);
  186. while (scanner.hasNext()) {
  187. bstf.insert(bstf.ROOT, scanner.next().toLowerCase().trim(), 1);
  188. }
  189.  
  190. bstf.wordCount(bstf.ROOT);
  191. bstf.listInOrder(bstf.ROOT);
  192. readMe.setText("--------------------------------------------------READ ME--------------------------------------------------" +
  193. "\n" +
  194. "\n\u2022To see total number of words in tree, along with the total number of unique words" +
  195. "\nthe total number of unique words, browse for file and select the 'Words' button. " +
  196. "\n"
  197. + "\n\u2022To see the tree in alphabetical order, with the number of occurrences, select the"
  198. + "\n'Alphabetical Order' button."
  199. + "\n"
  200. + "\n\u2022To add a file, select the 'Add File' button."
  201. +("\n")
  202. +("\n"));
  203. scanner.close();
  204. } catch (IOException e1) {
  205. results.setText("--------------------------------------------------WARNING--------------------------------------------------");
  206. results.append("\n");
  207. results.append("\n\u2022YOU MUST SELECT A FILE TO CONTINUE");
  208. }
  209.  
  210. }
  211. }
  212.  
  213. }
  214.  
  215.  
  216. /**
  217. *The purpose of this method is to set states of the frame for the GUI. It sets the close operation, size, location
  218. * and visibility.
  219. * @author Daniel Henrichs
  220. * @version 1.0
  221. * @since 2019-07-30
  222. * @param frame
  223. */
  224. public static void run(JFrame frame) {
  225. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  226. frame.setSize(510,475);
  227. frame.setLocationRelativeTo(null);
  228. frame.setVisible(true);
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement