AlgorithmMenu.java 6.1 KB

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