EditEdgesPopUp.java 4.6 KB

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