|
@@ -7,11 +7,10 @@ import java.awt.event.ActionEvent;
|
|
|
import java.awt.event.ActionListener;
|
|
|
import java.io.File;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
|
|
|
import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.ImportController;
|
|
@@ -42,9 +41,9 @@ import javax.swing.event.DocumentListener;
|
|
|
@SuppressWarnings("serial")
|
|
|
public class ConnectionCreationPanel extends JScrollPane {
|
|
|
/**
|
|
|
- *
|
|
|
+ * Panel which contains the content of this creation panel
|
|
|
*/
|
|
|
- JPanel content;
|
|
|
+ private JPanel content;
|
|
|
|
|
|
/**
|
|
|
* Textfield which stores the Name of the connection
|
|
@@ -83,10 +82,22 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
private boolean edit;
|
|
|
|
|
|
/**
|
|
|
- * Last index which was selected in the linkType ComboBox, to allow restore
|
|
|
+ * Last index which was selected in the protocolType ComboBox, to allow restore
|
|
|
* the selected Index in case of invalid protocols
|
|
|
*/
|
|
|
- private int lastIndex = 0;
|
|
|
+ private int lastProtocolIndex = 0;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Last index which was selected in the selectedLink ComboBox, to allow restore
|
|
|
+ * the selected Index in case of invalid links
|
|
|
+ */
|
|
|
+ private int lastLinkIndex = -1;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Last index which was selected in the selectedConnection ComboBox, to allow restore
|
|
|
+ * the selected Index in case of invalid Connection
|
|
|
+ */
|
|
|
+ private int lastConnectionIndex = -1;
|
|
|
|
|
|
/**
|
|
|
* Mutex for the protocol change, so the different Devices don't change
|
|
@@ -145,7 +156,16 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
private void initializePanel() {
|
|
|
- disconnectedIndex = connection.getProtocol().getNumberOfRoles();
|
|
|
+ System.out.println("Connection null?: " + (connection==null));
|
|
|
+ if(connection!=null){
|
|
|
+ System.out.println("Connection name: "+connection.getName());
|
|
|
+ System.out.println("Connection protocol null: "+(connection.getProtocol()==null));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(connection != null && connection.getProtocol()!=null)
|
|
|
+ disconnectedIndex = connection.getProtocol().getNumberOfRoles();
|
|
|
+ else
|
|
|
+ disconnectedIndex = 0;
|
|
|
|
|
|
availableProtocols = controller.getControllerImport().getProtocols();
|
|
|
availableConnection = controller.getControllerImport().getConnections();
|
|
@@ -187,16 +207,46 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
JComboBox<String> cmbSelectedLink = new JComboBox<String>();
|
|
|
cmbSelectedLink.setBounds(155, 40, 125, 20);
|
|
|
content.add(cmbSelectedLink);
|
|
|
+ int linkIndex = 0;
|
|
|
+ for(Link l:controller.getLinks()){
|
|
|
+ cmbSelectedLink.addItem(l.getName());
|
|
|
+ if(l.equals(connection.getLink())){
|
|
|
+ lastLinkIndex = linkIndex;
|
|
|
+ }
|
|
|
+ linkIndex++;
|
|
|
+ }
|
|
|
+ cmbSelectedLink.setSelectedIndex(lastLinkIndex);
|
|
|
+ cmbSelectedLink.addActionListener(a -> {
|
|
|
+ int cmbSelectedLinkIndex = cmbSelectedLink.getSelectedIndex();
|
|
|
+ if(cmbSelectedLinkIndex == -1) return;
|
|
|
+ Link newLink = null;
|
|
|
+ Iterator<Link> linkIterator = controller.getLinks().iterator();
|
|
|
+ for(int i = 0; i<cmbSelectedLinkIndex && linkIterator.hasNext(); i++){
|
|
|
+ linkIterator.next();
|
|
|
+ }
|
|
|
+ if(linkIterator.hasNext())
|
|
|
+ newLink = linkIterator.next();
|
|
|
+ if(!controller.changeLinkOfConnection(connection, newLink)){
|
|
|
+ //if LinkChange failed: Restore
|
|
|
+ cmbSelectedLink.setSelectedIndex(lastLinkIndex);
|
|
|
+ }else{
|
|
|
+ lastLinkIndex = cmbSelectedLinkIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
|
|
|
JButton btnCreateLink = new JButton("Create Link");
|
|
|
btnCreateLink.setBounds(290, 40, 155, 20);
|
|
|
content.add(btnCreateLink);
|
|
|
+ btnCreateLink.addActionListener(a->{
|
|
|
+ new LinkCreationDialog(connection.getParticipants().stream().map(lElem->lElem.getOwner()).collect(Collectors.toList()), controller, content);
|
|
|
+ //TODO: Update ComboBox
|
|
|
+ });
|
|
|
|
|
|
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);
|
|
@@ -213,6 +263,23 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
//Select the right protocol and save last index
|
|
|
cmbConnection.setSelectedIndex(i);
|
|
|
}
|
|
|
+ cmbConnection.addActionListener(a->{
|
|
|
+ cmbConnection.getSelectedIndex();
|
|
|
+ int selectedIndex = cmbConnection.getSelectedIndex();
|
|
|
+ if(lastConnectionIndex!=selectedIndex && selectedIndex!=-1){
|
|
|
+ Connection newConnection = controller.changeConnectionType(connection, availableConnection.get(selectedIndex));
|
|
|
+ if(newConnection==null){
|
|
|
+ System.out.println("Warning invalidConnection changed");
|
|
|
+ cmbConnection.setSelectedIndex(lastConnectionIndex);
|
|
|
+ }else{
|
|
|
+ connection = newConnection;
|
|
|
+ lastConnectionIndex = selectedIndex;
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ cmbConnection.setSelectedIndex(lastConnectionIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
|
|
|
JButton btnImportConnection = new JButton("Import Connection");
|
|
|
btnImportConnection.setBounds(290, 70, 155, 20);
|
|
@@ -240,7 +307,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
.equals(availableProtocols.get(i))){
|
|
|
//Select the right protocol and save last index
|
|
|
cmbProtocolType.setSelectedIndex(i);
|
|
|
- lastIndex = i;
|
|
|
+ lastProtocolIndex = i;
|
|
|
}
|
|
|
|
|
|
// Add Functionality for changing protocol
|
|
@@ -266,7 +333,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
.println("WARNING: Protocol could not be initialized");
|
|
|
}
|
|
|
if (newProtocol == null) {
|
|
|
- cmbProtocolType.setSelectedIndex(lastIndex);
|
|
|
+ cmbProtocolType.setSelectedIndex(lastProtocolIndex);
|
|
|
System.out
|
|
|
.println("WARNING: Invalid Protocol Selected - restore last index");
|
|
|
} else if(connection.setProtocol(newProtocol)){
|
|
@@ -282,8 +349,8 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
disconnectedIndex = newProtocol.getNumberOfRoles();
|
|
|
//Update Protocol
|
|
|
|
|
|
- // Update lastIndex to new Index
|
|
|
- lastIndex = cmbProtocolType.getSelectedIndex();
|
|
|
+ // Update lastProtocolIndex to new Index
|
|
|
+ lastProtocolIndex = cmbProtocolType.getSelectedIndex();
|
|
|
//Update Boxes
|
|
|
for (int i = 0; i < boxes.length; i++) {
|
|
|
/**
|
|
@@ -313,7 +380,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- cmbProtocolType.setSelectedIndex(lastIndex);
|
|
|
+ cmbProtocolType.setSelectedIndex(lastProtocolIndex);
|
|
|
}
|
|
|
//Set Mutex back to false - allow changes
|
|
|
protocolChange = false;
|
|
@@ -602,8 +669,11 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
devicesOfLink.add(d);
|
|
|
ports.add(p);
|
|
|
}
|
|
|
- panel = new ConnectionCreationPanel(ports, testNet, new Controller(
|
|
|
- new Model()));
|
|
|
+ Model m = new Model();
|
|
|
+ m.addConnectionNetwork(testNet);
|
|
|
+ m.addConnectionNetwork(new SimpleLink("Wifi"));
|
|
|
+ m.addConnectionNetwork (new SimpleLink("Ethernet"));
|
|
|
+ panel = new ConnectionCreationPanel(ports, testNet, new Controller(m));
|
|
|
panel.setFrame(frame);
|
|
|
frame.setContentPane(panel);
|
|
|
frame.pack();
|