AlgorithmMenu.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  55. fileChooser.setAcceptAllFileFilterUsed(false);
  56. if (fileChooser.showOpenDialog(parentGui.getFrmCyberPhysical()) == JFileChooser.APPROVE_OPTION) {
  57. // empty everything and reset the selected algorithm
  58. controller.setAlgorithm(null);
  59. mnSelectAlgorithm.removeAll();
  60. mnSelectAlgorithm.add(noneItem);
  61. noneItem.doClick();
  62. File[] files = fileChooser.getSelectedFile().listFiles();
  63. // Set Root Folder Path
  64. root = new File(fileChooser.getCurrentDirectory().getPath());
  65. for (int i = 0; i < files.length; i++) {
  66. if (files[i].toString()
  67. .endsWith(".java") /*
  68. * || files[i].toString().
  69. * endsWith(".class")
  70. */) {
  71. String name = files[i].getName();
  72. int tmpB = name.lastIndexOf('.');
  73. name = name.substring(0, tmpB);
  74. algosHash.put(name, files[i]);
  75. JMenuItem tempItem = new JMenuItem(name);
  76. tempItem.addActionListener(new ActionListener() {
  77. @Override
  78. public void actionPerformed(ActionEvent e) {
  79. for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
  80. mnSelectAlgorithm.getItem(i).setForeground(null);
  81. }
  82. tempItem.setForeground(Color.BLUE);
  83. setAlgorithm(algosHash.get(tempItem.getText()), tempItem.getText());
  84. }
  85. });
  86. mnSelectAlgorithm.add(tempItem);
  87. }
  88. }
  89. }
  90. }
  91. });
  92. // Add to Panel
  93. this.add(algoFolderButton);
  94. this.add(mnSelectAlgorithm);
  95. mnSelectAlgorithm.add(noneItem);
  96. noneItem.setForeground(Color.BLUE);
  97. noneItem.addActionListener(new ActionListener() {
  98. @Override
  99. public void actionPerformed(ActionEvent e) {
  100. for (int i = 0; i < mnSelectAlgorithm.getItemCount(); i++) {
  101. mnSelectAlgorithm.getItem(i).setForeground(null);
  102. }
  103. noneItem.setForeground(Color.BLUE);
  104. controller.setAlgorithm(null);
  105. }
  106. });
  107. }
  108. public void setAlgorithm(File file, String name) {
  109. boolean missingCompiler = false;
  110. boolean instantiationError = false;
  111. try {
  112. BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
  113. String line = br.readLine();
  114. // Package Name
  115. String packageName = "";
  116. while (line != null) {
  117. line = line.trim();
  118. if (!line.isEmpty()) {
  119. if (line.length() >= 7 && line.substring(0, 7).equals("package")) {
  120. packageName = line.substring(8, line.length() - 1);
  121. }
  122. }
  123. if (packageName.isEmpty()) {
  124. line = br.readLine();
  125. } else {
  126. line = null;
  127. }
  128. }
  129. // Compile source file.
  130. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  131. if (ToolProvider.getSystemJavaCompiler() == null) {
  132. missingCompiler = true;
  133. }
  134. compiler.run(null, null, null, file.getPath());
  135. // Load and instantiate compiled class.
  136. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
  137. instantiationError = true;
  138. Class<?> cls;
  139. if (packageName.isEmpty()) {
  140. cls = Class.forName(name, true, classLoader);
  141. } else {
  142. cls = Class.forName(packageName + "." + name, true, classLoader);
  143. }
  144. Object t = cls.newInstance();
  145. if (t instanceof CpsAlgorithm) {
  146. controller.setAlgorithm(t);
  147. } else {
  148. JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
  149. JOptionPane.ERROR_MESSAGE);
  150. noneItem.doClick();
  151. }
  152. } catch (Exception e) {
  153. if (missingCompiler) {
  154. JOptionPane.showMessageDialog(null, "Missing Compiiler! Please install the JDK!", "Error!",
  155. JOptionPane.ERROR_MESSAGE);
  156. } else if (instantiationError) {
  157. JOptionPane.showMessageDialog(null, "Class does not implement CpsAlgorithm!", "Error!",
  158. JOptionPane.ERROR_MESSAGE);
  159. noneItem.doClick();
  160. } else {
  161. JOptionPane.showMessageDialog(null, e.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
  162. }
  163. }
  164. }
  165. }