|
@@ -0,0 +1,156 @@
|
|
|
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
|
|
|
+
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.JProgressBar;
|
|
|
+import javax.swing.JButton;
|
|
|
+import javax.swing.JLabel;
|
|
|
+import javax.swing.JTextField;
|
|
|
+import javax.swing.Timer;
|
|
|
+
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
|
|
|
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
|
|
|
+
|
|
|
+import java.awt.Rectangle;
|
|
|
+import javax.swing.JCheckBox;
|
|
|
+
|
|
|
+@SuppressWarnings("serial")
|
|
|
+public class SimulationConfigurator extends JFrame {
|
|
|
+ private JTextField tfStartTimeLong;
|
|
|
+ private JTextField tfStepDuration;
|
|
|
+ private JTextField tfEndTimeLong;
|
|
|
+ private JLabel lbCurrentTimeShow;
|
|
|
+ private JProgressBar progressBar;
|
|
|
+ private Timer timer;
|
|
|
+ private SimulationManager sim;
|
|
|
+ private long currentTime = 0;
|
|
|
+ private long startTime = 0;
|
|
|
+ private long duration = 100;
|
|
|
+ private long endTime = 0;
|
|
|
+
|
|
|
+ public SimulationConfigurator(SimulationManager sim) {
|
|
|
+ this.sim = sim;
|
|
|
+ setBounds(new Rectangle(0, 0, 460, 300));
|
|
|
+ setResizable(false);
|
|
|
+ getContentPane().setLayout(null);
|
|
|
+
|
|
|
+ JButton btnStartStop = new JButton("Start Simulation");
|
|
|
+ btnStartStop.setBounds(12, 189, 149, 37);
|
|
|
+ getContentPane().add(btnStartStop);
|
|
|
+ btnStartStop.addActionListener(a->startStopButtonAction());
|
|
|
+
|
|
|
+ JButton btnReset = new JButton("Reset");
|
|
|
+ btnReset.setBounds(173, 189, 123, 37);
|
|
|
+ getContentPane().add(btnReset);
|
|
|
+ btnReset.addActionListener(a->resetAction());
|
|
|
+
|
|
|
+ JButton btnChooseStartDate = new JButton("Choose Start Date");
|
|
|
+ btnChooseStartDate.setBounds(12, 13, 161, 25);
|
|
|
+ getContentPane().add(btnChooseStartDate);
|
|
|
+
|
|
|
+ JButton btnStartTime = new JButton("Choose End Date");
|
|
|
+ btnStartTime.setBounds(271, 13, 161, 25);
|
|
|
+ getContentPane().add(btnStartTime);
|
|
|
+
|
|
|
+ JLabel lblStarttimeLong = new JLabel("StartTime (long):");
|
|
|
+ lblStarttimeLong.setBounds(12, 70, 128, 16);
|
|
|
+ getContentPane().add(lblStarttimeLong);
|
|
|
+
|
|
|
+ JLabel lblStepDuration = new JLabel("Step duration:");
|
|
|
+ lblStepDuration.setBounds(12, 99, 97, 16);
|
|
|
+ getContentPane().add(lblStepDuration);
|
|
|
+
|
|
|
+ tfStartTimeLong = new JTextField();
|
|
|
+ tfStartTimeLong.setText("0");
|
|
|
+ tfStartTimeLong.setBounds(116, 67, 116, 22);
|
|
|
+ getContentPane().add(tfStartTimeLong);
|
|
|
+ tfStartTimeLong.setColumns(10);
|
|
|
+
|
|
|
+ tfStepDuration = new JTextField();
|
|
|
+ tfStepDuration.setText("1000");
|
|
|
+ tfStepDuration.setBounds(116, 99, 116, 22);
|
|
|
+ getContentPane().add(tfStepDuration);
|
|
|
+ tfStepDuration.setColumns(10);
|
|
|
+
|
|
|
+ JLabel lblEndtimeLong = new JLabel("EndTime (long):");
|
|
|
+ lblEndtimeLong.setBounds(244, 70, 93, 16);
|
|
|
+ getContentPane().add(lblEndtimeLong);
|
|
|
+
|
|
|
+ tfEndTimeLong = new JTextField();
|
|
|
+ tfEndTimeLong.setText("1000");
|
|
|
+ tfEndTimeLong.setBounds(347, 67, 85, 22);
|
|
|
+ getContentPane().add(tfEndTimeLong);
|
|
|
+ tfEndTimeLong.setColumns(10);
|
|
|
+
|
|
|
+ JLabel lblCurrentTime = new JLabel("Current Time:");
|
|
|
+ lblCurrentTime.setBounds(12, 160, 97, 16);
|
|
|
+ getContentPane().add(lblCurrentTime);
|
|
|
+
|
|
|
+ lbCurrentTimeShow = new JLabel("0 ms");
|
|
|
+ lbCurrentTimeShow.setBounds(121, 160, 111, 16);
|
|
|
+ getContentPane().add(lbCurrentTimeShow);
|
|
|
+
|
|
|
+ progressBar = new JProgressBar();
|
|
|
+ progressBar.setBounds(0, 251, 442, 14);
|
|
|
+ getContentPane().add(progressBar);
|
|
|
+ progressBar.setValue(0);
|
|
|
+
|
|
|
+ JCheckBox chckbxPrintpackets = new JCheckBox("printPackets");
|
|
|
+ chckbxPrintpackets.setBounds(116, 127, 113, 25);
|
|
|
+ chckbxPrintpackets.setSelected(sim.getPrintPackets());
|
|
|
+ getContentPane().add(chckbxPrintpackets);
|
|
|
+ chckbxPrintpackets.addActionListener(l->sim.setPrintPackets(chckbxPrintpackets.isSelected()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void startStopButtonAction(){
|
|
|
+ if(timer == null){
|
|
|
+ timer = new Timer(0, a->simulateTimeStep());
|
|
|
+ currentTime = Long.parseLong(tfStartTimeLong.getText());
|
|
|
+ endTime = Long.parseLong(tfEndTimeLong.getText());
|
|
|
+ startTime = Long.parseLong(tfStartTimeLong.getText());
|
|
|
+ duration = Long.parseLong(tfStepDuration.getText());
|
|
|
+ progressBar.setMinimum((int) Long.parseLong(tfStartTimeLong.getText()));
|
|
|
+ progressBar.setMaximum((int) Long.parseLong(tfEndTimeLong.getText()));
|
|
|
+ progressBar.setValue((int) Long.parseLong(tfStartTimeLong.getText()));
|
|
|
+ lbCurrentTimeShow.setText(currentTime+" ms");
|
|
|
+ }
|
|
|
+ if(timer.isRunning()){
|
|
|
+ timer.stop();
|
|
|
+ }else{
|
|
|
+ timer.start();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void resetAction(){
|
|
|
+ timer.stop();
|
|
|
+ try {
|
|
|
+ currentTime = Long.parseLong(tfStartTimeLong.getText());
|
|
|
+ endTime = Long.parseLong(tfEndTimeLong.getText());
|
|
|
+ startTime = Long.parseLong(tfStartTimeLong.getText());
|
|
|
+ duration = Long.parseLong(tfStepDuration.getText());
|
|
|
+ progressBar.setMinimum((int) startTime);
|
|
|
+ progressBar.setMaximum((int) endTime);
|
|
|
+ progressBar.setValue((int) startTime);
|
|
|
+ lbCurrentTimeShow.setText(currentTime+"");
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void simulateTimeStep(){
|
|
|
+ if(currentTime>endTime){
|
|
|
+ timer.stop();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ sim.simulateTimeIntervall(currentTime,duration);
|
|
|
+ currentTime += duration;
|
|
|
+ progressBar.setValue((int) currentTime);
|
|
|
+ lbCurrentTimeShow.setText(currentTime+"");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ SimulationConfigurator simConfig = new SimulationConfigurator(new SimulationManager(new Model()));
|
|
|
+ simConfig.setEnabled(true);
|
|
|
+ simConfig.setVisible(true);
|
|
|
+
|
|
|
+ }
|
|
|
+}
|