Browse Source

Adds UI for defining the connection label

Andreas T. Meyer-Berg 4 years ago
parent
commit
388c39f592

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

@@ -207,7 +207,7 @@ public interface Connection {
 	 * Returns the default label for packets of this connection represented as a short
 	 * @return default label value
 	 */
-	public short getDefaultLabel();
+	public short getLabel();
 	
 	/**
 	 * Set the default label value for packets of this connection

+ 2 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/ConnectionPerformance.java

@@ -27,7 +27,7 @@ public class ConnectionPerformance implements Connection {
 	/** Link on which this connection runs */
 	protected String name;
 	/** default label */
-	protected short label;
+	protected short label = 0;
 	/**
 	 * Initializes the connection, adds participants of the protocol
 	 * 
@@ -172,7 +172,7 @@ public class ConnectionPerformance implements Connection {
 	}
 
 	@Override
-	public short getDefaultLabel() {
+	public short getLabel() {
 		return label;
 	}
 

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleConnection.java

@@ -210,7 +210,7 @@ public class SimpleConnection implements Connection {
 	}
 
 	@Override
-	public short getDefaultLabel() {
+	public short getLabel() {
 		return label;
 	}
 

+ 52 - 6
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/ConnectionCreationPanel.java

@@ -158,6 +158,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	 * Should not update if mutex is true
 	 */
 	private boolean mutex = true;
+	private JTextField tfLabel;
 
 
 	/**
@@ -165,6 +166,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	 * @param connection connection which should be edited
 	 * @param controller controller for manipulation
 	 * @param frame parent frame
+	 * @wbp.parser.constructor
 	 */// @wbp.parser.constructor for Eclipse:WindowBuilder
 	public ConnectionCreationPanel(Connection connection, Controller controller, Window frame) {
 		this.controller = controller;
@@ -216,9 +218,9 @@ public class ConnectionCreationPanel extends JScrollPane {
 
 		// Sets up window
 		setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
-		this.setPreferredSize(new Dimension(600, 220 + ports.length * 20));
+		this.setPreferredSize(new Dimension(600, 250 + ports.length * 20));
 		content = new JPanel();
-		content.setPreferredSize(new Dimension(600, 200 + ports.length * 20));
+		content.setPreferredSize(new Dimension(600, 240 + ports.length * 20));
 		this.setViewportView(content);
 		content.setLayout(null);
 
@@ -484,6 +486,8 @@ public class ConnectionCreationPanel extends JScrollPane {
 			}
 
 			private void updateConnectionAndField() {
+				if(mutex)
+					return;
 				try {
 					double newRate = Double.parseDouble(tfPacketLossRate.getText());
 					if (newRate >= 0.0 && newRate <= 1.0) {
@@ -498,6 +502,46 @@ public class ConnectionCreationPanel extends JScrollPane {
 			}
 		});
 
+
+		JLabel lblLabel = new JLabel("Label:");
+		lblLabel.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblLabel.setBounds(10, 160, 140, 20);
+		content.add(lblLabel);
+		
+		tfLabel = new JTextField();
+		tfLabel.setColumns(10);
+		tfLabel.setBounds(155, 160, 90, 20);
+		content.add(tfLabel);
+		tfLabel.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() {
+				if(mutex)
+					return;
+				try {
+					short newLabel = Short.parseShort(tfLabel.getText());
+					connection.setLabel(newLabel);
+					tfLabel.setBackground(Color.WHITE);
+				} catch (Exception exception) {
+					tfLabel.setBackground(Color.RED);
+				}
+			}
+		});
+		
 		JLabel lblStatus = new JLabel("Status:");
 		lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
 		lblStatus.setBounds(250, 130, 60, 20);
@@ -524,7 +568,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 		});
 
 		JButton btnCreate = new JButton("Verify and Create");
-		btnCreate.setBounds(125, 160, 206, 25);
+		btnCreate.setBounds(127, 186, 206, 25);
 		content.add(btnCreate);
 
 		btnCreate.addActionListener(new ActionListener() {
@@ -626,15 +670,14 @@ public class ConnectionCreationPanel extends JScrollPane {
 		 */
 		JLabel lblRole = new JLabel("Roles:");
 		lblRole.setHorizontalAlignment(SwingConstants.CENTER);
-		lblRole.setBounds(350, 165, 50, 18);
+		lblRole.setBounds(345, 189, 50, 18);
 		lblRole.setToolTipText("The roles each device plays in the connection, can be defined below. It defines how the device acts.");
 		content.add(lblRole);
 		
-		
 		/**
 		 * Height of the current Box which is being created
 		 */
-		int currentHeight = 190;
+		int currentHeight = 220;
 
 		cmbPortRoles = new JComboBox[ports.length];
 		for (int i = 0; i < ports.length; i++) {
@@ -750,6 +793,9 @@ public class ConnectionCreationPanel extends JScrollPane {
 		// Update packet loss field
 		tfPacketLossRate.setText("" + connection.getPacketLossProbability());
 
+		// Update Label
+		tfLabel.setText("" + connection.getLabel());
+		
 		// Update Status
 		cmbStatus.setSelectedIndex(connection.getStatus());