SimulationMenu.java 7.1 KB

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