Browse Source

Adds EditConnection Functionality to the PortEditorPanel

Andreas T. Meyer-Berg 6 years ago
parent
commit
837cef3b74

+ 40 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/ConnectionCreationDialog.java

@@ -0,0 +1,40 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.view;
+
+import java.awt.Component;
+import java.util.Collection;
+
+import javax.swing.JDialog;
+
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
+
+@SuppressWarnings("serial")
+public class ConnectionCreationDialog extends JDialog {
+ConnectionCreationPanel content;
+Component parent;
+
+	public ConnectionCreationDialog(Connection connection, Controller controller, Component panel) {
+		super();
+		content = new ConnectionCreationPanel(connection, controller);
+		parent = panel;
+		initialize();
+	}
+	
+	public ConnectionCreationDialog(Collection<Port> devices, Link link, Controller controller, Component panel){
+		super();
+		content = new ConnectionCreationPanel(devices, link, controller);
+		parent = panel;
+		initialize();
+	}
+
+	private void initialize() {
+		content.setFrame(this);
+		this.setModal(true);
+		this.setContentPane(content);
+		this.pack();
+		setLocationRelativeTo(parent);
+		this.setVisible(true);
+	}
+}

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

@@ -107,7 +107,12 @@ public class ConnectionCreationPanel extends JScrollPane {
 	public ConnectionCreationPanel(Connection connection, Controller controller) {
 		this.controller = controller;
 		this.connection = connection;
-		ports = (Port[]) connection.getParticipants().toArray();
+		ports = new Port[connection.getParticipants().size()];
+		int i = 0;
+		for (Port d : connection.getParticipants()) {
+			// controller.addLinkToDevice(connection, d);
+			this.ports[i++] = d;
+		}
 		edit = true;
 		initializePanel();
 	}

+ 29 - 7
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/PortEditorPanel.java

@@ -11,8 +11,16 @@ import java.awt.event.MouseListener;
 import javax.swing.*;
 import javax.swing.event.*;
 
+import junit.framework.TestListener;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ConnectionImplementation;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.protocols.MQTT_protocol;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  
 /**
  * Panel for editing of Ports of a SmartDevice
@@ -73,8 +81,9 @@ public class PortEditorPanel extends JPanel
     /**
      * Creates a new PortEditorPanel, which allows editing of the Ports of toEdit
      * @param toEdit SmartDevice, which ports will be edited
+     * @param controller Controller to modify the Model
      */
-    public PortEditorPanel(SmartDevice toEdit) {
+    public PortEditorPanel(SmartDevice toEdit, Controller controller) {
     	
     	this.toEdit = toEdit;
         //Create the list of ports
@@ -98,7 +107,7 @@ public class PortEditorPanel extends JPanel
         lblPortnumber.setBounds(10, 10, 110, 20);
         editPanel.add(lblPortnumber);
          
-        JScrollPane pictureScrollPane = new JScrollPane(editPanel);
+        JScrollPane portsScrollPane = new JScrollPane(editPanel);
         
         tfPortNumber = new JTextField();
         tfPortNumber.setBounds(130, 10, 130, 20);
@@ -120,6 +129,7 @@ public class PortEditorPanel extends JPanel
         JButton btnEditConnection = new JButton("Edit");
         btnEditConnection.setBounds(200, 40, 60, 20);
         editPanel.add(btnEditConnection);
+        btnEditConnection.addActionListener(a->new ConnectionCreationDialog(list.getSelectedValue().getConnection(), controller, this));
         
         JLabel lblStatus = new JLabel("Status:");
         lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
@@ -192,7 +202,7 @@ public class PortEditorPanel extends JPanel
         tfJitter.addMouseListener(this);
         
         splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
-                                   listScrollPane, pictureScrollPane);
+                                   listScrollPane, portsScrollPane);
         splitPane.setDividerLocation(90);
  
         /**
@@ -201,7 +211,7 @@ public class PortEditorPanel extends JPanel
         Dimension minimumSize = new Dimension(60, 50);
         listScrollPane.setMinimumSize(minimumSize);
         listScrollPane.setMaximumSize(minimumSize);
-        pictureScrollPane.setMinimumSize(minimumSize);
+        portsScrollPane.setMinimumSize(minimumSize);
  
         // Set the preferred size
         splitPane.setPreferredSize(new Dimension(380, 220));
@@ -255,9 +265,21 @@ public class PortEditorPanel extends JPanel
         JFrame frame = new JFrame("Port Editor");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         SmartDevice test = new SmartDevice("TestDevice");
-        test.addPort(new Port(test, (short) 1));
-        test.addPort(new Port(test, (short) 2));
-        PortEditorPanel portEditorPanel = new PortEditorPanel(test);
+        Port p1 = new Port(test, (short) 1);
+        Port p2 = new Port(test, (short) 2);
+        test.addPort(p1);
+        test.addPort(p2);
+        MQTT_protocol testProtocol = new MQTT_protocol();
+        testProtocol.addDeviceOfRole(p1, 0);
+        testProtocol.addDeviceOfRole(p2, 1);
+        Link test_link = new SimpleLink("Test");
+        test_link.addDevice(test);
+        Connection con = new ConnectionImplementation(test_link, testProtocol);
+        p1.setConnection(con);
+        p2.setConnection(con);
+        con.addSmartDevice(p1);
+        con.addSmartDevice(p2);
+        PortEditorPanel portEditorPanel = new PortEditorPanel(test, new Controller(new Model()));
         frame.getContentPane().add(portEditorPanel.getSplitPane());
  
         frame.pack();

+ 1 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/SmartDeviceCreationPopUp.java

@@ -162,7 +162,7 @@ public class SmartDeviceCreationPopUp extends JDialog {
 		LinkEditorPanel panelLinks = new LinkEditorPanel(newDevice, control);
 		tabbedPane.addTab("Links", null, panelLinks, "Edit Links of the SmartDevice");
 
-		PortEditorPanel panelConnections = new PortEditorPanel(newDevice);
+		PortEditorPanel panelConnections = new PortEditorPanel(newDevice, control);
 		tabbedPane.addTab("Connections", null, panelConnections.getSplitPane(), "Edit Connections/Ports/Timings of the SmartDevice");
 		panelConnections.setEnabled(true);