Selaa lähdekoodia

Adds PacketLossRate and ConnectionStatus to the ConnectionCreationPanel

Andreas T. Meyer-Berg 6 vuotta sitten
vanhempi
commit
e70b2465df

+ 22 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Connection.java

@@ -165,4 +165,26 @@ public interface Connection {
 	 * @param name new name of the connection
 	 */
 	public void setName(String name);
+	
+	/**
+	 * Returns a String representation of the different status values
+	 * @param status status to be converted
+	 * @return String representation of the status
+	 */
+	public static String getStatusName(byte status){
+		switch (status) {
+		case DONE:
+			return "DONE";
+		case ACTIVE:
+			return "ACTIVE";
+		case FINISHED:
+			return "FINISHED";
+		case TERMINATED:
+			return "TERMINATED";
+		case HALTED:
+			return "HALTED";
+		default:
+			return "unknown";
+		}
+	}
 }

+ 77 - 7
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/ConnectionCreationPanel.java

@@ -1,5 +1,6 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
 
+import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
@@ -29,6 +30,8 @@ import javax.swing.JTextField;
 import javax.swing.SwingConstants;
 import javax.swing.JComboBox;
 import javax.swing.JButton;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
 
 @SuppressWarnings("serial")
 public class ConnectionCreationPanel extends JScrollPane {
@@ -96,6 +99,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	 * via controller
 	 */
 	private LinkedList<Class<? extends Protocol>> availableProtocols;
+	private JTextField tfPacketLossRate;
 
 	/**
 	 * @wbp.parser.constructor
@@ -139,17 +143,17 @@ public class ConnectionCreationPanel extends JScrollPane {
 		content.setLayout(null);
 
 		JLabel lblDescription = new JLabel("Connection Creation");
-		lblDescription.setBounds(12, 13, 476, 16);
+		lblDescription.setBounds(12, 0, 144, 16);
 		content.add(lblDescription);
 
 		JLabel lblName = new JLabel("Name:");
 		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
-		lblName.setBounds(12, 42, 138, 16);
+		lblName.setBounds(10, 10, 140, 20);
 		content.add(lblName);
 
 		tfName = new JTextField();
 		tfName.setText(connection.getName());
-		tfName.setBounds(162, 39, 300, 22);
+		tfName.setBounds(155, 10, 290, 20);
 		content.add(tfName);
 		tfName.setColumns(10);
 		tfName.addActionListener(new ActionListener() {
@@ -162,7 +166,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 
 		JLabel lblLinkType = new JLabel("Type:");
 		lblLinkType.setHorizontalAlignment(SwingConstants.RIGHT);
-		lblLinkType.setBounds(12, 71, 138, 16);
+		lblLinkType.setBounds(10, 40, 140, 20);
 		content.add(lblLinkType);
 
 		JComboBox<String> cmbLinkType = new JComboBox<String>();
@@ -174,7 +178,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 				System.out.println("Protocol " + i + " is invalid");
 				cmbLinkType.addItem("unknown");
 			}
-		cmbLinkType.setBounds(162, 68, 116, 22);
+		cmbLinkType.setBounds(155, 40, 125, 20);
 		content.add(cmbLinkType);
 		// Set Index to selected Protocol
 		for (int i = 0; i < availableProtocols.size(); i++)
@@ -257,7 +261,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 		});
 
 		JButton btnImportLink = new JButton("Import Link Type");
-		btnImportLink.setBounds(290, 67, 144, 25);
+		btnImportLink.setBounds(290, 40, 155, 20);
 		content.add(btnImportLink);
 		btnImportLink.addActionListener(a -> System.out
 				.println("WARNING: No import yet"));
@@ -265,6 +269,72 @@ public class ConnectionCreationPanel extends JScrollPane {
 		JButton btnCreate = new JButton("Verify and Create");
 		btnCreate.setBounds(121, 103, 206, 25);
 		content.add(btnCreate);
+		
+		JLabel lblStatus = new JLabel("Status:");
+		lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblStatus.setBounds(260, 70, 60, 20);
+		content.add(lblStatus);
+		
+		JComboBox<String> cmbStatus = new JComboBox<String>();
+		for(int i = 0; i<5; i++)
+			cmbStatus.addItem(Connection.getStatusName((byte)i));
+		cmbStatus.setBounds(320, 70, 125, 20);
+		cmbStatus.setSelectedIndex(connection.getStatus());
+		content.add(cmbStatus);
+		cmbStatus.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				if(cmbStatus.getSelectedIndex()<0||cmbStatus.getSelectedIndex()>4)
+					cmbStatus.setSelectedIndex(connection.getStatus());
+				else
+					connection.setStatus((byte) cmbStatus.getSelectedIndex());
+				
+			}
+		});
+
+		JLabel lblPacketlossrate = new JLabel("PacketLossRate:");
+		lblPacketlossrate.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblPacketlossrate.setBounds(10, 70, 140, 20);
+		lblPacketlossrate.setToolTipText("Probability of Packet loss, should be a Double in [0.0,1.0]");
+		content.add(lblPacketlossrate);
+		
+		tfPacketLossRate = new JTextField();
+		tfPacketLossRate.setBounds(155, 70, 90, 20);
+		content.add(tfPacketLossRate);
+		tfPacketLossRate.setColumns(10);
+		tfPacketLossRate.setText(""+connection.getPacketLossProbability());
+		tfPacketLossRate.getDocument().addDocumentListener(new DocumentListener() {
+			
+			@Override
+			public void removeUpdate(DocumentEvent e) {
+				updateConnectionAndField();
+			}
+			
+			@Override
+			public void insertUpdate(DocumentEvent e) {
+				updateConnectionAndField();
+			}
+			
+			@Override
+			public void changedUpdate(DocumentEvent e) {
+				updateConnectionAndField();
+			}
+			
+			private void updateConnectionAndField(){
+				try{
+					double newRate = Double.parseDouble(tfPacketLossRate.getText());
+					if(newRate >= 0.0 && newRate <= 1.0){
+						connection.setPacketLossProbability(newRate);
+						tfPacketLossRate.setBackground(Color.WHITE);
+					}else{
+						tfPacketLossRate.setBackground(Color.RED);
+					}
+				}catch(Exception exception){
+					tfPacketLossRate.setBackground(Color.RED);					
+				}
+			}
+		});
+		
 		btnCreate.addActionListener(new ActionListener() {
 
 			@Override
@@ -367,7 +437,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	 * Test the Panel
 	 */
 	private static void testGUI() {
-		JFrame frame = new JFrame("LinkCreation Panel");
+		JFrame frame = new JFrame("Connection Creation Panel");
 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
 		ConnectionCreationPanel panel;