AlgoWindow.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.Font;
  5. import java.io.File;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.net.URLClassLoader;
  11. import java.util.Arrays;
  12. import java.util.Scanner;
  13. import java.util.stream.Collectors;
  14. import javax.swing.JButton;
  15. import javax.swing.JFileChooser;
  16. import javax.swing.JFrame;
  17. import javax.swing.JLabel;
  18. import javax.swing.JMenu;
  19. import javax.swing.JMenuBar;
  20. import javax.swing.JMenuItem;
  21. import javax.swing.JPanel;
  22. import javax.swing.JTextArea;
  23. import javax.swing.JTextPane;
  24. import javax.swing.text.SimpleAttributeSet;
  25. import javax.swing.text.StyleConstants;
  26. import javax.swing.text.StyledDocument;
  27. import javax.tools.JavaCompiler;
  28. import javax.tools.ToolProvider;
  29. import api.Algorithm;
  30. import ui.controller.Control;
  31. //TODO delete old class AlgorithmMenu and change this class to AlgorithmMenu;
  32. public class AlgoWindow extends JFrame{
  33. private File root;
  34. private Algorithm actual;
  35. private Control control;
  36. private JPanel content = new JPanel();
  37. AlgoWindow(JFrame parentFrame, Control control){
  38. this.control = control;
  39. this.setTitle("Algorithm");
  40. this.setVisible(true);
  41. this.setContentPane(content);
  42. this.setIconImage(Util.loadImage("/Images/Holeg.png", 30, 30));
  43. initMenuBar();
  44. initDefaultContentPanel();
  45. this.pack();
  46. this.setLocationRelativeTo(parentFrame);
  47. }
  48. private void initMenuBar() {
  49. JMenuBar menuBar = new JMenuBar();
  50. JMenu menuSelect = new JMenu("File");
  51. JMenuItem menuItemFolder= new JMenuItem("Open Folder..");
  52. JMenuItem menuItemAlgorithm= new JMenuItem("Open .java-File..");
  53. menuItemAlgorithm.addActionListener(actionEvent -> {
  54. System.out.println("Test");
  55. JFileChooser fileChooser = new JFileChooser();
  56. fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
  57. fileChooser.setAcceptAllFileFilterUsed(false);
  58. int result = fileChooser.showOpenDialog(this);
  59. if(result == JFileChooser.APPROVE_OPTION) {
  60. //Found a file
  61. File founded = fileChooser.getSelectedFile();
  62. //Get Class name:
  63. String name = founded.getName().substring(0, founded.getName().lastIndexOf("."));
  64. //get Package name
  65. String packageName = "";
  66. Scanner in = null;
  67. try {
  68. in = new Scanner(founded);
  69. } catch (FileNotFoundException e) {
  70. System.out.println("File not Found");
  71. }
  72. while(in.hasNext()) {
  73. String line = in.nextLine().trim();
  74. if(line.startsWith("package")) {
  75. packageName = line.substring(8, line.length()-1);
  76. break;
  77. }
  78. }
  79. //GetPathName
  80. String path = founded.getParentFile().isDirectory() ? founded.getParentFile().getAbsolutePath() : "";
  81. File folder = founded.getParentFile();
  82. System.out.println(founded.getName() + ", Name:" + name + ", Package:" + packageName + ", Path:" + path);
  83. // Compile source file.
  84. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  85. if (ToolProvider.getSystemJavaCompiler() == null) {
  86. System.out.println("MissingCompiler");
  87. }else {
  88. compiler.run(null, null, null, founded.getAbsolutePath());
  89. }
  90. try {
  91. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { folder.toURI().toURL() });
  92. Class<?> cls = Class.forName(packageName.isEmpty()?name:packageName + "." +name, true, classLoader);
  93. Object object = cls.newInstance();
  94. if(!(object instanceof Algorithm)) {
  95. System.out.println("Class does not implement CpsAlgorithm!");
  96. }else {
  97. actual = (Algorithm)object;
  98. actual.setController(control);
  99. this.setContentPane(actual.getAlgorithmPanel());
  100. this.pack();
  101. }
  102. } catch (MalformedURLException e) {
  103. System.out.println("URLClassLoader error");
  104. e.printStackTrace();
  105. } catch (ClassNotFoundException e) {
  106. System.out.println("ClassNotFound error" + e.getMessage());
  107. } catch (InstantiationException e) {
  108. // TODO Auto-generated catch block
  109. e.printStackTrace();
  110. } catch (IllegalAccessException e) {
  111. // TODO Auto-generated catch block
  112. e.printStackTrace();
  113. }
  114. }else {
  115. //Not
  116. }
  117. });
  118. menuSelect.add(menuItemAlgorithm);
  119. menuItemFolder.addActionListener(actionEvent->openFolder(menuSelect));
  120. menuSelect.add(menuItemFolder);
  121. menuSelect.addSeparator();
  122. JMenu menuHelp = new JMenu("Help");
  123. menuBar.add(menuSelect);
  124. menuBar.add(menuHelp);
  125. this.setJMenuBar(menuBar);
  126. }
  127. private void initDefaultContentPanel() {
  128. content.setLayout(new BorderLayout());
  129. JTextPane textPane = new JTextPane();
  130. SimpleAttributeSet attribs = new SimpleAttributeSet();
  131. StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_JUSTIFIED);
  132. textPane.setParagraphAttributes(attribs, true);
  133. textPane.setText("No algorithm is loaded."
  134. +"\n OPTION[1]:"
  135. +"\n• Select '.java'-file via the file browser. (File\u2192Open .jave-File..)"
  136. +"\n"
  137. +"\n OPTION[2]: CURRENTLY NOT WORKING"
  138. +"\n• First select the folder where the algorithm '.java'-file is located. (File\u2192Open Folder..)"
  139. +"\n• Second load the algorithm. (File\u2192'YourAlgo')");
  140. textPane.setFont(new Font("Serif", Font.PLAIN, 16));
  141. content.add(textPane, BorderLayout.CENTER);
  142. //content.add(new JButton("Run"),BorderLayout.PAGE_END );
  143. content.setPreferredSize(new Dimension(500, 500));
  144. }
  145. private void openFolder(JMenu menuSelect) {
  146. System.out.println("OpenFolder");
  147. JFileChooser fileChooser = new JFileChooser();
  148. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  149. fileChooser.setAcceptAllFileFilterUsed(false);
  150. int result = fileChooser.showOpenDialog(this);
  151. if(result == JFileChooser.APPROVE_OPTION) {
  152. System.out.println("Richtig" + menuSelect.getItemCount());
  153. System.out.println(fileChooser.getSelectedFile().getAbsolutePath());
  154. root = fileChooser.getSelectedFile();
  155. File[] files = fileChooser.getSelectedFile().listFiles(file -> file.getName().endsWith(".java"));
  156. //Remove all Index from old Folder
  157. for(int i= menuSelect.getItemCount()-1; i> 2; i--) {
  158. menuSelect.remove(i);
  159. }
  160. for(File file: files) {
  161. menuSelect.add(new JMenuItem(file.getName().substring(0, file.getName().lastIndexOf("."))));
  162. }
  163. //System.out.println(Arrays.asList(files).stream().map(Object::toString).collect(Collectors.joining(", ")));
  164. }else {
  165. System.out.println("Falsch");
  166. }
  167. //
  168. }
  169. }