EditEdgesPopUp.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package holeg.ui.view.dialog;
  2. import holeg.model.Edge;
  3. import holeg.preferences.ImagePreference;
  4. import holeg.ui.controller.Control;
  5. import holeg.ui.model.GuiSettings;
  6. import holeg.ui.view.image.Import;
  7. import java.awt.BorderLayout;
  8. import java.awt.Font;
  9. import javax.swing.ButtonGroup;
  10. import javax.swing.JButton;
  11. import javax.swing.JDialog;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JOptionPane;
  15. import javax.swing.JPanel;
  16. import javax.swing.JRadioButton;
  17. import javax.swing.JTextField;
  18. import javax.swing.border.EmptyBorder;
  19. /**
  20. * Popup for Editing Edges.
  21. *
  22. * @author Gruppe14
  23. */
  24. public class EditEdgesPopUp extends JDialog {
  25. private final JTextField capacityField;
  26. private final JRadioButton existingEdgesRadioButton = new JRadioButton(
  27. "Change for all existing Edges only");
  28. private final JRadioButton newEdgesRadioButton = new JRadioButton(
  29. "Change for new created Edges only");
  30. private final JRadioButton existingAndNewEdgesRadioButton = new JRadioButton(
  31. "Change for all existing and new created Edges");
  32. private final Control control;
  33. private float capacity;
  34. /**
  35. * Constructor.
  36. */
  37. public EditEdgesPopUp(JFrame parentFrame, Control control) {
  38. super((java.awt.Frame) null, true);
  39. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  40. this.setTitle("Edit Capacities of Edges");
  41. setBounds(100, 100, 400, 220);
  42. setLocationRelativeTo(parentFrame);
  43. getContentPane().setLayout(new BorderLayout());
  44. JPanel contentPanel = new JPanel();
  45. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  46. getContentPane().add(contentPanel, BorderLayout.CENTER);
  47. contentPanel.setLayout(null);
  48. this.setIconImage(Import.loadImage(ImagePreference.Logo, 30, 30));
  49. JLabel lblMaximumCapacity = new JLabel("Maximum Capacity:");
  50. lblMaximumCapacity.setFont(new Font("Tahoma", Font.PLAIN, 11));
  51. lblMaximumCapacity.setBounds(10, 11, 98, 14);
  52. contentPanel.add(lblMaximumCapacity);
  53. capacityField = new JTextField();
  54. capacityField.setBounds(107, 8, 120, 20);
  55. contentPanel.add(capacityField);
  56. capacityField.setColumns(10);
  57. existingEdgesRadioButton.setBounds(10, 39, 330, 23);
  58. contentPanel.add(existingEdgesRadioButton);
  59. newEdgesRadioButton.setBounds(10, 65, 330, 23);
  60. contentPanel.add(newEdgesRadioButton);
  61. existingAndNewEdgesRadioButton.setBounds(10, 95, 400, 23);
  62. contentPanel.add(existingAndNewEdgesRadioButton);
  63. JButton btnCancel = new JButton("Cancel");
  64. btnCancel.setActionCommand("Cancel");
  65. btnCancel.addActionListener(clicked -> dispose());
  66. btnCancel.setBounds(285, 147, 89, 23);
  67. contentPanel.add(btnCancel);
  68. JButton btnOk1 = new JButton("OK");
  69. btnOk1.addActionListener(clicked -> {
  70. try {
  71. capacity = Float.parseFloat(capacityField.getText());
  72. if (capacity < 0) {
  73. throw new NumberFormatException();
  74. }
  75. if (existingEdgesRadioButton.isSelected()) {
  76. changeForExisting(capacity);
  77. dispose();
  78. } else if (newEdgesRadioButton.isSelected()) {
  79. changeForNew(capacity);
  80. dispose();
  81. } else if (existingAndNewEdgesRadioButton.isSelected()) {
  82. changeForExAndNew(capacity);
  83. dispose();
  84. } else {
  85. JOptionPane.showMessageDialog(new JFrame(), "Please select one of the options");
  86. }
  87. } catch (NumberFormatException eex) {
  88. JOptionPane.showMessageDialog(new JFrame(),
  89. "Please enter a number greater or equal 0 in the Field for Maximum Capacity");
  90. }
  91. });
  92. btnOk1.setBounds(186, 147, 89, 23);
  93. contentPanel.add(btnOk1);
  94. this.setTitle("Edit Edge Capacities");
  95. ButtonGroup bG = new ButtonGroup();
  96. bG.add(existingAndNewEdgesRadioButton);
  97. bG.add(newEdgesRadioButton);
  98. bG.add(existingEdgesRadioButton);
  99. existingAndNewEdgesRadioButton.setSelected(true);
  100. this.control = control;
  101. this.setVisible(true);
  102. }
  103. /**
  104. * set edge capacity for new edges.
  105. *
  106. * @param cap the capacity
  107. */
  108. public void changeForNew(float cap) {
  109. GuiSettings.maxCapacityForNewCreatedEdges = cap;
  110. control.resetSimulation();
  111. }
  112. /**
  113. * Set Capacity for all existing Edges.
  114. *
  115. * @param cap the Capacity
  116. */
  117. public void changeForExisting(float cap) {
  118. /*
  119. * for(SubNet sn: controller.getSimManager().getSubNets()){ for(CpsEdge
  120. * edge: sn.getEdges()){ edge.setCapacity(cap); } } for(CpsEdge edge:
  121. * controller.getSimManager().getBrokenEdges()){ edge.setCapacity(cap);
  122. * }
  123. */
  124. for (Edge edge : control.getModel().getEdgesOnCanvas()) {
  125. edge.maxCapacity = cap;
  126. }
  127. control.resetSimulation();
  128. control.updateStateForCurrentIteration();
  129. }
  130. /**
  131. * Set the Capacity for all existing and new edges.
  132. *
  133. * @param cap the capacity
  134. */
  135. public void changeForExAndNew(float cap) {
  136. changeForNew(cap);
  137. changeForExisting(cap);
  138. }
  139. }