PortDistributionConfigurationPopUp.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import javax.swing.JFrame;
  3. import javax.swing.JPanel;
  4. import java.awt.BorderLayout;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.util.LinkedList;
  9. import java.util.Observable;
  10. import java.util.Observer;
  11. import javax.swing.JComboBox;
  12. import javax.swing.JLabel;
  13. import javax.swing.JButton;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JScrollPane;
  16. import javax.swing.JSplitPane;
  17. import javax.swing.border.EmptyBorder;
  18. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  19. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  20. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  21. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.ProbabilityDistributionHandler;
  22. public class PortDistributionConfigurationPopUp extends JFrame implements Observer{
  23. /**
  24. *
  25. */
  26. private static final long serialVersionUID = 1L;
  27. private Port port;
  28. private JSplitPane splitPane;
  29. private JScrollPane scrollPane;
  30. /**
  31. * ComboBox for link selection
  32. */
  33. private JComboBox<String> cmbDistribution;
  34. /**
  35. * Distributions that can be selected;
  36. */
  37. LinkedList<Class<? extends ProbabilityDistributionHandler>> availableDistributions;
  38. /**
  39. * Last index which was selected in the combo box
  40. */
  41. int lastIndex = -1;
  42. boolean mutex = false;
  43. private Controller controller;
  44. private PortDistributionConfigurationPopUp that = this;
  45. public PortDistributionConfigurationPopUp(Port port, Controller controller) {
  46. this.port = port;
  47. this.controller = controller;
  48. this.setSize(480, 360);
  49. splitPane = new JSplitPane();
  50. splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  51. splitPane.setBorder(new EmptyBorder(0, 0, 0, 0));
  52. getContentPane().add(splitPane, BorderLayout.CENTER);
  53. JPanel panel = new JPanel();
  54. splitPane.setLeftComponent(panel);
  55. panel.setLayout(null);
  56. JLabel lblDescription = new JLabel("Distribution Type:");
  57. lblDescription.setBounds(10, 10, 120, 20);
  58. panel.add(lblDescription);
  59. cmbDistribution = new JComboBox<String>();
  60. cmbDistribution.setBounds(120, 10, 190, 20);
  61. panel.add(cmbDistribution);
  62. JButton btnImport = new JButton("Import");
  63. btnImport.setBounds(320, 10, 100, 20);
  64. panel.add(btnImport);
  65. btnImport.addActionListener(a -> {
  66. ImportPopUp<ProbabilityDistributionHandler> popUp = new ImportPopUp<ProbabilityDistributionHandler>(this, ProbabilityDistributionHandler.class);
  67. try {
  68. Class<? extends ProbabilityDistributionHandler> imported = popUp.showPopUp();
  69. if (imported == null)
  70. return;
  71. if (controller.getImportController().addDistributionHandler(imported)) {
  72. update(null, null);
  73. } else {
  74. JOptionPane.showMessageDialog(that, "Import failed: Invalid Distribution Handler");
  75. }
  76. } catch (Exception e1) {
  77. JOptionPane.showMessageDialog(that, "Import failed: " + e1.getMessage());
  78. }
  79. });
  80. splitPane.setDividerLocation(40);
  81. scrollPane = new JScrollPane();
  82. splitPane.setRightComponent(scrollPane);
  83. update(null,null);
  84. this.addWindowListener(new WindowAdapter() {
  85. public void windowClosing(java.awt.event.WindowEvent e) {
  86. that.controller.removeObserver(that);
  87. }
  88. });
  89. cmbDistribution.addActionListener(new ActionListener() {
  90. @Override
  91. public void actionPerformed(ActionEvent e) {
  92. if (mutex)
  93. return;
  94. /**
  95. * New Distribution
  96. */
  97. ProbabilityDistributionHandler newDistribution = null;
  98. try {
  99. // Create new Instance of the protocol
  100. newDistribution = controller.getImportController().getDistributionHandlers()
  101. .get(cmbDistribution.getSelectedIndex()).newInstance();
  102. } catch (InstantiationException | IllegalAccessException e1) {
  103. System.out.println("WARNING: Distribution could not be initialized");
  104. }
  105. if (newDistribution == null) {
  106. cmbDistribution.setSelectedIndex(lastIndex);
  107. System.out.println("WARNING: Invalid Distribution Selected - restore last index");
  108. } else {
  109. /**
  110. * Add new Distribution
  111. */
  112. port.setTriggerHandler(newDistribution);
  113. /**
  114. * Update Index
  115. */
  116. lastIndex = cmbDistribution.getSelectedIndex();
  117. update(null, null);
  118. }
  119. }
  120. });
  121. this.controller.addObserver(this);
  122. }
  123. @Override
  124. public void update(Observable o, Object arg) {
  125. if(port == null)return;
  126. ProbabilityDistributionHandler handler = port.getTriggerHandler();
  127. if(handler!=null)
  128. scrollPane.setViewportView(handler.getConfigurationPanel());
  129. else{
  130. scrollPane.setViewportView(null);
  131. return;
  132. }
  133. mutex = true;
  134. /**
  135. * Update Distribution function
  136. */
  137. availableDistributions = controller.getImportController().getDistributionHandlers();
  138. cmbDistribution.removeAllItems();
  139. for (int i = 0; i < availableDistributions.size(); i++)
  140. try {
  141. cmbDistribution.addItem(availableDistributions.get(i).newInstance().getSimpleDescription());
  142. } catch (InstantiationException | IllegalAccessException e1) {
  143. System.out.println("Distribution " + i + " is invalid");
  144. cmbDistribution.addItem("unknown");
  145. }
  146. // Set Index to selected Protocol
  147. lastIndex = -1;
  148. for (int i = 0; i < availableDistributions.size(); i++) {
  149. if (handler.getClass().equals(availableDistributions.get(i))) {
  150. // Select the right protocol and save last index
  151. lastIndex = i;
  152. }
  153. }
  154. cmbDistribution.setSelectedIndex(lastIndex);
  155. mutex = false;
  156. }
  157. public static void main(String[] args) {
  158. PortDistributionConfigurationPopUp test = new PortDistributionConfigurationPopUp(null, new Controller(new Model()));
  159. test.setEnabled(true);
  160. test.setVisible(true);
  161. }
  162. }