Sfoglia il codice sorgente

Fixes various minor bugs

* RoleSelection not removing empty Connections/Links
* ProtocolType comboBox not removing the old protocols on refresh
* PortEditorPanel labels being too short on linux machines
* LinkTypeChange deleting the link -> Adds Controller.changeLinkType()
Andreas T. Meyer-Berg 5 anni fa
parent
commit
e8e0a6da5f

+ 50 - 7
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -397,9 +397,14 @@ public class Controller {
 		
 	}
 
-	public void removeLinkFromDevice(Link newLink, SmartDevice smartDevice) {
-		newLink.removeDevice(smartDevice);
-		smartDevice.removeLink(newLink);
+	public void removeLinkFromDevice(Link link, SmartDevice smartDevice) {
+		if(smartDevice!=null)
+			smartDevice.removeLink(link);
+		if(link!=null){
+			link.removeDevice(smartDevice);
+			if(link.getDevices().size()==0)
+				deleteLink(link);
+		}
 	}
 	
 	/**
@@ -414,10 +419,7 @@ public class Controller {
 	public boolean changeRoleOfDevice(Protocol protocol, Connection con, Port device, int newRole){
 		if(newRole < 0 || newRole >= protocol.getNumberOfRoles()){
 			protocol.removeDevice(device);
-			if(con.getParticipants().contains(device))
-				con.removeSmartDevice(device);
-			if(device.getConnection()!=null)
-				device.setConnection(null);
+			removeDeviceFromConnection(device, con);
 			return false;
 		} else if(protocol.getDevicesWithRole(newRole).contains(device)){
 			if(!con.getParticipants().contains(device))
@@ -454,6 +456,9 @@ public class Controller {
 		if(connection != null){
 			connection.removeSmartDevice(p);
 			connection.getProtocol().removeDevice(p);
+			//TODO: Protocol ?
+			if(connection.getProtocol()!=null)
+				connection.getProtocol().removeDevice(p);
 			if(connection.getParticipants().size() == 0)
 				deleteConnection(connection);
 		}
@@ -673,4 +678,42 @@ public class Controller {
 	public SimulationManager getSimulationManager() {
 		return model.getSim();
 	}
+
+	/**
+	 * Changes the type of the link to new type, specified by the given Link-class
+	 * @param oldLink oldLink, whose attributes will b copied to the new Link 
+	 * @param newType Type/Class of the new Link
+	 * @return newly created Link, null on failure/error
+	 */
+	public Link changeLinkType(Link oldLink, Class<? extends Link> newType) {
+		if(newType == null)
+			return null;
+		/**
+		 * New Link which was created
+		 */
+		Link newLink = null;
+		try{
+			newLink = newType.newInstance();
+		}catch(Exception e){
+			return null;
+		}
+		if (newLink == null) {
+			return null;
+		}else {
+			// Set old Name
+			newLink.setName(oldLink.getName());
+			// Add to Mode
+			if(getLinks().contains(oldLink)){
+				removeLink(oldLink);
+				addLink(newLink);
+			}
+			// Add devices to the new Link
+			LinkedList<SmartDevice> devices= new LinkedList<>(oldLink.getDevices());
+			for(SmartDevice device:devices){
+				removeLinkFromDevice(oldLink, device);
+				addLinkToDevice(newLink, device);
+			}
+		return newLink;
+		}
+	}
 }

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

@@ -523,7 +523,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 			public void actionPerformed(ActionEvent e) {
 
 				if (!edit) {
-					if (!connection.getLink().getConnections()
+					if (connection.getLink()!=null && !connection.getLink().getConnections()
 							.contains(connection))
 						connection.getLink().addConnection(connection);
 					for (int i = 0; i < ports.length; i++) {
@@ -688,7 +688,6 @@ public class ConnectionCreationPanel extends JScrollPane {
 		cmbStatus.setSelectedIndex(connection.getStatus());
 
 		// Update selected Connection
-		availableConnection = controller.getControllerImport().getConnections();
 		cmbConnection.removeAllItems();
 		for (Class<? extends Connection> c : availableConnection)
 			cmbConnection.addItem(c.getSimpleName());
@@ -712,6 +711,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 		cmbSelectedLink.setSelectedIndex(lastLinkIndex);
 
 		// Update protocol
+		cmbProtocolType.removeAllItems();
 		for (int i = 0; i < availableProtocols.size(); i++)
 			try {
 				cmbProtocolType.addItem(availableProtocols.get(i).newInstance()

+ 4 - 22
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/LinkCreationPanel.java

@@ -127,31 +127,13 @@ public class LinkCreationPanel extends JScrollPane{
 				/**
 				 * Imported Link which should be created and configured
 				 */
-				Link importedLink = null;
-				try {
-					//Create new Instance of the Link
-					importedLink = availableLinks.get(
-							cmbLinkType.getSelectedIndex()).newInstance();
-				} catch (Exception e1) {
-					System.out
-							.println("WARNING: Link could not be initialized");
-				}
+				Link importedLink = controller.changeLinkType(newLink, availableLinks.get(
+							cmbLinkType.getSelectedIndex()));
 				if (importedLink == null) {
-					cmbLinkType.setSelectedIndex(lastIndex);
 					System.out
-							.println("WARNING: Invalid Protocol Selected - restore last index");
+					.println("WARNING: Invalid Protocol Selected or failure on initialization - restore last index");
+					cmbLinkType.setSelectedIndex(lastIndex);
 				}else {
-					importedLink.setName(newLink.getName());
-					for(SmartDevice device:devices){
-						if(newLink.getDevices().contains(device)){
-							controller.removeLinkFromDevice(newLink, device);
-							controller.addLinkToDevice(importedLink, device);
-						}
-					}
-					if(controller.getLinks().contains(newLink)){
-						controller.removeLink(newLink);
-						controller.addLink(importedLink);
-					}
 					newLink = importedLink;
 					lastIndex = cmbLinkType.getSelectedIndex();
 				}

+ 22 - 21
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/popups/PortEditorPanel.java

@@ -105,14 +105,14 @@ public class PortEditorPanel extends JPanel
         String portNumberToolTip = "<html>Number of this port, should be a 16bit short [0 - 65335].<br> A Port describes the Endpoint of an Connection.</html>";
         JLabel lblPortnumber = new JLabel("PortNumber:");
         lblPortnumber.setHorizontalAlignment(SwingConstants.RIGHT);
-        lblPortnumber.setBounds(10, 10, 110, 20);
+        lblPortnumber.setBounds(10, 10, 140, 20);
         lblPortnumber.setToolTipText(portNumberToolTip);
         editPanel.add(lblPortnumber);
          
         JScrollPane portsScrollPane = new JScrollPane(editPanel);
         
         tfPortNumber = new JTextField();
-        tfPortNumber.setBounds(130, 10, 130, 20);
+        tfPortNumber.setBounds(160, 10, 130, 20);
         editPanel.add(tfPortNumber);
         tfPortNumber.setColumns(10);
         tfPortNumber.addFocusListener(this);
@@ -122,17 +122,17 @@ public class PortEditorPanel extends JPanel
         
         JLabel lblProtocol = new JLabel("Protocol:");
         lblProtocol.setHorizontalAlignment(SwingConstants.RIGHT);
-        lblProtocol.setBounds(10, 40, 110, 20);
+        lblProtocol.setBounds(10, 40, 140, 20);
         lblProtocol.setToolTipText("Protocol which this Port participates in.");
         editPanel.add(lblProtocol);
         
         lblProtocolName = new JLabel("Protocol");
-        lblProtocolName.setBounds(130, 40, 60, 20);
+        lblProtocolName.setBounds(160, 40, 60, 20);
         lblProtocolName.setToolTipText("Name of the protocol, which is performed via this Port.");
         editPanel.add(lblProtocolName);
 
         JButton btnEditConnection = new JButton("Edit");
-        btnEditConnection.setBounds(200, 40, 60, 20);
+        btnEditConnection.setBounds(230, 40, 60, 20);
         btnEditConnection.setToolTipText("Edit the Connection & Protocol performed via this port.");
         editPanel.add(btnEditConnection);
         btnEditConnection.addActionListener(a->{
@@ -149,7 +149,7 @@ public class PortEditorPanel extends JPanel
         JLabel lblStatus = new JLabel("Status:");
         lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
         //lblStatus.setBounds(66, 66, 56, 16);
-        lblStatus.setBounds(10, 70, 110, 20);
+        lblStatus.setBounds(10, 70, 140, 20);
         lblStatus.setToolTipText(toolTipStatus);
         editPanel.add(lblStatus);
         
@@ -158,7 +158,7 @@ public class PortEditorPanel extends JPanel
         cmbStatus.addItem(Port.statusToString(Port.OPEN));
         cmbStatus.addItem(Port.statusToString(Port.SENDING));
         cmbStatus.addItem(Port.statusToString((short) 4));
-        cmbStatus.setBounds(130, 70, 130, 20);
+        cmbStatus.setBounds(160, 70, 130, 20);
         editPanel.add(cmbStatus);
         cmbStatus.addFocusListener(this);
         cmbStatus.addActionListener(this);
@@ -168,12 +168,12 @@ public class PortEditorPanel extends JPanel
         String toolTipTrigger = "<html>Interval between to triggers of this port.<br> This port will sent a new outgoing packet every *triggerIntervall* milliseconds.<html>";
         JLabel lblTriggerInterval = new JLabel("Trigger Interval:");
         lblTriggerInterval.setHorizontalAlignment(SwingConstants.RIGHT);
-        lblTriggerInterval.setBounds(10, 100, 110, 20);
+        lblTriggerInterval.setBounds(10, 100, 140, 20);
         lblTriggerInterval.setToolTipText(toolTipTrigger);
         editPanel.add(lblTriggerInterval);
         
         tfTriggerInterval = new JTextField();
-        tfTriggerInterval.setBounds(130, 100, 130, 20);
+        tfTriggerInterval.setBounds(160, 100, 130, 20);
         editPanel.add(tfTriggerInterval);
         tfTriggerInterval.setColumns(10);
         tfTriggerInterval.addFocusListener(this);
@@ -182,7 +182,7 @@ public class PortEditorPanel extends JPanel
         tfTriggerInterval.setToolTipText(toolTipTrigger);
         
         JLabel lblTriggerIntervalUnit = new JLabel("ms");
-        lblTriggerIntervalUnit.setBounds(270, 100, 50, 20);
+        lblTriggerIntervalUnit.setBounds(3000, 100, 50, 20);
         editPanel.add(lblTriggerIntervalUnit);
         
         
@@ -191,12 +191,12 @@ public class PortEditorPanel extends JPanel
         
         JLabel lblResponseTime = new JLabel("Response Time:");
         lblResponseTime.setHorizontalAlignment(SwingConstants.RIGHT);
-        lblResponseTime.setBounds(10, 130, 110, 20);
+        lblResponseTime.setBounds(10, 130, 140, 20);
         lblResponseTime.setToolTipText(toolTipResponse);
         editPanel.add(lblResponseTime);
         
         tfResponseTime = new JTextField();
-        tfResponseTime.setBounds(130, 130, 130, 20);
+        tfResponseTime.setBounds(160, 130, 130, 20);
         editPanel.add(tfResponseTime);
         tfResponseTime.setColumns(10);
         tfResponseTime.addFocusListener(this);
@@ -205,7 +205,7 @@ public class PortEditorPanel extends JPanel
         tfResponseTime.setToolTipText(toolTipResponse);
         
         JLabel lblResponseTimeUnit = new JLabel("ms");
-        lblResponseTimeUnit.setBounds(270, 130, 50, 20);
+        lblResponseTimeUnit.setBounds(300, 130, 50, 20);
         editPanel.add(lblResponseTimeUnit);
         
         String toolTipLastTrigger = "<html>Timestep in milliseconds, where this port sent its last packet.<br>"
@@ -213,12 +213,12 @@ public class PortEditorPanel extends JPanel
         
         JLabel lblLasttrigger = new JLabel("LastTrigger:");
         lblLasttrigger.setHorizontalAlignment(SwingConstants.RIGHT);
-        lblLasttrigger.setBounds(10, 160, 110, 20);
+        lblLasttrigger.setBounds(10, 160, 140, 20);
         lblLasttrigger.setToolTipText(toolTipLastTrigger);
         editPanel.add(lblLasttrigger);
         
         tfLastTrigger = new JTextField();
-        tfLastTrigger.setBounds(130, 160, 130, 20);
+        tfLastTrigger.setBounds(160, 160, 130, 20);
         editPanel.add(tfLastTrigger);
         tfLastTrigger.setColumns(10);
         tfLastTrigger.addFocusListener(this);
@@ -227,25 +227,25 @@ public class PortEditorPanel extends JPanel
         tfLastTrigger.setToolTipText(toolTipLastTrigger);
         
         JLabel lblLastTriggerUnit = new JLabel("ms");
-        lblLastTriggerUnit.setBounds(270, 160, 50, 20);
+        lblLastTriggerUnit.setBounds(300, 160, 50, 20);
         editPanel.add(lblLastTriggerUnit);
         
         String toolTipJitter = "<html>Jitter describes the differences between the minimum and maximum delay of a connection.<br>"
                 + "In this takes the Port may send packets jitter/2 ms earlier or later</html>";
         JLabel lblJitter = new JLabel("Jitter:");
         lblJitter.setHorizontalAlignment(SwingConstants.RIGHT);
-        lblJitter.setBounds(10, 190, 110, 20);
+        lblJitter.setBounds(10, 190, 140, 20);
         lblJitter.setToolTipText(toolTipJitter);
         editPanel.add(lblJitter);
         
         tfJitter = new JTextField();
-        tfJitter.setBounds(130, 190, 130, 20);
+        tfJitter.setBounds(160, 190, 130, 20);
         editPanel.add(tfJitter);
         tfJitter.setColumns(10);
         tfJitter.setToolTipText(toolTipJitter);
         
         JLabel lblJitterUnit = new JLabel("ms");
-        lblJitterUnit.setBounds(270, 190, 50, 20);
+        lblJitterUnit.setBounds(300, 190, 50, 20);
         editPanel.add(lblJitterUnit);
         tfJitter.addFocusListener(this);
         tfJitter.addActionListener(this);
@@ -258,13 +258,13 @@ public class PortEditorPanel extends JPanel
         /**
          * minimum sizes of the panels
          */
-        Dimension minimumSize = new Dimension(60, 50);
+        Dimension minimumSize = new Dimension(150, 60);
         listScrollPane.setMinimumSize(minimumSize);
         listScrollPane.setMaximumSize(minimumSize);
         portsScrollPane.setMinimumSize(minimumSize);
  
         // Set the preferred size
-        splitPane.setPreferredSize(new Dimension(380, 220));
+        splitPane.setPreferredSize(new Dimension(500, 240));
         if(!toEdit.getPorts().isEmpty())
         	updateLabel(toEdit.getPorts().get(list.getSelectedIndex()));
     }
@@ -333,6 +333,7 @@ public class PortEditorPanel extends JPanel
         frame.getContentPane().add(portEditorPanel.getSplitPane());
  
         frame.pack();
+        frame.revalidate();
         frame.setVisible(true);
     }