EditEdgesPopUp.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Font;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import javax.swing.ButtonGroup;
  7. import javax.swing.JButton;
  8. import javax.swing.JDialog;
  9. import javax.swing.JFrame;
  10. import javax.swing.JLabel;
  11. import javax.swing.JOptionPane;
  12. import javax.swing.JPanel;
  13. import javax.swing.JRadioButton;
  14. import javax.swing.JTextField;
  15. import javax.swing.border.EmptyBorder;
  16. import classes.AbstractCpsObject;
  17. import classes.CpsEdge;
  18. import classes.CpsUpperNode;
  19. import ui.controller.Control;
  20. /**
  21. * Popup for Editing Edges.
  22. *
  23. * @author Gruppe14
  24. */
  25. public class EditEdgesPopUp extends JDialog {
  26. private static final long serialVersionUID = 1L;
  27. private final JPanel contentPanel = new JPanel();
  28. private JTextField capacityField;
  29. private float capacity;
  30. private JRadioButton rdbtnChangeForAll;
  31. private JRadioButton rdbtnChangeForNew;
  32. private JRadioButton rdbtnChangeForAll1;
  33. private Control controller;
  34. private MyCanvas canvas;
  35. private JLabel lblenterinfiniteFor;
  36. /**
  37. * Launch the application.
  38. *
  39. * @param args
  40. * standard
  41. */
  42. public static void main(String[] args) {
  43. try {
  44. EditEdgesPopUp dialog = new EditEdgesPopUp();
  45. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  46. dialog.setVisible(true);
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. /**
  52. * Constructor.
  53. */
  54. public EditEdgesPopUp() {
  55. super((java.awt.Frame) null, true);
  56. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  57. this.setTitle(Languages.getLanguage()[46]);
  58. setBounds(100, 100, 600, 220);
  59. getContentPane().setLayout(new BorderLayout());
  60. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  61. getContentPane().add(contentPanel, BorderLayout.CENTER);
  62. contentPanel.setLayout(null);
  63. JLabel lblMaximumCapacity = new JLabel(Languages.getLanguage()[47]);
  64. lblMaximumCapacity.setFont(new Font("Tahoma", Font.PLAIN, 11));
  65. lblMaximumCapacity.setBounds(10, 11, 98, 14);
  66. contentPanel.add(lblMaximumCapacity);
  67. capacityField = new JTextField();
  68. capacityField.setBounds(107, 8, 120, 20);
  69. contentPanel.add(capacityField);
  70. capacityField.setColumns(10);
  71. rdbtnChangeForAll = new JRadioButton(Languages.getLanguage()[48]);
  72. rdbtnChangeForAll.setBounds(10, 39, 330, 23);
  73. contentPanel.add(rdbtnChangeForAll);
  74. rdbtnChangeForNew = new JRadioButton(Languages.getLanguage()[49]);
  75. rdbtnChangeForNew.setBounds(10, 65, 330, 23);
  76. contentPanel.add(rdbtnChangeForNew);
  77. rdbtnChangeForAll1 = new JRadioButton(Languages.getLanguage()[50]);
  78. rdbtnChangeForAll1.setBounds(10, 95, 400, 23);
  79. contentPanel.add(rdbtnChangeForAll1);
  80. JButton btnCancel = new JButton(Languages.getLanguage()[51]);
  81. btnCancel.setActionCommand("Cancel");
  82. btnCancel.addActionListener(new ActionListener() {
  83. public void actionPerformed(ActionEvent arg0) {
  84. dispose();
  85. }
  86. });
  87. btnCancel.setBounds(285, 147, 89, 23);
  88. contentPanel.add(btnCancel);
  89. JButton btnOk1 = new JButton("OK");
  90. btnOk1.addActionListener(new ActionListener() {
  91. public void actionPerformed(ActionEvent e) {
  92. if (capacityField.getText().equals("infinite")) {
  93. capacity = -1;
  94. if (rdbtnChangeForAll.isSelected()) {
  95. changeForExisting(capacity);
  96. dispose();
  97. } else if (rdbtnChangeForNew.isSelected()) {
  98. changeForNew(capacity);
  99. dispose();
  100. } else if (rdbtnChangeForAll1.isSelected()) {
  101. changeForExAndNew(capacity);
  102. dispose();
  103. } else {
  104. JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[52]);
  105. }
  106. } else {
  107. try {
  108. capacity = Float.parseFloat(capacityField.getText().toString());
  109. if (capacity < 0) {
  110. throw new NumberFormatException();
  111. }
  112. if (rdbtnChangeForAll.isSelected()) {
  113. changeForExisting(capacity);
  114. dispose();
  115. } else if (rdbtnChangeForNew.isSelected()) {
  116. changeForNew(capacity);
  117. dispose();
  118. } else if (rdbtnChangeForAll1.isSelected()) {
  119. changeForExAndNew(capacity);
  120. dispose();
  121. } else {
  122. JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[52]);
  123. }
  124. } catch (NumberFormatException eex) {
  125. JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[53]);
  126. }
  127. }
  128. }
  129. });
  130. btnOk1.setBounds(186, 147, 89, 23);
  131. contentPanel.add(btnOk1);
  132. this.setTitle(Languages.getLanguage()[54]);
  133. ButtonGroup bG = new ButtonGroup();
  134. bG.add(rdbtnChangeForAll1);
  135. bG.add(rdbtnChangeForNew);
  136. bG.add(rdbtnChangeForAll);
  137. lblenterinfiniteFor = new JLabel(Languages.getLanguage()[55]);
  138. lblenterinfiniteFor.setBounds(237, 11, 360, 14);
  139. contentPanel.add(lblenterinfiniteFor);
  140. }
  141. /**
  142. * Set the Canvas.
  143. *
  144. * @param can
  145. * the Canvas
  146. */
  147. public void setCanvas(MyCanvas can) {
  148. canvas = can;
  149. }
  150. /**
  151. * set the Controller.
  152. *
  153. * @param cont
  154. * the Controller
  155. */
  156. public void setController(Control cont) {
  157. controller = cont;
  158. }
  159. /**
  160. * set edge capacity for new edges.
  161. *
  162. * @param cap
  163. * the capacity
  164. */
  165. public void changeForNew(float cap) {
  166. controller.setMaxCapacity(cap);
  167. controller.resetSimulation();
  168. }
  169. /**
  170. * Set Capacity for all existing Edges.
  171. *
  172. * @param cap
  173. * the Capacity
  174. */
  175. public void changeForExisting(float cap) {
  176. /*
  177. * for(SubNet sn: controller.getSimManager().getSubNets()){ for(CpsEdge
  178. * edge: sn.getEdges()){ edge.setCapacity(cap); } } for(CpsEdge edge:
  179. * controller.getSimManager().getBrokenEdges()){ edge.setCapacity(cap);
  180. * }
  181. */
  182. for (CpsEdge edge : controller.getModel().getEdgesOnCanvas()) {
  183. edge.setCapacity(cap);
  184. edge.setState(true);
  185. }
  186. for (AbstractCpsObject abs : controller.getModel().getObjectsOnCanvas()) {
  187. if (abs instanceof CpsUpperNode) {
  188. changeInUpperNode((CpsUpperNode) abs, cap);
  189. }
  190. }
  191. controller.resetSimulation();
  192. controller.calculateStateForCurrentTimeStep();
  193. canvas.repaint();
  194. }
  195. public void changeInUpperNode(CpsUpperNode uN, float cap) {
  196. for (CpsEdge e : uN.getNodeEdges()) {
  197. e.setCapacity(cap);
  198. }
  199. for (CpsEdge e : uN.getOldEdges()) {
  200. e.setCapacity(cap);
  201. }
  202. for (AbstractCpsObject abs : uN.getNodes()) {
  203. if (abs instanceof CpsUpperNode) {
  204. changeInUpperNode((CpsUpperNode) abs, cap);
  205. }
  206. }
  207. }
  208. /**
  209. * Set the Capacity for all existing and new edges.
  210. *
  211. * @param cap
  212. * the capacity
  213. */
  214. public void changeForExAndNew(float cap) {
  215. changeForNew(cap);
  216. changeForExisting(cap);
  217. }
  218. }