SimulationMenu.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.swing.event.ChangeEvent;
  14. import javax.swing.event.ChangeListener;
  15. import ui.controller.Control;
  16. import ui.model.Model;
  17. import java.util.ArrayList;
  18. import java.awt.GridBagLayout;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.GridBagConstraints;
  22. import java.awt.Insets;
  23. import java.awt.event.ActionListener;
  24. import java.beans.PropertyChangeEvent;
  25. import java.beans.PropertyChangeListener;
  26. import java.io.File;
  27. import java.awt.FlowLayout;
  28. public class SimulationMenu extends JMenuBar {
  29. /**
  30. *
  31. */
  32. private static final long serialVersionUID = 1L;
  33. private JPanel menuPanel = new JPanel();
  34. private JRadioButton simButton = new JRadioButton("Simulate");
  35. private JLabel simSpeedLabel = new JLabel("Simulation Speed:");
  36. private JTextField simSpeedText = new JTextField("1000");
  37. private JLabel algoLabel = new JLabel("Algorithm:");
  38. private JComboBox algoCombo;
  39. private JButton algoFolderButton = new JButton("Set Algorithm Folder");
  40. Model model;
  41. Control controller;
  42. public SimulationMenu(Model mod, Control cont) {
  43. super();
  44. // Init Stuff
  45. this.model = mod;
  46. this.controller = cont;
  47. // algoFolderButton Action
  48. algoFolderButton.addActionListener(new ActionListener() {
  49. @Override
  50. public void actionPerformed(java.awt.event.ActionEvent evt) {
  51. menuSetFolderActionPerformed(evt);
  52. }
  53. private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
  54. JFileChooser fileChooser = new JFileChooser();
  55. JFrame test = new JFrame();
  56. fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  57. fileChooser.setAcceptAllFileFilterUsed(false);
  58. if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
  59. algoCombo.removeAllItems();
  60. File[] files = fileChooser.getSelectedFile().listFiles();
  61. for (int i = 0; i < files.length; i++) {
  62. if (files[i].toString().endsWith(".java") || files[i].toString().endsWith(".class")) {
  63. algoCombo.addItem(files[i]);
  64. }
  65. }
  66. }
  67. }
  68. });
  69. // Add to Panel
  70. GridBagLayout gbl_menuPanel = new GridBagLayout();
  71. gbl_menuPanel.columnWidths = new int[] { 79, 105, 34, 60, 31, 151, 0 };
  72. gbl_menuPanel.rowHeights = new int[] { 25, 0 };
  73. gbl_menuPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
  74. gbl_menuPanel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
  75. menuPanel.setLayout(gbl_menuPanel);
  76. // isSimulation
  77. simButton.addPropertyChangeListener(new PropertyChangeListener() {
  78. @Override
  79. public void propertyChange(PropertyChangeEvent evt) {
  80. controller.setIsSimulation(simButton.isSelected());
  81. }
  82. });
  83. GridBagConstraints gbc_simButton = new GridBagConstraints();
  84. gbc_simButton.anchor = GridBagConstraints.NORTHWEST;
  85. gbc_simButton.insets = new Insets(0, 0, 0, 5);
  86. gbc_simButton.gridx = 0;
  87. gbc_simButton.gridy = 0;
  88. menuPanel.add(simButton, gbc_simButton);
  89. GridBagConstraints gbc_simSpeedLabel = new GridBagConstraints();
  90. gbc_simSpeedLabel.anchor = GridBagConstraints.WEST;
  91. gbc_simSpeedLabel.insets = new Insets(0, 0, 0, 5);
  92. gbc_simSpeedLabel.gridx = 1;
  93. gbc_simSpeedLabel.gridy = 0;
  94. menuPanel.add(simSpeedLabel, gbc_simSpeedLabel);
  95. // timerSpeed
  96. simSpeedText.setMaximumSize(new Dimension(300, 300));
  97. simSpeedText.addCaretListener(new CaretListener() {
  98. @Override
  99. public void caretUpdate(CaretEvent e) {
  100. try {
  101. controller.setTimerSpeed(Integer.parseInt(simSpeedText.getText()));
  102. } catch (Exception e2) {
  103. // TODO: handle exception
  104. }
  105. }
  106. });
  107. GridBagConstraints gbc_simSpeedText = new GridBagConstraints();
  108. gbc_simSpeedText.anchor = GridBagConstraints.WEST;
  109. gbc_simSpeedText.insets = new Insets(0, 0, 0, 5);
  110. gbc_simSpeedText.gridx = 2;
  111. gbc_simSpeedText.gridy = 0;
  112. menuPanel.add(simSpeedText, gbc_simSpeedText);
  113. GridBagConstraints gbc_algoLabel = new GridBagConstraints();
  114. gbc_algoLabel.anchor = GridBagConstraints.WEST;
  115. gbc_algoLabel.insets = new Insets(0, 0, 0, 5);
  116. gbc_algoLabel.gridx = 3;
  117. gbc_algoLabel.gridy = 0;
  118. menuPanel.add(algoLabel, gbc_algoLabel);
  119. algoCombo = new JComboBox<>();
  120. GridBagConstraints gbc_algoCombo = new GridBagConstraints();
  121. gbc_algoCombo.anchor = GridBagConstraints.WEST;
  122. gbc_algoCombo.insets = new Insets(0, 0, 0, 5);
  123. gbc_algoCombo.gridx = 4;
  124. gbc_algoCombo.gridy = 0;
  125. menuPanel.add(algoCombo, gbc_algoCombo);
  126. GridBagConstraints gbc_algoFolderButton = new GridBagConstraints();
  127. gbc_algoFolderButton.anchor = GridBagConstraints.NORTHWEST;
  128. gbc_algoFolderButton.gridx = 5;
  129. gbc_algoFolderButton.gridy = 0;
  130. menuPanel.add(algoFolderButton, gbc_algoFolderButton);
  131. //Add Panel to SimulationMenu
  132. this.add(menuPanel);
  133. }
  134. }