浏览代码

Adds protocol role change to controller and ConnectionCreationPanel

Andreas T. Meyer-Berg 6 年之前
父节点
当前提交
db528258b5

+ 12 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -6,6 +6,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
 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.Protocol;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 
 /**
@@ -346,4 +347,15 @@ public class Controller {
 		newLink.removeDevice(smartDevice);
 		smartDevice.removeLink(newLink);
 	}
+	
+	public void changeRoleOfDevice(Protocol protocol, Port device, int newRole){
+		if(newRole < 0 || newRole >= protocol.getNumberOfRoles())
+			protocol.removeDevice(device);
+		else if(protocol.getDevicesWithRole(newRole).contains(device))
+			return;
+		else{
+			protocol.removeDevice(device);
+			protocol.addDeviceOfRole(device, newRole);
+		}
+	}
 }

+ 23 - 14
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/ConnectionCreationPanel.java

@@ -38,9 +38,11 @@ public class ConnectionCreationPanel extends JScrollPane{
 	
 	private Connection connection;
 	private Port[] ports;
+	private JComboBox<String>[] boxes;
 	private boolean edit;
 	
 	private Controller controller;
+	
 	/**
 	 * @wbp.parser.constructor
 	 */
@@ -110,6 +112,7 @@ public class ConnectionCreationPanel extends JScrollPane{
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				System.out.println("WARNING: No  further Link Types implemented");
+				//Link changing possibility, port all comboboxes - change combobox options
 			}
 		});
 		
@@ -140,29 +143,35 @@ public class ConnectionCreationPanel extends JScrollPane{
 		});
 		
 		int currentHeight = 150;
-		for(int i = 0; i<ports.length;i++){
+		boxes = new JComboBox[ports.length];
+		for(int i = 0; i < ports.length;i++){
 			int pos = i;
-			JLabel lblDevice = new JLabel(ports[i].getOwner().getName()+":"+ports[i].getPortNumber()+":");
+			Port currentPort = ports[i];
+			SmartDevice currentDevice = currentPort.getOwner();
+			JLabel lblDevice = new JLabel(currentDevice.getName()+":"+currentPort.getPortNumber()+":");
 			lblDevice.setHorizontalAlignment(SwingConstants.RIGHT);
 			lblDevice.setBounds(10, currentHeight, 230, 18);
 			content.add(lblDevice);
 			
-			JComboBox<String> cmbDev = new JComboBox<String>();
+			boxes[pos] = new JComboBox<String>();
 			for(String role:connection.getProtocol().getRoles())
-				cmbDev.addItem(role);
-			cmbDev.addItem("Disconnected");
-			cmbDev.setBounds(250, currentHeight, 240, 18);
-			content.add(cmbDev);
-			cmbDev.addActionListener(new ActionListener() {
+				boxes[pos].addItem(role);
+			boxes[pos].addItem("Disconnected");
+			boxes[pos].setBounds(250, currentHeight, 240, 18);
+			content.add(boxes[pos]);
+			boxes[pos].addActionListener(new ActionListener() {
 				
 				@Override
 				public void actionPerformed(ActionEvent e) {
-					/*
-					if(cmbDev.getSelectedIndex()==0 && !connection.getDevices().contains(devices[pos])){
-						controller.addLinkToDevice(connection, devices[pos]);
-					}else if(cmbDev.getSelectedIndex()==1&&connection.getDevices().contains(devices[pos])){
-						controller.removeLinkFromDevice(connection, devices[pos]);
-					}*/
+					int selected = boxes[pos].getSelectedIndex();
+					if(selected==-1)
+						System.out.println("Changed "+lblDevice.getText()+" to -1");
+					else if(selected<connection.getProtocol().getNumberOfRoles())
+						System.out.println("Changed "+lblDevice.getText()+" to "+connection.getProtocol().getRoles()[selected]);
+					else
+						System.out.println("Changed "+lblDevice.getText()+" to Disconnected");
+					
+					controller.changeRoleOfDevice(connection.getProtocol(), ports[pos], selected);
 				}
 			});
 			currentHeight += 20;