123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- package ui.view;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JMenuBar;
- import javax.swing.JPanel;
- import javax.swing.JRadioButton;
- import javax.swing.JTextField;
- import javax.swing.event.CaretEvent;
- import javax.swing.event.CaretListener;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- import ui.controller.Control;
- import ui.model.Model;
- import java.util.ArrayList;
- import java.awt.GridBagLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.GridBagConstraints;
- import java.awt.Insets;
- import java.awt.event.ActionListener;
- import java.beans.PropertyChangeEvent;
- import java.beans.PropertyChangeListener;
- import java.io.File;
- import java.awt.FlowLayout;
- public class SimulationMenu extends JMenuBar {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private JPanel menuPanel = new JPanel();
- private JRadioButton simButton = new JRadioButton("Simulate");
- private JLabel simSpeedLabel = new JLabel("Simulation Speed:");
- private JTextField simSpeedText = new JTextField("1000");
- private JLabel algoLabel = new JLabel("Algorithm:");
- private JComboBox algoCombo;
- private JButton algoFolderButton = new JButton("Set Algorithm Folder");
- Model model;
- Control controller;
- public SimulationMenu(Model mod, Control cont) {
- super();
- // Init Stuff
- this.model = mod;
- this.controller = cont;
- // algoFolderButton Action
- algoFolderButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- menuSetFolderActionPerformed(evt);
- }
- private void menuSetFolderActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser fileChooser = new JFileChooser();
- JFrame test = new JFrame();
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- fileChooser.setAcceptAllFileFilterUsed(false);
- if (fileChooser.showOpenDialog(test) == JFileChooser.APPROVE_OPTION) {
- algoCombo.removeAllItems();
- File[] files = fileChooser.getSelectedFile().listFiles();
- for (int i = 0; i < files.length; i++) {
- if (files[i].toString().endsWith(".java") || files[i].toString().endsWith(".class")) {
- algoCombo.addItem(files[i]);
- }
- }
- }
- }
- });
- // Add to Panel
- GridBagLayout gbl_menuPanel = new GridBagLayout();
- gbl_menuPanel.columnWidths = new int[] { 79, 105, 34, 60, 31, 151, 0 };
- gbl_menuPanel.rowHeights = new int[] { 25, 0 };
- gbl_menuPanel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
- gbl_menuPanel.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
- menuPanel.setLayout(gbl_menuPanel);
- // isSimulation
- simButton.addPropertyChangeListener(new PropertyChangeListener() {
- @Override
- public void propertyChange(PropertyChangeEvent evt) {
- controller.setIsSimulation(simButton.isSelected());
- }
- });
- GridBagConstraints gbc_simButton = new GridBagConstraints();
- gbc_simButton.anchor = GridBagConstraints.NORTHWEST;
- gbc_simButton.insets = new Insets(0, 0, 0, 5);
- gbc_simButton.gridx = 0;
- gbc_simButton.gridy = 0;
- menuPanel.add(simButton, gbc_simButton);
- GridBagConstraints gbc_simSpeedLabel = new GridBagConstraints();
- gbc_simSpeedLabel.anchor = GridBagConstraints.WEST;
- gbc_simSpeedLabel.insets = new Insets(0, 0, 0, 5);
- gbc_simSpeedLabel.gridx = 1;
- gbc_simSpeedLabel.gridy = 0;
- menuPanel.add(simSpeedLabel, gbc_simSpeedLabel);
- // timerSpeed
- simSpeedText.setMaximumSize(new Dimension(300, 300));
- simSpeedText.addCaretListener(new CaretListener() {
- @Override
- public void caretUpdate(CaretEvent e) {
- try {
- controller.setTimerSpeed(Integer.parseInt(simSpeedText.getText()));
- } catch (Exception e2) {
- // TODO: handle exception
- }
- }
- });
- GridBagConstraints gbc_simSpeedText = new GridBagConstraints();
- gbc_simSpeedText.anchor = GridBagConstraints.WEST;
- gbc_simSpeedText.insets = new Insets(0, 0, 0, 5);
- gbc_simSpeedText.gridx = 2;
- gbc_simSpeedText.gridy = 0;
- menuPanel.add(simSpeedText, gbc_simSpeedText);
- GridBagConstraints gbc_algoLabel = new GridBagConstraints();
- gbc_algoLabel.anchor = GridBagConstraints.WEST;
- gbc_algoLabel.insets = new Insets(0, 0, 0, 5);
- gbc_algoLabel.gridx = 3;
- gbc_algoLabel.gridy = 0;
- menuPanel.add(algoLabel, gbc_algoLabel);
- algoCombo = new JComboBox<>();
- GridBagConstraints gbc_algoCombo = new GridBagConstraints();
- gbc_algoCombo.anchor = GridBagConstraints.WEST;
- gbc_algoCombo.insets = new Insets(0, 0, 0, 5);
- gbc_algoCombo.gridx = 4;
- gbc_algoCombo.gridy = 0;
- menuPanel.add(algoCombo, gbc_algoCombo);
- GridBagConstraints gbc_algoFolderButton = new GridBagConstraints();
- gbc_algoFolderButton.anchor = GridBagConstraints.NORTHWEST;
- gbc_algoFolderButton.gridx = 5;
- gbc_algoFolderButton.gridy = 0;
- menuPanel.add(algoFolderButton, gbc_algoFolderButton);
-
- //Add Panel to SimulationMenu
- this.add(menuPanel);
- }
- }
|