AddElementPopUp.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. public class AddElementPopUp extends JDialog {
  25. /* Data */
  26. /**
  27. * Panel containing everything
  28. */
  29. private final JPanel contentPanel = new JPanel();
  30. /**
  31. * Textfield for entering a Name for the Element
  32. */
  33. private final JTextField elementName;
  34. /* GUI */
  35. /**
  36. * Textfield for the energy the Element consumes/produces
  37. */
  38. private final JTextField providedEnergy;
  39. /**
  40. * Element is active if checked
  41. */
  42. JCheckBox checkBoxActive;
  43. /**
  44. * Holon Object the Element should be added to
  45. */
  46. private HolonObject tempCps;
  47. /**
  48. * Holon Element that should be edited (if in edit Modus
  49. */
  50. private HolonElement hl;
  51. AddElementPopUp(JFrame parentFrame) {
  52. super((java.awt.Frame) null, true);
  53. this.setIconImage(Import.loadImage(ImagePreference.Logo, 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("Add Element to Object");
  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("Element Name:");
  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. /* Add Provided Energy Label and Textfield */
  83. JLabel lblProvidedEnergy = new JLabel("Provided Energy:");
  84. lblProvidedEnergy.setBounds(10, 50, 120, 20);
  85. contentPanel.add(lblProvidedEnergy);
  86. providedEnergy = new JTextField();
  87. providedEnergy.setBounds(130, 50, 110, 20);
  88. contentPanel.add(providedEnergy);
  89. providedEnergy.setColumns(10);
  90. providedEnergy.setText("0");
  91. checkBoxActive = new JCheckBox("Active");
  92. checkBoxActive.setSelected(true);
  93. checkBoxActive.setBounds(250, 50, 115, 20);
  94. contentPanel.add(checkBoxActive);
  95. /* Add Buttons and Actions */
  96. JPanel buttonPane = new JPanel();
  97. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  98. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  99. JButton okButton = new JButton("OK");
  100. okButton.addActionListener(arg0 -> okAction());
  101. okButton.setActionCommand("OK");
  102. buttonPane.add(okButton);
  103. getRootPane().setDefaultButton(okButton);
  104. JButton cancelButton = new JButton("Cancel");
  105. cancelButton.setActionCommand("Cancel");
  106. buttonPane.add(cancelButton);
  107. cancelButton.addActionListener(e -> dispose());
  108. }
  109. /**
  110. * Sets the actual Cps.
  111. *
  112. * @param cps actual Cps
  113. */
  114. void setActualHolonObject(HolonObject cps) {
  115. this.tempCps = cps;
  116. }
  117. /**
  118. * Returns the created Element.
  119. *
  120. * @return the Element
  121. */
  122. public HolonElement getElement() {
  123. return hl;
  124. }
  125. public void setElement(HolonElement holonElement) {
  126. hl = holonElement;
  127. elementName.setText(hl.getName());
  128. providedEnergy.setText("" + hl.getEnergy());
  129. checkBoxActive.setSelected(hl.active);
  130. }
  131. /**
  132. * Trys to create/edit the Element
  133. */
  134. private void okAction() {
  135. boolean repeated = false;
  136. for (HolonElement e : tempCps.elementsStream().toList()) {
  137. if (elementName.getText().equals(e.getName()) && (hl == null)) {
  138. repeated = true;
  139. break;
  140. }
  141. }
  142. if (elementName.getText().length() != 0 && !repeated) {
  143. try {
  144. float energy = Float.parseFloat(providedEnergy.getText());
  145. if (hl == null) {
  146. hl = new HolonElement(this.tempCps, elementName.getText(), energy);
  147. } else {
  148. hl.setName(elementName.getText());
  149. hl.setEnergy(energy);
  150. hl.active = checkBoxActive.isSelected();
  151. }
  152. dispose();
  153. } catch (NumberFormatException e) {
  154. JOptionPane.showMessageDialog(new JFrame(),
  155. "Please enter numbers in the Fields amount and Energy");
  156. }
  157. } else {
  158. if (elementName.getText().isEmpty()) {
  159. JLabel errorString = new JLabel("No name");
  160. errorString.setBounds(240, 8, 100, 20);
  161. contentPanel.add(errorString);
  162. } else if (repeated) {
  163. JLabel errorString = new JLabel("Name already given");
  164. errorString.setBounds(250, 8, 100, 20);
  165. contentPanel.add(errorString);
  166. }
  167. elementName.setBackground(ColorPreference.Dialog.BackgroundColor);
  168. }
  169. }
  170. }