AlgorithmMenu.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package ui.view;
  2. import api.CpsAlgorithm;
  3. import ui.controller.Control;
  4. import ui.model.Model;
  5. import javax.swing.*;
  6. import javax.tools.JavaCompiler;
  7. import javax.tools.ToolProvider;
  8. import java.awt.*;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.io.BufferedReader;
  12. import java.io.File;
  13. import java.io.FileReader;
  14. import java.net.URL;
  15. import java.net.URLClassLoader;
  16. import java.util.HashMap;
  17. /**
  18. * This Class represents the Menu, where you can select the Algorithms.
  19. *
  20. * @author Gruppe14
  21. */
  22. public class AlgorithmMenu extends JMenu {
  23. private static final long serialVersionUID = 1L;
  24. JMenuItem algoFolderButton = new JMenuItem("Select Algorithm Folder");
  25. // root Directory
  26. File root = null;
  27. Model model;
  28. Control controller;
  29. private JMenu mnSelectAlgorithm = new JMenu("Select Algorithm");
  30. private HashMap<String, File> algosHash = new HashMap<>();
  31. private JMenuItem noneItem = new JMenuItem("none");
  32. /**
  33. * Constructor.
  34. *
  35. * @param mod
  36. * the Model
  37. * @param cont
  38. * the Controller
  39. */
  40. public AlgorithmMenu(Model mod, Control cont, GUI parentGui) {
  41. super();
  42. // Init Stuff
  43. this.model = mod;
  44. this.controller = cont;
  45. this.setText("Algorithm");
  46. // algoFolderButton Action
  47. algoFolderButton.addActionListener(new ActionListener() {
  48. @Override
  49. public void actionPerformed(java.awt.event.ActionEvent evt) {
  50. menuSetFolderActionPerformed(evt);
  51. }
  52. private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
  53. JFileChooser fileChooser = new JFileChooser();
  54. fileChooser.setCurrentDirectory(new File("."));
  55. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  56. fileChooser.setAcceptAllFileFilterUsed(false);
  57. if (fileChooser.showOpenDialog(parentGui.getFrmCyberPhysical()) == JFileChooser.APPROVE_OPTION) {
  58. // empty everything and reset the selected algorithm
  59. controller.setAlgorithm(null);
  60. mnSelectAlgorithm.removeAll();
  61. mnSelectAlgorithm.add(noneItem);
  62. noneItem.doClick();
  63. File[] files = fileChooser.getSelectedFile().listFiles();
  64. // Set Root Folder Path
  65. root = new File(fileChooser.getCurrentDirectory().getPath());
  66. for (int i = 0; i < files.length; i++) {
  67. if (files[i].toString()
  68. .endsWith(".java") /*
  69. * || files[i].toString().
  70. * endsWith(".class")
  71. */) {
  72. String name = files[i].getName();
  73. int tmpB = name.lastIndexOf('.');
  74. name = name.substring(0, tmpB);
  75. algosHash.put(name, files[i]);
  76. JMenuItem tempItem = new JMenuItem(name);
  77. tempItem.addActionListener(new ActionListener() {
  78. @Override
  79. public void actionPerformed(ActionEvent e) {
  80. for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
  81. mnSelectAlgorithm.getItem(i).setForeground(null);
  82. }
  83. tempItem.setForeground(Color.BLUE);
  84. setAlgorithm(algosHash.get(tempItem.getText()), tempItem.getText());
  85. }
  86. });
  87. mnSelectAlgorithm.add(tempItem);
  88. }
  89. }
  90. }
  91. }
  92. });
  93. // Add to Panel
  94. this.add(algoFolderButton);
  95. this.add(mnSelectAlgorithm);
  96. mnSelectAlgorithm.add(noneItem);
  97. noneItem.setForeground(Color.BLUE);
  98. noneItem.addActionListener(new ActionListener() {
  99. @Override
  100. public void actionPerformed(ActionEvent e) {
  101. for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
  102. mnSelectAlgorithm.getItem(i).setForeground(null);
  103. }
  104. noneItem.setForeground(Color.BLUE);
  105. controller.setAlgorithm(null);
  106. }
  107. });
  108. }
  109. public void setAlgorithm(File file, String name) {
  110. boolean missingCompiler = false;
  111. boolean instantiationError = false;
  112. try {
  113. BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
  114. String line = br.readLine();
  115. // Package Name
  116. String packageName = "";
  117. while (line != null) {
  118. line = line.trim();
  119. if (!line.isEmpty()) {
  120. if (line.length() >= 7 && line.substring(0, 7).equals("package")) {
  121. packageName = line.substring(8, line.length() - 1);
  122. }
  123. }
  124. if (packageName.isEmpty()) {
  125. line = br.readLine();
  126. } else {
  127. line = null;
  128. }
  129. }
  130. // Compile source file.
  131. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  132. System.out.println(System.getProperty("java.home"));
  133. if (ToolProvider.getSystemJavaCompiler() == null) {
  134. missingCompiler = true;
  135. }
  136. compiler.run(null, null, null, file.getPath());
  137. // Load and instantiate compiled class.
  138. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
  139. instantiationError = true;
  140. Class<?> cls;
  141. if (packageName.isEmpty()) {
  142. cls = Class.forName(name, true, classLoader);
  143. } else {
  144. cls = Class.forName(packageName + "." + name, true, classLoader);
  145. }
  146. Object t = cls.newInstance();
  147. if (t instanceof CpsAlgorithm) {
  148. controller.setAlgorithm(t);
  149. } else {
  150. JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
  151. JOptionPane.ERROR_MESSAGE);
  152. noneItem.doClick();
  153. }
  154. } catch (Exception e) {
  155. if (missingCompiler) {
  156. JOptionPane.showMessageDialog(null, "Missing Compiiler! Please install the JDK!", "Error!",
  157. JOptionPane.ERROR_MESSAGE);
  158. } else if (instantiationError) {
  159. JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
  160. JOptionPane.ERROR_MESSAGE);
  161. noneItem.doClick();
  162. } else {
  163. JOptionPane.showMessageDialog(null, e.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
  164. }
  165. }
  166. }
  167. }