|
@@ -59,7 +59,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
private Connection connection;
|
|
|
|
|
|
/**
|
|
|
- * Index of the disconnected option in the boxes Array
|
|
|
+ * Index of the disconnected option in the cmbPortRoles Array
|
|
|
*/
|
|
|
private int disconnectedIndex;
|
|
|
|
|
@@ -86,12 +86,12 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
/**
|
|
|
* ComboBoxes to allow role changes of the different Ports
|
|
|
*/
|
|
|
- private JComboBox<String>[] boxes;
|
|
|
+ private JComboBox<String>[] cmbPortRoles;
|
|
|
|
|
|
/**
|
|
|
* ComboBox for the status
|
|
|
*/
|
|
|
- JComboBox<String> cmbStatus;
|
|
|
+ private JComboBox<String> cmbStatus;
|
|
|
|
|
|
/**
|
|
|
* True if an existing connection is being edited, or false if a new one is
|
|
@@ -120,7 +120,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
/**
|
|
|
* Mutex for the protocol change, so the different Devices don't change
|
|
|
* their role, while their comboBoxes are updated. True if the protocol
|
|
|
- * change is in progress and the boxes should not update their role.
|
|
|
+ * change is in progress and the cmbPortRoles should not update their role.
|
|
|
*/
|
|
|
private boolean protocolChange = false;
|
|
|
/**
|
|
@@ -167,8 +167,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
initializePanel();
|
|
|
}
|
|
|
|
|
|
- public ConnectionCreationPanel(Collection<Port> ports, Link l,
|
|
|
- Controller controller) {
|
|
|
+ public ConnectionCreationPanel(Collection<Port> ports, Link l, Controller controller) {
|
|
|
this.controller = controller;
|
|
|
this.network = controller.getNetworkController();
|
|
|
connection = new ConnectionPerformance(l, new MQTT_protocol());
|
|
@@ -228,50 +227,45 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
content.add(lblSelectedLink);
|
|
|
|
|
|
// Check if model contains link
|
|
|
- if (connection.getLink() != null
|
|
|
- && !network.getLinks().contains(connection.getLink()))
|
|
|
+ if (connection.getLink() != null && !network.getLinks().contains(connection.getLink()))
|
|
|
network.addLink(connection.getLink());
|
|
|
|
|
|
cmbSelectedLink = new JComboBox<String>();
|
|
|
cmbSelectedLink.setBounds(155, 40, 125, 20);
|
|
|
content.add(cmbSelectedLink);
|
|
|
|
|
|
- cmbSelectedLink
|
|
|
- .addActionListener(a -> {
|
|
|
- if (mutex) {
|
|
|
- return;
|
|
|
- }
|
|
|
- int cmbSelectedLinkIndex = cmbSelectedLink
|
|
|
- .getSelectedIndex();
|
|
|
- if (cmbSelectedLinkIndex == -1)
|
|
|
- return;
|
|
|
- Link newLink = null;
|
|
|
- Iterator<Link> linkIterator = network.getLinks()
|
|
|
- .iterator();
|
|
|
- for (int i = 0; i < cmbSelectedLinkIndex
|
|
|
- && linkIterator.hasNext(); i++) {
|
|
|
- linkIterator.next();
|
|
|
- }
|
|
|
- if (linkIterator.hasNext())
|
|
|
- newLink = linkIterator.next();
|
|
|
- if (!network.changeLinkOfConnection(connection, newLink)) {
|
|
|
- // if LinkChange failed: Restore
|
|
|
- cmbSelectedLink.setSelectedIndex(lastLinkIndex);
|
|
|
- } else {
|
|
|
- lastLinkIndex = cmbSelectedLinkIndex;
|
|
|
- }
|
|
|
+ cmbSelectedLink.addActionListener(a -> {
|
|
|
+ if (mutex) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int cmbSelectedLinkIndex = cmbSelectedLink.getSelectedIndex();
|
|
|
+ if (cmbSelectedLinkIndex == -1)
|
|
|
+ return;
|
|
|
+ Link newLink = null;
|
|
|
+ Iterator<Link> linkIterator = network.getLinks().iterator();
|
|
|
+ for (int i = 0; i < cmbSelectedLinkIndex && linkIterator.hasNext(); i++) {
|
|
|
+ linkIterator.next();
|
|
|
+ }
|
|
|
+ if (linkIterator.hasNext())
|
|
|
+ newLink = linkIterator.next();
|
|
|
+ if (!network.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);
|
|
|
+ new LinkCreationDialog(
|
|
|
+ connection.getParticipants().stream().map(lElem -> lElem.getOwner()).collect(Collectors.toList()),
|
|
|
+ controller, content);
|
|
|
// TODO: Refresh
|
|
|
- });
|
|
|
+ });
|
|
|
|
|
|
JLabel lblConnectionType = new JLabel("Connection type:");
|
|
|
lblConnectionType.setHorizontalAlignment(SwingConstants.RIGHT);
|
|
@@ -289,8 +283,8 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
cmbConnection.getSelectedIndex();
|
|
|
int selectedIndex = cmbConnection.getSelectedIndex();
|
|
|
if (lastConnectionIndex != selectedIndex && selectedIndex != -1) {
|
|
|
- Connection newConnection = network.changeConnectionType(
|
|
|
- connection, availableConnection.get(selectedIndex));
|
|
|
+ Connection newConnection = network.changeConnectionType(connection,
|
|
|
+ availableConnection.get(selectedIndex));
|
|
|
if (newConnection == null) {
|
|
|
System.out.println("Warning invalidConnection changed");
|
|
|
cmbConnection.setSelectedIndex(lastConnectionIndex);
|
|
@@ -308,8 +302,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
btnImportConnection.setBounds(290, 70, 155, 20);
|
|
|
content.add(btnImportConnection);
|
|
|
btnImportConnection.addActionListener(a -> {
|
|
|
- ImportPopUp<Connection> popUp = new ImportPopUp<Connection>(this,
|
|
|
- Connection.class);
|
|
|
+ ImportPopUp<Connection> popUp = new ImportPopUp<Connection>(this, Connection.class);
|
|
|
try {
|
|
|
Class<? extends Connection> imported = popUp.showPopUp();
|
|
|
if (imported == null)
|
|
@@ -317,12 +310,10 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
if (controller.getImportController().addConnection(imported)) {
|
|
|
refreshGUI();
|
|
|
} else {
|
|
|
- JOptionPane.showMessageDialog(frame,
|
|
|
- "Import failed: Invalid Connection");
|
|
|
+ JOptionPane.showMessageDialog(frame, "Import failed: Invalid Connection");
|
|
|
}
|
|
|
} catch (Exception e1) {
|
|
|
- JOptionPane.showMessageDialog(frame,
|
|
|
- "Import failed: " + e1.getMessage());
|
|
|
+ JOptionPane.showMessageDialog(frame, "Import failed: " + e1.getMessage());
|
|
|
}
|
|
|
});
|
|
|
|
|
@@ -339,9 +330,9 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
cmbProtocolType.addActionListener(new ActionListener() {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
- if(mutex)
|
|
|
+ if (mutex)
|
|
|
return;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Old Protocol
|
|
|
*/
|
|
@@ -355,16 +346,14 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
Protocol newProtocol = null;
|
|
|
try {
|
|
|
// Create new Instance of the protocol
|
|
|
- newProtocol = controller.getImportController().getProtocols().get(
|
|
|
- cmbProtocolType.getSelectedIndex()).newInstance();
|
|
|
+ newProtocol = controller.getImportController().getProtocols()
|
|
|
+ .get(cmbProtocolType.getSelectedIndex()).newInstance();
|
|
|
} catch (InstantiationException | IllegalAccessException e1) {
|
|
|
- System.out
|
|
|
- .println("WARNING: Protocol could not be initialized");
|
|
|
+ System.out.println("WARNING: Protocol could not be initialized");
|
|
|
}
|
|
|
if (newProtocol == null) {
|
|
|
cmbProtocolType.setSelectedIndex(lastProtocolIndex);
|
|
|
- System.out
|
|
|
- .println("WARNING: Invalid Protocol Selected - restore last index");
|
|
|
+ System.out.println("WARNING: Invalid Protocol Selected - restore last index");
|
|
|
} else if (connection.setProtocol(newProtocol)) {
|
|
|
/**
|
|
|
* New Roles as Strings
|
|
@@ -381,33 +370,29 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
// Update lastProtocolIndex to new Index
|
|
|
lastProtocolIndex = cmbProtocolType.getSelectedIndex();
|
|
|
// Update Boxes
|
|
|
- for (int i = 0; i < boxes.length; i++) {
|
|
|
+ for (int i = 0; i < cmbPortRoles.length; i++) {
|
|
|
/**
|
|
|
* The previous selected Index
|
|
|
*/
|
|
|
- int oldSelected = boxes[i].getSelectedIndex();
|
|
|
+ int oldSelected = cmbPortRoles[i].getSelectedIndex();
|
|
|
// Update Labels of the current Box
|
|
|
- boxes[i].removeAllItems();
|
|
|
+ cmbPortRoles[i].removeAllItems();
|
|
|
for (int j = 0; j < newRoles.length; j++)
|
|
|
- boxes[i].addItem(newRoles[j]);
|
|
|
- boxes[i].addItem("Disconnected");
|
|
|
+ cmbPortRoles[i].addItem(newRoles[j]);
|
|
|
+ cmbPortRoles[i].addItem("Disconnected");
|
|
|
// Try to update the Roles
|
|
|
- if (oldSelected < 0 || oldSelected >= disconnectedIndex
|
|
|
- || oldSelected == oldDisconnected) {
|
|
|
+ if (oldSelected < 0 || oldSelected >= disconnectedIndex || oldSelected == oldDisconnected) {
|
|
|
// Invalid Index -> Disconnected
|
|
|
- network.removeDeviceFromConnection(ports[i],
|
|
|
- connection);
|
|
|
- boxes[i].setSelectedIndex(disconnectedIndex);
|
|
|
+ network.removeDeviceFromConnection(ports[i], connection);
|
|
|
+ cmbPortRoles[i].setSelectedIndex(disconnectedIndex);
|
|
|
} else {
|
|
|
- if (network.addDeviceToConnection(ports[i],
|
|
|
- connection, oldSelected)) {
|
|
|
+ if (network.addDeviceToConnection(ports[i], connection, oldSelected)) {
|
|
|
// Set to Box to display role
|
|
|
- boxes[i].setSelectedIndex(oldSelected);
|
|
|
+ cmbPortRoles[i].setSelectedIndex(oldSelected);
|
|
|
} else {
|
|
|
// Could not be added -> Disconnected
|
|
|
- network.removeDeviceFromConnection(ports[i],
|
|
|
- connection);
|
|
|
- boxes[i].setSelectedIndex(disconnectedIndex);
|
|
|
+ network.removeDeviceFromConnection(ports[i], connection);
|
|
|
+ cmbPortRoles[i].setSelectedIndex(disconnectedIndex);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -423,19 +408,17 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
btnImportProtocol.setBounds(290, 100, 155, 20);
|
|
|
content.add(btnImportProtocol);
|
|
|
btnImportProtocol.addActionListener(a -> {
|
|
|
- ImportPopUp<Protocol> popUp = new ImportPopUp<Protocol>(this,
|
|
|
- Protocol.class);
|
|
|
+ ImportPopUp<Protocol> popUp = new ImportPopUp<Protocol>(this, Protocol.class);
|
|
|
|
|
|
- Class<? extends Protocol> imported = null;
|
|
|
- try {
|
|
|
- imported = popUp.showPopUp();
|
|
|
- } catch (Exception e1) {
|
|
|
- JOptionPane.showMessageDialog(frame,
|
|
|
- "Import failed: " + e1.getMessage());
|
|
|
- return;
|
|
|
- }
|
|
|
- if (imported == null) {
|
|
|
- // Import cancelled
|
|
|
+ Class<? extends Protocol> imported = null;
|
|
|
+ try {
|
|
|
+ imported = popUp.showPopUp();
|
|
|
+ } catch (Exception e1) {
|
|
|
+ JOptionPane.showMessageDialog(frame, "Import failed: " + e1.getMessage());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (imported == null) {
|
|
|
+ // Import cancelled
|
|
|
return;
|
|
|
}
|
|
|
// Add Protocol
|
|
@@ -443,8 +426,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
// Update GUI
|
|
|
refreshGUI();
|
|
|
} else {
|
|
|
- JOptionPane.showMessageDialog(frame,
|
|
|
- "Import failed: Invalid Protocol");
|
|
|
+ JOptionPane.showMessageDialog(frame, "Import failed: Invalid Protocol");
|
|
|
return;
|
|
|
}
|
|
|
});
|
|
@@ -452,47 +434,44 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
JLabel lblPacketlossrate = new JLabel("PacketLossRate:");
|
|
|
lblPacketlossrate.setHorizontalAlignment(SwingConstants.RIGHT);
|
|
|
lblPacketlossrate.setBounds(10, 130, 140, 20);
|
|
|
- lblPacketlossrate
|
|
|
- .setToolTipText("Probability of Packet loss, should be a Double in [0.0,1.0]");
|
|
|
+ lblPacketlossrate.setToolTipText("Probability of Packet loss, should be a Double in [0.0,1.0]");
|
|
|
content.add(lblPacketlossrate);
|
|
|
|
|
|
tfPacketLossRate = new JTextField();
|
|
|
tfPacketLossRate.setBounds(155, 130, 90, 20);
|
|
|
content.add(tfPacketLossRate);
|
|
|
tfPacketLossRate.setColumns(10);
|
|
|
- tfPacketLossRate.getDocument().addDocumentListener(
|
|
|
- new DocumentListener() {
|
|
|
+ tfPacketLossRate.getDocument().addDocumentListener(new DocumentListener() {
|
|
|
|
|
|
- @Override
|
|
|
- public void removeUpdate(DocumentEvent e) {
|
|
|
- updateConnectionAndField();
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void removeUpdate(DocumentEvent e) {
|
|
|
+ updateConnectionAndField();
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void insertUpdate(DocumentEvent e) {
|
|
|
- updateConnectionAndField();
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void insertUpdate(DocumentEvent e) {
|
|
|
+ updateConnectionAndField();
|
|
|
+ }
|
|
|
|
|
|
- @Override
|
|
|
- public void changedUpdate(DocumentEvent e) {
|
|
|
- updateConnectionAndField();
|
|
|
- }
|
|
|
+ @Override
|
|
|
+ public void changedUpdate(DocumentEvent e) {
|
|
|
+ updateConnectionAndField();
|
|
|
+ }
|
|
|
|
|
|
- private void updateConnectionAndField() {
|
|
|
- try {
|
|
|
- double newRate = Double
|
|
|
- .parseDouble(tfPacketLossRate.getText());
|
|
|
- if (newRate >= 0.0 && newRate <= 1.0) {
|
|
|
- connection.setPacketLossProbability(newRate);
|
|
|
- tfPacketLossRate.setBackground(Color.WHITE);
|
|
|
- } else {
|
|
|
- tfPacketLossRate.setBackground(Color.RED);
|
|
|
- }
|
|
|
- } catch (Exception exception) {
|
|
|
- tfPacketLossRate.setBackground(Color.RED);
|
|
|
- }
|
|
|
+ private void updateConnectionAndField() {
|
|
|
+ try {
|
|
|
+ double newRate = Double.parseDouble(tfPacketLossRate.getText());
|
|
|
+ if (newRate >= 0.0 && newRate <= 1.0) {
|
|
|
+ connection.setPacketLossProbability(newRate);
|
|
|
+ tfPacketLossRate.setBackground(Color.WHITE);
|
|
|
+ } else {
|
|
|
+ tfPacketLossRate.setBackground(Color.RED);
|
|
|
}
|
|
|
- });
|
|
|
+ } catch (Exception exception) {
|
|
|
+ tfPacketLossRate.setBackground(Color.RED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
JLabel lblStatus = new JLabel("Status:");
|
|
|
lblStatus.setHorizontalAlignment(SwingConstants.RIGHT);
|
|
@@ -511,8 +490,7 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
if (mutex) {
|
|
|
cmbStatus.setSelectedIndex(connection.getStatus());
|
|
|
}
|
|
|
- if (cmbStatus.getSelectedIndex() < 0
|
|
|
- || cmbStatus.getSelectedIndex() > 4)
|
|
|
+ if (cmbStatus.getSelectedIndex() < 0 || cmbStatus.getSelectedIndex() > 4)
|
|
|
cmbStatus.setSelectedIndex(connection.getStatus());
|
|
|
else
|
|
|
connection.setStatus((byte) cmbStatus.getSelectedIndex());
|
|
@@ -527,43 +505,86 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
btnCreate.addActionListener(new ActionListener() {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
+ /**
|
|
|
+ * Validate Packet loss rate
|
|
|
+ */
|
|
|
+ try{
|
|
|
+ double d = Double.parseDouble(tfPacketLossRate.getText());
|
|
|
+ if(d < 0.0 && 1.0 < d)
|
|
|
+ throw new Exception("Packet loss rate out of bounds");
|
|
|
+ connection.setPacketLossProbability(d);
|
|
|
+ }catch(Exception e1){
|
|
|
+ JOptionPane.showMessageDialog(frame, "Invalid PacketsLossProbability, should be double between 0.0 and 1.0", "Invalid Packetloss Rate",
|
|
|
+ JOptionPane.WARNING_MESSAGE);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * Validate Status
|
|
|
+ */
|
|
|
+ if(cmbStatus.getSelectedIndex()<0){
|
|
|
+ JOptionPane.showMessageDialog(frame, "Status not set", "Status invalid",
|
|
|
+ JOptionPane.WARNING_MESSAGE);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ connection.setStatus((byte)cmbStatus.getSelectedIndex());
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Link should be set and contain connection
|
|
|
+ */
|
|
|
+ if (connection.getLink() == null) {
|
|
|
+ JOptionPane.showMessageDialog(frame, "Link should not be null.", "Invalid Link Selected",
|
|
|
+ JOptionPane.WARNING_MESSAGE);
|
|
|
+ return;
|
|
|
+ } else if (!connection.getLink().getConnections().contains(connection)){
|
|
|
+ network.addConnectionToLink(connection, connection.getLink());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(tfName.getText()!=null)
|
|
|
+ connection.setName(tfName.getText());
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Check each Port/Device
|
|
|
+ */
|
|
|
+ for (int i = 0; i < ports.length; i++) {
|
|
|
+ //Add Device to Link
|
|
|
+ if (connection.getProtocol().getDevices().contains(ports[i])) {
|
|
|
+ /**
|
|
|
+ * If Link part of protocol & connection
|
|
|
+ */
|
|
|
+ if (!ports[i].getOwner().getLinks().contains(connection.getLink()))
|
|
|
+ network.addLinkToDevice(connection.getLink(), ports[i].getOwner());
|
|
|
+ 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 {
|
|
|
+ /**
|
|
|
+ * Else remove from COnnection
|
|
|
+ */
|
|
|
+ network.removeDeviceFromConnection(ports[i], connection);
|
|
|
+ if (ports[i].getOwner().getPorts().contains(ports[i]))
|
|
|
+ ports[i].getOwner().removePort(ports[i]);
|
|
|
|
|
|
- if (!edit) {
|
|
|
- if (connection.getLink()!=null && !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 (!network.getLinks().contains(connection.getLink()))
|
|
|
- network.addLink(connection.getLink());
|
|
|
- if (!network.getConnections().contains(connection))
|
|
|
- network.addConnection(connection);
|
|
|
}
|
|
|
-
|
|
|
- connection.setName(tfName.getText());
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Connection should be in the model
|
|
|
+ */
|
|
|
+ if(!network.getConnections().contains(connection))
|
|
|
+ network.addConnection(connection);
|
|
|
+ /**
|
|
|
+ * Link should be in the model
|
|
|
+ */
|
|
|
+ if(!network.getLinks().contains(connection.getLink())){
|
|
|
+ network.addLink(connection.getLink());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Close Window
|
|
|
+ */
|
|
|
content.setVisible(false);
|
|
|
setVisible(false);
|
|
|
if (frame != null) {
|
|
@@ -579,32 +600,30 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
*/
|
|
|
int currentHeight = 190;
|
|
|
|
|
|
- boxes = new JComboBox[ports.length];
|
|
|
+ cmbPortRoles = new JComboBox[ports.length];
|
|
|
for (int i = 0; i < ports.length; i++) {
|
|
|
// Effectively final variables for the action listener
|
|
|
int pos = i;
|
|
|
Port currentPort = ports[i];
|
|
|
SmartDevice currentDevice = currentPort.getOwner();
|
|
|
|
|
|
- JLabel lblDevice = new JLabel(currentDevice.getName() + ":"
|
|
|
- + currentPort.getPortNumber() + ":");
|
|
|
+ JLabel lblDevice = new JLabel(currentDevice.getName() + ":" + currentPort.getPortNumber() + ":");
|
|
|
lblDevice.setHorizontalAlignment(SwingConstants.RIGHT);
|
|
|
lblDevice.setBounds(10, currentHeight, 230, 18);
|
|
|
content.add(lblDevice);
|
|
|
|
|
|
- boxes[pos] = new JComboBox<String>();
|
|
|
+ cmbPortRoles[pos] = new JComboBox<String>();
|
|
|
for (String role : connection.getProtocol().getRoles())
|
|
|
- boxes[pos].addItem(role);
|
|
|
- boxes[pos].addItem("Disconnected");
|
|
|
- boxes[pos].setBounds(250, currentHeight, 240, 18);
|
|
|
- content.add(boxes[pos]);
|
|
|
+ cmbPortRoles[pos].addItem(role);
|
|
|
+ cmbPortRoles[pos].addItem("Disconnected");
|
|
|
+ cmbPortRoles[pos].setBounds(250, currentHeight, 240, 18);
|
|
|
+ content.add(cmbPortRoles[pos]);
|
|
|
if (edit) {
|
|
|
- int roleOfDevice = connection.getProtocol().getRoleOfDevice(
|
|
|
- currentPort);
|
|
|
+ int roleOfDevice = connection.getProtocol().getRoleOfDevice(currentPort);
|
|
|
if (roleOfDevice != -1)
|
|
|
- boxes[pos].setSelectedIndex(roleOfDevice);
|
|
|
+ cmbPortRoles[pos].setSelectedIndex(roleOfDevice);
|
|
|
else
|
|
|
- boxes[pos].setSelectedIndex(disconnectedIndex);
|
|
|
+ cmbPortRoles[pos].setSelectedIndex(disconnectedIndex);
|
|
|
} else {
|
|
|
/**
|
|
|
* Number of tries to add the Port
|
|
@@ -612,28 +631,24 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
int n = 5;
|
|
|
for (int j = 0; j <= n; j++) {
|
|
|
if (j == 0) {// Try to add Router first
|
|
|
- if (network.addDeviceToConnection(currentPort,
|
|
|
- connection, 0)) {
|
|
|
- boxes[pos].setSelectedIndex(0);
|
|
|
+ if (network.addDeviceToConnection(currentPort, connection, 0)) {
|
|
|
+ cmbPortRoles[pos].setSelectedIndex(0);
|
|
|
break;
|
|
|
}
|
|
|
} else if (j == n) {// If it could not be added
|
|
|
- boxes[pos].setSelectedIndex(disconnectedIndex);
|
|
|
- network.removeDeviceFromConnection(currentPort,
|
|
|
- connection);
|
|
|
+ cmbPortRoles[pos].setSelectedIndex(disconnectedIndex);
|
|
|
+ network.removeDeviceFromConnection(currentPort, connection);
|
|
|
break;
|
|
|
} else {
|
|
|
- int randomRole = ThreadLocalRandom.current().nextInt(0,
|
|
|
- disconnectedIndex);
|
|
|
- if (network.addDeviceToConnection(currentPort,
|
|
|
- connection, randomRole)) {
|
|
|
- boxes[pos].setSelectedIndex(randomRole);
|
|
|
+ int randomRole = ThreadLocalRandom.current().nextInt(0, disconnectedIndex);
|
|
|
+ if (network.addDeviceToConnection(currentPort, connection, randomRole)) {
|
|
|
+ cmbPortRoles[pos].setSelectedIndex(randomRole);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- boxes[pos].addActionListener(new ActionListener() {
|
|
|
+ cmbPortRoles[pos].addActionListener(new ActionListener() {
|
|
|
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
@@ -644,19 +659,17 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
* Selected Index of the ComboBox, which represents the new
|
|
|
* role (or disconnected)
|
|
|
*/
|
|
|
- int selected = boxes[pos].getSelectedIndex();
|
|
|
+ int selected = cmbPortRoles[pos].getSelectedIndex();
|
|
|
/**
|
|
|
* True if role was successfully changed
|
|
|
*/
|
|
|
- boolean successfullChange = network.changeRoleOfDevice(
|
|
|
- connection.getProtocol(), connection, ports[pos],
|
|
|
- selected);
|
|
|
+ boolean successfullChange = network.changeRoleOfDevice(connection.getProtocol(), connection,
|
|
|
+ ports[pos], selected);
|
|
|
/**
|
|
|
* Set to Disconnected, if role could not be changed
|
|
|
*/
|
|
|
if (!successfullChange) {
|
|
|
- boxes[pos].setSelectedIndex(connection.getProtocol()
|
|
|
- .getNumberOfRoles());
|
|
|
+ cmbPortRoles[pos].setSelectedIndex(connection.getProtocol().getNumberOfRoles());
|
|
|
}
|
|
|
}
|
|
|
});
|
|
@@ -720,17 +733,15 @@ public class ConnectionCreationPanel extends JScrollPane {
|
|
|
cmbProtocolType.removeAllItems();
|
|
|
for (int i = 0; i < availableProtocols.size(); i++)
|
|
|
try {
|
|
|
- cmbProtocolType.addItem(availableProtocols.get(i).newInstance()
|
|
|
- .getName());
|
|
|
+ cmbProtocolType.addItem(availableProtocols.get(i).newInstance().getName());
|
|
|
} catch (InstantiationException | IllegalAccessException e1) {
|
|
|
System.out.println("Protocol " + i + " is invalid");
|
|
|
cmbProtocolType.addItem("unknown");
|
|
|
}
|
|
|
// Set Index to selected Protocol
|
|
|
lastProtocolIndex = -1;
|
|
|
- for (int i = 0; i < availableProtocols.size(); i++){
|
|
|
- if (connection.getProtocol().getClass()
|
|
|
- .equals(availableProtocols.get(i))) {
|
|
|
+ for (int i = 0; i < availableProtocols.size(); i++) {
|
|
|
+ if (connection.getProtocol().getClass().equals(availableProtocols.get(i))) {
|
|
|
// Select the right protocol and save last index
|
|
|
lastProtocolIndex = i;
|
|
|
}
|