ConnectionCreationDialog.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import java.awt.Component;
  3. import java.util.Collection;
  4. import java.util.LinkedList;
  5. import javax.swing.JDialog;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  10. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  11. /**
  12. * PopUp for the creation of Connections
  13. *
  14. *
  15. * @author Andreas T. Meyer-Berg
  16. */
  17. @SuppressWarnings("serial")
  18. public class ConnectionCreationDialog extends JDialog {
  19. private ConnectionCreationPanel content;
  20. private Component parent;
  21. /**
  22. * Create a Dialog for editing the given Connection
  23. * @param connection Connection to be edited
  24. * @param controller Controller for changing the connection
  25. * @param panel Panel the PopUp should be moved relative to
  26. */
  27. public ConnectionCreationDialog(Connection connection, Controller controller, Component panel) {
  28. super();
  29. content = new ConnectionCreationPanel(connection, controller, this);
  30. parent = panel;
  31. initialize();
  32. }
  33. /**
  34. * Create a Dialog for creating a Connection of the given devices
  35. * @param devices ports which should be connected
  36. * @param link Link which the connection should run on
  37. * @param controller Controller for changing the connection
  38. * @param panel Panel the PopUp should be moved relative to
  39. */
  40. public ConnectionCreationDialog(Collection<Port> devices, Link link, Controller controller, Component panel){
  41. super();
  42. content = new ConnectionCreationPanel(devices, link, controller, this);
  43. parent = panel;
  44. initialize();
  45. }
  46. /**
  47. * Initializes and shows a creation dialog for a connection of the given devices
  48. * @param devices Devices, which should be connected
  49. * @param controller Controller for changing the connection
  50. * @param panel Panel the PopUp should be moved relative to
  51. */
  52. public ConnectionCreationDialog(Collection<SmartDevice> devices, Controller controller, Component panel){
  53. super();
  54. LinkedList<Port> ports = new LinkedList<Port>();
  55. for(SmartDevice d: devices)
  56. ports.add(new Port(d, (short) 12));
  57. Link link = null;
  58. /**
  59. * Find common link -> else null -> create new link
  60. */
  61. for(Link l:controller.getNetworkController().getLinks()){
  62. if(l.getDevices().containsAll(devices)){
  63. link = l;
  64. break;
  65. }
  66. }
  67. content = new ConnectionCreationPanel(ports, link, controller, this);
  68. parent = panel;
  69. initialize();
  70. }
  71. /**
  72. * Initialize and show the popUp
  73. */
  74. private void initialize() {
  75. this.setModal(true);
  76. this.setContentPane(content);
  77. this.pack();
  78. setLocationRelativeTo(parent);
  79. this.setVisible(true);
  80. }
  81. }