AlgorithmMenu.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package ui.view;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileReader;
  7. import java.net.URL;
  8. import java.net.URLClassLoader;
  9. import java.util.HashMap;
  10. import javax.swing.JButton;
  11. import javax.swing.JComboBox;
  12. import javax.swing.JFileChooser;
  13. import javax.swing.JFrame;
  14. import javax.swing.JMenu;
  15. import javax.swing.JMenuBar;
  16. import javax.swing.JMenuItem;
  17. import javax.swing.JPanel;
  18. import javax.tools.JavaCompiler;
  19. import javax.tools.ToolProvider;
  20. import ui.controller.Control;
  21. import ui.model.Model;
  22. /**
  23. * This Class represents the Menu, where you can edit stuff about the
  24. * Simulation.
  25. *
  26. * @author Gruppe14
  27. */
  28. public class AlgorithmMenu extends JPanel{
  29. private static final long serialVersionUID = 1L;
  30. private JComboBox<Object> algoCombo = new JComboBox<>();
  31. JButton algoFolderButton = new JButton(Languages.getLanguage()[85]);
  32. private HashMap<String, File> algosHash = new HashMap<>();
  33. private JPanel menuPanel = new JPanel();
  34. // root Directory
  35. File root = null;
  36. Model model;
  37. Control controller;
  38. /**
  39. * Constructor.
  40. *
  41. * @param mod
  42. * the Model
  43. * @param cont
  44. * the Controller
  45. */
  46. public AlgorithmMenu(Model mod, Control cont) {
  47. super();
  48. // Init Stuff
  49. this.model = mod;
  50. this.controller = cont;
  51. algoCombo.addItem("None");
  52. // Algorithm ComboBox Action
  53. algoCombo.addActionListener(new ActionListener() {
  54. @Override
  55. public void actionPerformed(ActionEvent e) {
  56. if (algoCombo.getSelectedIndex() != 0) {
  57. setAlgorithm(algosHash.get(algoCombo.getSelectedItem()), algoCombo.getSelectedItem() + "");
  58. }else{
  59. setAlgorithm(null, "non Chosen");
  60. }
  61. }
  62. });
  63. // algoFolderButton Action
  64. algoFolderButton.addActionListener(new ActionListener() {
  65. @Override
  66. public void actionPerformed(java.awt.event.ActionEvent evt) {
  67. menuSetFolderActionPerformed(evt);
  68. }
  69. private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
  70. JFileChooser fileChooser = new JFileChooser();
  71. JFrame test = new JFrame();
  72. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  73. fileChooser.setAcceptAllFileFilterUsed(false);
  74. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  75. algoCombo.removeAllItems();
  76. algoCombo.addItem("non chosen");
  77. File[] files = fileChooser.getSelectedFile().listFiles();
  78. // Set Root Folder Path
  79. root = new File(fileChooser.getCurrentDirectory().getPath());
  80. for (int i = 0; i < files.length; i++) {
  81. if (files[i].toString()
  82. .endsWith(".java") /*
  83. * || files[i].toString().
  84. * endsWith(".class")
  85. */) {
  86. String name = files[i].getName();
  87. int tmpB = name.lastIndexOf('.');
  88. name = name.substring(0, tmpB);
  89. algosHash.put(name, files[i]);
  90. algoCombo.addItem(name);
  91. }
  92. }
  93. }
  94. }
  95. });
  96. // Add to Panel
  97. this.add(algoFolderButton);
  98. this.add(algoCombo);
  99. }
  100. public void setAlgorithm(File file, String name) {
  101. try {
  102. BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
  103. String line = br.readLine();
  104. // Package Name
  105. String packageName = "";
  106. while (line != null) {
  107. line = line.trim();
  108. if (!line.isEmpty()) {
  109. if (line.length() >= 7 && line.substring(0, 7).equals("package")) {
  110. packageName = line.substring(8, line.length() - 1);
  111. }
  112. }
  113. if (packageName.isEmpty()) {
  114. line = br.readLine();
  115. } else {
  116. line = null;
  117. }
  118. }
  119. // Compile source file.
  120. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  121. compiler.run(null, null, null, file.getPath());
  122. // Load and instantiate compiled class.
  123. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
  124. Class<?> cls;
  125. if (packageName.isEmpty()) {
  126. cls = Class.forName(name, true, classLoader);
  127. } else {
  128. cls = Class.forName(packageName + "." + name, true, classLoader);
  129. }
  130. Object t = cls.newInstance();
  131. controller.setAlgorithm(t);
  132. } catch (Exception e) {
  133. controller.addTextToConsole(e.toString());
  134. }
  135. }
  136. }