SimulationMenu.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package ui.view;
  2. import javax.swing.JButton;
  3. import javax.swing.JComboBox;
  4. import javax.swing.JFileChooser;
  5. import javax.swing.JFrame;
  6. import javax.swing.JLabel;
  7. import javax.swing.JMenuBar;
  8. import javax.swing.JPanel;
  9. import javax.swing.JRadioButton;
  10. import javax.swing.JTextField;
  11. import javax.swing.event.CaretEvent;
  12. import javax.swing.event.CaretListener;
  13. import javax.tools.JavaCompiler;
  14. import javax.tools.ToolProvider;
  15. import ui.controller.Control;
  16. import ui.model.Model;
  17. import java.util.HashMap;
  18. import java.awt.GridBagLayout;
  19. import java.awt.Dimension;
  20. import java.awt.GridBagConstraints;
  21. import java.awt.Insets;
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.io.BufferedReader;
  25. import java.io.File;
  26. import java.io.FileReader;
  27. import java.net.URL;
  28. import java.net.URLClassLoader;
  29. /**
  30. * This Class represents the Menu, where you can edit stuff about the
  31. * Simulation.
  32. *
  33. * @author Gruppe14
  34. */
  35. public class SimulationMenu extends JMenuBar {
  36. private static final long serialVersionUID = 1L;
  37. private JPanel menuPanel = new JPanel();
  38. JRadioButton simButton = new JRadioButton(Languages.getLanguage()[83]);
  39. JLabel simSpeedLabel = new JLabel(Languages.getLanguage()[84]);
  40. private JTextField simSpeedText = new JTextField("1000");
  41. private JComboBox algoCombo = new JComboBox<>();
  42. JButton algoFolderButton = new JButton(Languages.getLanguage()[85]);
  43. private HashMap<String, File> algosHash = new HashMap<>();
  44. // root Directory
  45. File root = null;
  46. Model model;
  47. Control controller;
  48. /**
  49. * Constructor.
  50. *
  51. * @param mod
  52. * the Model
  53. * @param cont
  54. * the Controller
  55. */
  56. public SimulationMenu(Model mod, Control cont) {
  57. super();
  58. // Init Stuff
  59. this.model = mod;
  60. this.controller = cont;
  61. // Algorithm ComboBox Action
  62. algoCombo.addActionListener(new ActionListener() {
  63. @Override
  64. public void actionPerformed(ActionEvent e) {
  65. setAlgorithm(algosHash.get(algoCombo.getSelectedItem()), algoCombo.getSelectedItem() + "");
  66. }
  67. });
  68. // algoFolderButton Action
  69. algoFolderButton.addActionListener(new ActionListener() {
  70. @Override
  71. public void actionPerformed(java.awt.event.ActionEvent evt) {
  72. menuSetFolderActionPerformed(evt);
  73. }
  74. private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
  75. JFileChooser fileChooser = new JFileChooser();
  76. JFrame test = new JFrame();
  77. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  78. fileChooser.setAcceptAllFileFilterUsed(false);
  79. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  80. algoCombo.removeAllItems();
  81. File[] files = fileChooser.getSelectedFile().listFiles();
  82. // Set Root Folder Path
  83. root = new File(fileChooser.getCurrentDirectory().getPath());
  84. for (int i = 0; i < files.length; i++) {
  85. if (files[i].toString()
  86. .endsWith(".java") /*
  87. * || files[i].toString().
  88. * endsWith(".class")
  89. */) {
  90. String name = files[i].getName();
  91. int tmpB = name.lastIndexOf('.');
  92. name = name.substring(0, tmpB);
  93. algosHash.put(name, files[i]);
  94. algoCombo.addItem(name);
  95. }
  96. }
  97. }
  98. }
  99. });
  100. // Add to Panel
  101. GridBagLayout gblmenuPanel = new GridBagLayout();
  102. gblmenuPanel.columnWidths = new int[] { 79, 105, 34, 60, 31, 151, 0 };
  103. gblmenuPanel.rowHeights = new int[] { 25, 0 };
  104. gblmenuPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
  105. gblmenuPanel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
  106. menuPanel.setLayout(gblmenuPanel);
  107. // isSimulation
  108. simButton.addActionListener(new ActionListener() {
  109. @Override
  110. public void actionPerformed(ActionEvent e) {
  111. controller.setIsSimulation(simButton.isSelected());
  112. controller.calculateStateForCurrentTimeStep();
  113. }
  114. });
  115. GridBagConstraints gbcsimButton = new GridBagConstraints();
  116. gbcsimButton.anchor = GridBagConstraints.NORTHWEST;
  117. gbcsimButton.insets = new Insets(0, 0, 0, 5);
  118. gbcsimButton.gridx = 0;
  119. gbcsimButton.gridy = 0;
  120. menuPanel.add(simButton, gbcsimButton);
  121. GridBagConstraints gbcsimSpeedLabel = new GridBagConstraints();
  122. gbcsimSpeedLabel.anchor = GridBagConstraints.WEST;
  123. gbcsimSpeedLabel.insets = new Insets(0, 0, 0, 5);
  124. gbcsimSpeedLabel.gridx = 1;
  125. gbcsimSpeedLabel.gridy = 0;
  126. menuPanel.add(simSpeedLabel, gbcsimSpeedLabel);
  127. // timerSpeed
  128. simSpeedText.setMaximumSize(new Dimension(300, 300));
  129. simSpeedText.addCaretListener(new CaretListener() {
  130. @Override
  131. public void caretUpdate(CaretEvent e) {
  132. try {
  133. controller.setTimerSpeed(Integer.parseInt(simSpeedText.getText()));
  134. } catch (Exception ex) {
  135. // TODO: handle exception
  136. }
  137. }
  138. });
  139. GridBagConstraints gbcSimSpeedText = new GridBagConstraints();
  140. gbcSimSpeedText.anchor = GridBagConstraints.WEST;
  141. gbcSimSpeedText.insets = new Insets(0, 0, 0, 5);
  142. gbcSimSpeedText.gridx = 2;
  143. gbcSimSpeedText.gridy = 0;
  144. menuPanel.add(simSpeedText, gbcSimSpeedText);
  145. GridBagConstraints gbcAlgoFolderButton = new GridBagConstraints();
  146. gbcAlgoFolderButton.anchor = GridBagConstraints.WEST;
  147. gbcAlgoFolderButton.insets = new Insets(0, 0, 0, 5);
  148. gbcAlgoFolderButton.gridx = 3;
  149. gbcAlgoFolderButton.gridy = 0;
  150. menuPanel.add(algoFolderButton, gbcAlgoFolderButton);
  151. GridBagConstraints gbcAlgoCombo = new GridBagConstraints();
  152. gbcAlgoCombo.anchor = GridBagConstraints.WEST;
  153. gbcAlgoCombo.insets = new Insets(0, 0, 0, 5);
  154. gbcAlgoCombo.gridx = 4;
  155. gbcAlgoCombo.gridy = 0;
  156. menuPanel.add(algoCombo, gbcAlgoCombo);
  157. algoCombo.addItem(Languages.getLanguage()[86]);
  158. // Add Panel to SimulationMenu
  159. this.add(menuPanel);
  160. }
  161. public void setAlgorithm(File file, String name) {
  162. try {
  163. BufferedReader br = new BufferedReader(new FileReader(file.getPath()));
  164. String line = br.readLine();
  165. // Package Name
  166. String packageName = "";
  167. while (line != null) {
  168. line = line.trim();
  169. if (!line.isEmpty()) {
  170. controller.addTextToConsole(line);
  171. }
  172. line = br.readLine();
  173. }
  174. controller.addTextToConsole("name" + packageName);
  175. // Compile source file.
  176. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  177. compiler.run(null, null, null, file.getPath());
  178. // Load and instantiate compiled class.
  179. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
  180. Class<?> cls = Class.forName("projectPackage." + name, true, classLoader);
  181. Object t = cls.newInstance();
  182. controller.setAlgorithm(t);
  183. } catch (Exception e) {
  184. e.printStackTrace();
  185. }
  186. }
  187. }