AddStorageElementPopUp.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package ui.view;
  2. import classes.AbstractCpsObject;
  3. import classes.HolonElement;
  4. import classes.HolonObject;
  5. import classes.StorageElement;
  6. import ui.model.Model;
  7. import javax.swing.*;
  8. import javax.swing.border.EmptyBorder;
  9. import java.awt.*;
  10. import java.awt.event.KeyEvent;
  11. import java.awt.event.KeyListener;
  12. /**
  13. * popup for adding an Holon Element to a holon Object.
  14. *
  15. * @author Gruppe14
  16. * @author improved by A.T.M.-B. (Gruppe007)
  17. */
  18. public class AddStorageElementPopUp extends JDialog {
  19. /**
  20. * Serial.
  21. */
  22. private static final long serialVersionUID = 1L;
  23. /* Data */
  24. /** Holon Object the Element should be added to */
  25. private AbstractCpsObject tempCps;
  26. /** Holon Element that should be edited (if in edit Modus */
  27. private StorageElement hl;
  28. /* GUI */
  29. /** Panel containing everything */
  30. private final JPanel contentPanel = new JPanel();
  31. /** Textfield for entering a Name for the Element */
  32. private JTextField elementName;
  33. /** Textfield for the energy the Element consumes/produces */
  34. private JTextField capacity;
  35. /** Element is active if checked */
  36. JCheckBox checkBoxActive;
  37. /** Textfield to enter the amount of this Element */
  38. private JTextField amount;
  39. /** Textfield to enter the flexible Energy of the Element */
  40. private JTextField maxInRatio;
  41. private JTextField maxOutRatio;
  42. /** Flexible if checkbox is checked */
  43. JCheckBox checkboxFlexible;
  44. /** Model which is used */
  45. private Model model;
  46. /**
  47. * Create the AddElementPopup Dialog
  48. * @param parentFrame
  49. */
  50. AddStorageElementPopUp(JFrame parentFrame, Model model) {
  51. super((java.awt.Frame) null, true);
  52. this.model = model;
  53. this.setIconImage(Util.loadImage("/Images/Holeg.png", 30, 30));
  54. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  55. setBounds(100, 100, 400, 245);
  56. setLocationRelativeTo(parentFrame);
  57. getContentPane().setLayout(new BorderLayout());
  58. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  59. getContentPane().add(contentPanel, BorderLayout.CENTER);
  60. contentPanel.setLayout(null);
  61. this.setTitle(Languages.getLanguage()[64]);
  62. /* Element Name Textfield and Label */
  63. elementName = new JTextField();
  64. elementName.addKeyListener(new KeyListener() {
  65. @Override
  66. public void keyPressed(KeyEvent arg0) {
  67. }
  68. @Override
  69. public void keyReleased(KeyEvent e) {
  70. }
  71. @Override
  72. public void keyTyped(KeyEvent e) {
  73. elementName.setBackground(Color.WHITE);
  74. }
  75. });
  76. JLabel lblElementName = new JLabel(Languages.getLanguage()[65]);
  77. lblElementName.setBounds(10, 10, 100, 20);
  78. contentPanel.add(lblElementName);
  79. elementName.setBounds(130, 10, 110, 20);
  80. contentPanel.add(elementName);
  81. elementName.setColumns(10);
  82. elementName.setText("Storage");
  83. /* Add Provided Energy Label and Textfield */
  84. JLabel lblCapacity = new JLabel("Capacity in kWh");
  85. lblCapacity.setBounds(10, 50, 120, 20);
  86. contentPanel.add(lblCapacity);
  87. capacity = new JTextField();
  88. capacity.setBounds(130, 50, 110, 20);
  89. contentPanel.add(capacity);
  90. capacity.setColumns(10);
  91. capacity.setText("13.5");
  92. checkBoxActive = new JCheckBox("Active");
  93. checkBoxActive.setSelected(true);
  94. checkBoxActive.setBounds(250, 50, 115, 20);
  95. contentPanel.add(checkBoxActive);
  96. /* Add Flexible Energy Textfield and CheckBox */
  97. JLabel lblinRatio = new JLabel("maxInRatio in kW");
  98. lblinRatio.setBounds(10, 90, 100, 20);
  99. contentPanel.add(lblinRatio);
  100. maxInRatio = new JTextField();
  101. maxInRatio.setText("2");
  102. maxInRatio.setColumns(10);
  103. maxInRatio.setBounds(130, 90, 110, 20);
  104. contentPanel.add(maxInRatio);
  105. JLabel lbloutRatio = new JLabel("nominal OutRatio in kW");
  106. lbloutRatio.setBounds(10, 130, 100, 20);
  107. contentPanel.add(lbloutRatio);
  108. maxOutRatio = new JTextField();
  109. maxOutRatio.setText("4.6");
  110. maxOutRatio.setColumns(10);
  111. maxOutRatio.setBounds(130, 130, 110, 20);
  112. contentPanel.add(maxOutRatio);
  113. // checkboxFlexible = new JCheckBox("Flexible");
  114. // checkboxFlexible.setBounds(250, 90, 115, 20);
  115. // contentPanel.add(checkboxFlexible);
  116. /* Add Amount Textfield and Checkbox */
  117. JLabel lblAmount = new JLabel(Languages.getLanguage()[67]);
  118. lblAmount.setBounds(10, 170, 100, 14);
  119. contentPanel.add(lblAmount);
  120. amount = new JTextField();
  121. amount.setBounds(130, 170, 110, 20);
  122. contentPanel.add(amount);
  123. amount.setColumns(10);
  124. amount.setText("1");
  125. /* Add Buttons and Actions */
  126. JPanel buttonPane = new JPanel();
  127. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  128. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  129. JButton okButton = new JButton("OK");
  130. okButton.addActionListener(arg0 -> okAction());
  131. okButton.setActionCommand("OK");
  132. buttonPane.add(okButton);
  133. getRootPane().setDefaultButton(okButton);
  134. JButton cancelButton = new JButton(Languages.getLanguage()[71]);
  135. cancelButton.setActionCommand("Cancel");
  136. buttonPane.add(cancelButton);
  137. cancelButton.addActionListener(e -> dispose());
  138. }
  139. /**
  140. * Sets the actual Cps.
  141. *
  142. * @param cps
  143. * actual Cps
  144. */
  145. void setActualCps(AbstractCpsObject cps) {
  146. this.tempCps = cps;
  147. }
  148. /**
  149. * Returns the created Element.
  150. *
  151. * @return the Element
  152. */
  153. public StorageElement getElement() {
  154. return hl;
  155. }
  156. /**
  157. * setElement that should be edited
  158. * @param storageElement
  159. */
  160. public void setElement(StorageElement storageElement) {
  161. hl = storageElement;
  162. elementName.setText(hl.getEleName());
  163. amount.setText(""+hl.getAmount());
  164. capacity.setText(""+hl.getEnergyPerElement());
  165. checkBoxActive.setSelected(hl.isActive());
  166. }
  167. /**
  168. * Trys to create/edit the Element
  169. */
  170. private void okAction() {
  171. boolean repeated = false;
  172. for (HolonElement e : ((HolonObject) tempCps).getElements()) {
  173. if (elementName.getText().equals(e.getEleName())&&(hl == null || hl.getId()!=e.getId())) {
  174. repeated = true;
  175. break;
  176. }
  177. }
  178. if (elementName.getText().length() != 0 && !repeated) {
  179. try {
  180. float elementCapacity = Float.parseFloat(capacity.getText())*1000;
  181. float maxIn = Float.parseFloat(maxInRatio.getText())*1000;
  182. float maxOut = Float.parseFloat(maxOutRatio.getText())*1000;
  183. int elementAmount = Integer.parseInt(amount.getText());
  184. if(hl == null){
  185. hl = new StorageElement(elementName.getText(), elementAmount, 0, model, elementCapacity, maxIn, maxOut);
  186. } else {
  187. hl.setEleName(elementName.getText());
  188. hl.setAmount(elementAmount);
  189. hl.setEnergyPerElement(0);
  190. hl.setStatusAndSetEnergy(StorageElement.Mode.STANDBY, 0 ,0);
  191. }
  192. hl.setActive(checkBoxActive.isSelected());
  193. dispose();
  194. } catch (NumberFormatException e) {
  195. JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[68]);
  196. }
  197. } else {
  198. if (elementName.getText().length() == 0) {
  199. JLabel errorString = new JLabel(Languages.getLanguage()[69]);
  200. errorString.setBounds(240, 8, 100, 20);
  201. contentPanel.add(errorString);
  202. } else if (repeated) {
  203. JLabel errorString = new JLabel(Languages.getLanguage()[70]);
  204. errorString.setBounds(250, 8, 100, 20);
  205. contentPanel.add(errorString);
  206. }
  207. elementName.setBackground(new Color(255, 50, 50));
  208. }
  209. }
  210. }