AddElementPopUp.java 6.6 KB

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