Explorar el Código

Adds GUI elements for Link/Connection editing to the ConnectionPanel

Andreas T. Meyer-Berg hace 6 años
padre
commit
04b3813167

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

@@ -104,6 +104,13 @@ public class ConnectionCreationPanel extends JScrollPane {
 	 * via controller
 	 */
 	private LinkedList<Class<? extends Protocol>> availableProtocols;
+	/**
+	 * List of available connections
+	 */
+	private LinkedList<Class<? extends Connection>> availableConnection;
+	/**
+	 * Textfield for the packet loss
+	 */
 	private JTextField tfPacketLossRate;
 
 	/**
@@ -141,11 +148,12 @@ public class ConnectionCreationPanel extends JScrollPane {
 		disconnectedIndex = connection.getProtocol().getNumberOfRoles();
 		
 		availableProtocols = controller.getControllerImport().getProtocols();
+		availableConnection = controller.getControllerImport().getConnections();
 
 		setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
-		this.setPreferredSize(new Dimension(600, 170 + ports.length * 20));
+		this.setPreferredSize(new Dimension(600, 220 + ports.length * 20));
 		content = new JPanel();
-		content.setPreferredSize(new Dimension(600, 150 + ports.length * 20));
+		content.setPreferredSize(new Dimension(600, 200 + ports.length * 20));
 		this.setViewportView(content);
 		content.setLayout(null);
 
@@ -170,34 +178,73 @@ public class ConnectionCreationPanel extends JScrollPane {
 				connection.setName(tfName.getText());
 			}
 		});
+		
+		JLabel lblSelectedLink = new JLabel("Selected link:");
+		lblSelectedLink.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblSelectedLink.setBounds(10, 40, 140, 20);
+		content.add(lblSelectedLink);
+		
+		JComboBox<String> cmbSelectedLink = new JComboBox<String>();
+		cmbSelectedLink.setBounds(155, 40, 125, 20);
+		content.add(cmbSelectedLink);
+		
+		JButton btnCreateLink = new JButton("Create Link");
+		btnCreateLink.setBounds(290, 40, 155, 20);
+		content.add(btnCreateLink);
+		
+		JLabel lblConnectionType = new JLabel("Connection type:");
+		lblConnectionType.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblConnectionType.setBounds(10, 70, 140, 20);
+		content.add(lblConnectionType);
+		
+		JComboBox<String> cmbConnection = new JComboBox<String>();
+		cmbConnection.setBounds(155, 70, 125, 20);
+		content.add(cmbConnection);
+		
+		for (int i = 0; i < availableConnection.size(); i++)
+			cmbConnection.addItem(availableConnection.get(i).getSimpleName());
+
+		cmbConnection.setBounds(155, 70, 125, 20);
+		content.add(cmbConnection);
+		// Set Index to selected Protocol
+		for (int i = 0; i < availableConnection.size(); i++)
+			if (connection.getClass()
+					.equals(availableConnection.get(i))){
+				//Select the right protocol and save last index
+				cmbConnection.setSelectedIndex(i);
+			}
+		
+		JButton btnImportConnection = new JButton("Import Connection");
+		btnImportConnection.setBounds(290, 70, 155, 20);
+		content.add(btnImportConnection);
 
-		JLabel lblLinkType = new JLabel("Type:");
-		lblLinkType.setHorizontalAlignment(SwingConstants.RIGHT);
-		lblLinkType.setBounds(10, 40, 140, 20);
-		content.add(lblLinkType);
+		JLabel lblProtocolType = new JLabel("Protocol Type:");
+		lblProtocolType.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblProtocolType.setBounds(10, 100, 140, 20);
+		content.add(lblProtocolType);
 
-		JComboBox<String> cmbLinkType = new JComboBox<String>();
+		JComboBox<String> cmbProtocolType = new JComboBox<String>();
 		for (int i = 0; i < availableProtocols.size(); i++)
 			try {
-				cmbLinkType.addItem(availableProtocols.get(i).newInstance()
+				cmbProtocolType.addItem(availableProtocols.get(i).newInstance()
 						.getName());
 			} catch (InstantiationException | IllegalAccessException e1) {
 				System.out.println("Protocol " + i + " is invalid");
-				cmbLinkType.addItem("unknown");
+				cmbProtocolType.addItem("unknown");
 			}
-		cmbLinkType.setBounds(155, 40, 125, 20);
-		content.add(cmbLinkType);
+		cmbProtocolType.setBounds(155, 100, 125, 20);
+		content.add(cmbProtocolType);
 		// Set Index to selected Protocol
 		for (int i = 0; i < availableProtocols.size(); i++)
 			if (connection.getProtocol().getClass()
 					.equals(availableProtocols.get(i))){
 				//Select the right protocol and save last index
-				cmbLinkType.setSelectedIndex(i);
+				cmbProtocolType.setSelectedIndex(i);
 				lastIndex = i;
 			}
 
 		// Add Functionality for changing protocol
-		cmbLinkType.addActionListener(new ActionListener() {
+		cmbProtocolType.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				/**
@@ -213,13 +260,13 @@ public class ConnectionCreationPanel extends JScrollPane {
 				try {
 					//Create new Instance of the protocol
 					newProtocol = availableProtocols.get(
-							cmbLinkType.getSelectedIndex()).newInstance();
+							cmbProtocolType.getSelectedIndex()).newInstance();
 				} catch (InstantiationException | IllegalAccessException e1) {
 					System.out
 							.println("WARNING: Protocol could not be initialized");
 				}
 				if (newProtocol == null) {
-					cmbLinkType.setSelectedIndex(lastIndex);
+					cmbProtocolType.setSelectedIndex(lastIndex);
 					System.out
 							.println("WARNING: Invalid Protocol Selected - restore last index");
 				} else if(connection.setProtocol(newProtocol)){
@@ -236,7 +283,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 					//Update Protocol
 						
 					// Update lastIndex to new Index
-					lastIndex = cmbLinkType.getSelectedIndex();
+					lastIndex = cmbProtocolType.getSelectedIndex();
 					//Update Boxes
 					for (int i = 0; i < boxes.length; i++) {
 						/**
@@ -266,17 +313,17 @@ public class ConnectionCreationPanel extends JScrollPane {
 						}
 					}
 				} else {
-					cmbLinkType.setSelectedIndex(lastIndex);
+					cmbProtocolType.setSelectedIndex(lastIndex);
 				}
 				//Set Mutex back to false - allow changes
 				protocolChange = false;
 			}
 		});
 
-		JButton btnImportLink = new JButton("Import Protocol");
-		btnImportLink.setBounds(290, 40, 155, 20);
-		content.add(btnImportLink);
-		btnImportLink.addActionListener(a -> {
+		JButton btnImportProtocol = new JButton("Import Protocol");
+		btnImportProtocol.setBounds(290, 100, 155, 20);
+		content.add(btnImportProtocol);
+		btnImportProtocol.addActionListener(a -> {
 			//Filechooser starting in the base directory
 			JFileChooser fc = new JFileChooser(new File(System.getProperty("user.dir")));
 			fc.setFileFilter(new JavaFileFilter());
@@ -323,7 +370,7 @@ public class ConnectionCreationPanel extends JScrollPane {
 	            		if(controller.getControllerImport().addProtocol(imported)){
 	            			//Update GUI
 	            			try {
-								cmbLinkType.addItem((imported.newInstance()).getName());
+								cmbProtocolType.addItem((imported.newInstance()).getName());
 								availableProtocols.add(imported);
 							} catch (Exception e1) {
 								System.out.println("Adding Protocol to the Links failed");
@@ -340,41 +387,15 @@ public class ConnectionCreationPanel extends JScrollPane {
 	           //Importing Cancelled by user
 	        }
 		});
-
-		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.setBounds(10, 130, 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);
+		tfPacketLossRate.setBounds(155, 130, 90, 20);
 		content.add(tfPacketLossRate);
 		tfPacketLossRate.setColumns(10);
 		tfPacketLossRate.setText(""+connection.getPacketLossProbability());
@@ -410,55 +431,81 @@ public class ConnectionCreationPanel extends JScrollPane {
 			}
 		});
 		
-		btnCreate.addActionListener(new ActionListener() {
-
+		JLabel lblStatus = new JLabel("Status:");
+		lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
+		lblStatus.setBounds(250, 130, 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, 130, 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());
 				
-				if(!edit){ 
-					if(!connection.getLink().getConnections().contains(connection))
-					connection.getLink().addConnection(connection);
-					for(int i = 0; i<ports.length; i++){
-						if(!ports[i].getOwner().getLinks().contains(connection.getLink()))
-							ports[i].getOwner().addLink(connection.getLink());
-						if(connection.getProtocol().getDevices().contains(ports[i])){
-							if(!connection.getParticipants().contains(ports[i]))
-								connection.addSmartDevice(ports[i]);
-							if(ports[i].getConnection()!=connection)
-								ports[i].setConnection(connection);
-							if(!ports[i].getOwner().getPorts().contains(ports[i]))
-								ports[i].getOwner().addPort(ports[i]);
-						} else {
-							if(connection.getParticipants().contains(ports[i]))
-								connection.removeSmartDevice(ports[i]);
-							if(ports[i].getConnection()!=null)
-								ports[i].setConnection(null);
-							if(ports[i].getOwner().getPorts().contains(ports[i]))
-								ports[i].getOwner().removePort(ports[i]);
-							
-						}
-					}
-					if(!controller.getLinks().contains(connection.getLink()))
-						controller.addLink(connection.getLink());
-					if(!controller.getConnections().contains(connection))
-						controller.addConnection(connection);
-				}
-				 
-				connection.setName(tfName.getText());
-				content.setVisible(false);
-				setVisible(false);
-				if (frame != null) {
-					frame.setVisible(false);
-					frame.dispose();
-				}
-				controller.notifyObservers();
 			}
 		});
 		
+				JButton btnCreate = new JButton("Verify and Create");
+				btnCreate.setBounds(125, 160, 206, 25);
+				content.add(btnCreate);
+				
+				btnCreate.addActionListener(new ActionListener() {
+
+					@Override
+					public void actionPerformed(ActionEvent e) {
+						
+						if(!edit){ 
+							if(!connection.getLink().getConnections().contains(connection))
+							connection.getLink().addConnection(connection);
+							for(int i = 0; i<ports.length; i++){
+								if(!ports[i].getOwner().getLinks().contains(connection.getLink()))
+									ports[i].getOwner().addLink(connection.getLink());
+								if(connection.getProtocol().getDevices().contains(ports[i])){
+									if(!connection.getParticipants().contains(ports[i]))
+										connection.addSmartDevice(ports[i]);
+									if(ports[i].getConnection()!=connection)
+										ports[i].setConnection(connection);
+									if(!ports[i].getOwner().getPorts().contains(ports[i]))
+										ports[i].getOwner().addPort(ports[i]);
+								} else {
+									if(connection.getParticipants().contains(ports[i]))
+										connection.removeSmartDevice(ports[i]);
+									if(ports[i].getConnection()!=null)
+										ports[i].setConnection(null);
+									if(ports[i].getOwner().getPorts().contains(ports[i]))
+										ports[i].getOwner().removePort(ports[i]);
+									
+								}
+							}
+							if(!controller.getLinks().contains(connection.getLink()))
+								controller.addLink(connection.getLink());
+							if(!controller.getConnections().contains(connection))
+								controller.addConnection(connection);
+						}
+						 
+						connection.setName(tfName.getText());
+						content.setVisible(false);
+						setVisible(false);
+						if (frame != null) {
+							frame.setVisible(false);
+							frame.dispose();
+						}
+						controller.notifyObservers();
+					}
+				});
+		
 		/**
 		 * Height of the current Box which is being created
 		 */
-		int currentHeight = 150;
+		int currentHeight = 190;
 
 		boxes = new JComboBox[ports.length];
 		for (int i = 0; i < ports.length; i++) {