AddElementPopUp.java 5.7 KB

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