EditEdgesPopUp.java 5.0 KB

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