AddElementPopUp.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package ui.view.dialog;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import javax.swing.JButton;
  8. import javax.swing.JCheckBox;
  9. import javax.swing.JDialog;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPanel;
  14. import javax.swing.JTextField;
  15. import javax.swing.border.EmptyBorder;
  16. import classes.HolonElement;
  17. import classes.HolonObject;
  18. import utility.ImageImport;
  19. /**
  20. * popup for adding an Holon Element to a holon Object.
  21. *
  22. * @author Gruppe14
  23. * @author improved by A.T.M.-B. (Gruppe007)
  24. */
  25. public class AddElementPopUp extends JDialog {
  26. /**
  27. * Serial.
  28. */
  29. private static final long serialVersionUID = 1L;
  30. /* Data */
  31. /** Holon Object the Element should be added to */
  32. private HolonObject tempCps;
  33. /** Holon Element that should be edited (if in edit Modus */
  34. private HolonElement hl;
  35. /* GUI */
  36. /** Panel containing everything */
  37. private final JPanel contentPanel = new JPanel();
  38. /** Textfield for entering a Name for the Element */
  39. private JTextField elementName;
  40. /** Textfield for the energy the Element consumes/produces */
  41. private JTextField providedEnergy;
  42. /** Element is active if checked */
  43. JCheckBox checkBoxActive;
  44. /**
  45. * Create the AddElementPopup Dialog
  46. * @param parentFrame
  47. */
  48. AddElementPopUp(JFrame parentFrame) {
  49. super((java.awt.Frame) null, true);
  50. this.setIconImage(ImageImport.loadImage("/Images/Holeg.png", 30, 30));
  51. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  52. setBounds(100, 100, 400, 245);
  53. setLocationRelativeTo(parentFrame);
  54. getContentPane().setLayout(new BorderLayout());
  55. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  56. getContentPane().add(contentPanel, BorderLayout.CENTER);
  57. contentPanel.setLayout(null);
  58. this.setTitle("Add Element to Object");
  59. /* Element Name Textfield and Label */
  60. elementName = new JTextField();
  61. elementName.addKeyListener(new KeyListener() {
  62. @Override
  63. public void keyPressed(KeyEvent arg0) {
  64. }
  65. @Override
  66. public void keyReleased(KeyEvent e) {
  67. }
  68. @Override
  69. public void keyTyped(KeyEvent e) {
  70. elementName.setBackground(Color.WHITE);
  71. }
  72. });
  73. JLabel lblElementName = new JLabel("Element Name:");
  74. lblElementName.setBounds(10, 10, 100, 20);
  75. contentPanel.add(lblElementName);
  76. elementName.setBounds(130, 10, 110, 20);
  77. contentPanel.add(elementName);
  78. elementName.setColumns(10);
  79. /* Add Provided Energy Label and Textfield */
  80. JLabel lblProvidedEnergy = new JLabel("Provided Energy:");
  81. lblProvidedEnergy.setBounds(10, 50, 120, 20);
  82. contentPanel.add(lblProvidedEnergy);
  83. providedEnergy = new JTextField();
  84. providedEnergy.setBounds(130, 50, 110, 20);
  85. contentPanel.add(providedEnergy);
  86. providedEnergy.setColumns(10);
  87. providedEnergy.setText("0");
  88. checkBoxActive = new JCheckBox("Active");
  89. checkBoxActive.setSelected(true);
  90. checkBoxActive.setBounds(250, 50, 115, 20);
  91. contentPanel.add(checkBoxActive);
  92. /* Add Buttons and Actions */
  93. JPanel buttonPane = new JPanel();
  94. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  95. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  96. JButton okButton = new JButton("OK");
  97. okButton.addActionListener(arg0 -> okAction());
  98. okButton.setActionCommand("OK");
  99. buttonPane.add(okButton);
  100. getRootPane().setDefaultButton(okButton);
  101. JButton cancelButton = new JButton("Cancel");
  102. cancelButton.setActionCommand("Cancel");
  103. buttonPane.add(cancelButton);
  104. cancelButton.addActionListener(e -> dispose());
  105. }
  106. /**
  107. * Sets the actual Cps.
  108. *
  109. * @param cps
  110. * actual Cps
  111. */
  112. void setActualHolonObject(HolonObject cps) {
  113. this.tempCps = cps;
  114. }
  115. /**
  116. * Returns the created Element.
  117. *
  118. * @return the Element
  119. */
  120. public HolonElement getElement() {
  121. return hl;
  122. }
  123. /**
  124. * setElement that should be edited
  125. * @param holonElement
  126. */
  127. public void setElement(HolonElement holonElement) {
  128. hl = holonElement;
  129. elementName.setText(hl.getName());
  130. providedEnergy.setText(""+hl.getEnergy());
  131. checkBoxActive.setSelected(hl.isActive());
  132. }
  133. /**
  134. * Trys to create/edit the Element
  135. */
  136. private void okAction() {
  137. boolean repeated = false;
  138. for (HolonElement e : tempCps.getElements()) {
  139. if (elementName.getText().equals(e.getName())&&(hl == null || hl.getId()!=e.getId())) {
  140. repeated = true;
  141. break;
  142. }
  143. }
  144. if (elementName.getText().length() != 0 && !repeated) {
  145. try {
  146. float energy = Float.parseFloat(providedEnergy.getText());
  147. if(hl == null){
  148. hl = new HolonElement(this.tempCps, elementName.getText(), energy);
  149. } else {
  150. hl.setEleName(elementName.getText());
  151. hl.setEnergyPerElement(energy);
  152. hl.setActive(checkBoxActive.isSelected());
  153. }
  154. dispose();
  155. } catch (NumberFormatException e) {
  156. JOptionPane.showMessageDialog(new JFrame(), "Please enter numbers in the Fields amount and Energy");
  157. }
  158. } else {
  159. if (elementName.getText().length() == 0) {
  160. JLabel errorString = new JLabel("No name");
  161. errorString.setBounds(240, 8, 100, 20);
  162. contentPanel.add(errorString);
  163. } else if (repeated) {
  164. JLabel errorString = new JLabel("Name already given");
  165. errorString.setBounds(250, 8, 100, 20);
  166. contentPanel.add(errorString);
  167. }
  168. elementName.setBackground(new Color(255, 50, 50));
  169. }
  170. }
  171. }