AddElementPopUp.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package holeg.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 holeg.model.HolonElement;
  17. import holeg.model.HolonObject;
  18. import holeg.preferences.ColorPreference;
  19. import holeg.preferences.ImagePreference;
  20. import holeg.utility.image.Import;
  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. /**
  29. * Serial.
  30. */
  31. private static final long serialVersionUID = 1L;
  32. /* Data */
  33. /** Holon Object the Element should be added to */
  34. private HolonObject tempCps;
  35. /** Holon Element that should be edited (if in edit Modus */
  36. private HolonElement hl;
  37. /* GUI */
  38. /** Panel containing everything */
  39. private final JPanel contentPanel = new JPanel();
  40. /** Textfield for entering a Name for the Element */
  41. private JTextField elementName;
  42. /** Textfield for the energy the Element consumes/produces */
  43. private JTextField providedEnergy;
  44. /** Element is active if checked */
  45. JCheckBox checkBoxActive;
  46. /**
  47. * Create the AddElementPopup Dialog
  48. * @param parentFrame
  49. */
  50. AddElementPopUp(JFrame parentFrame) {
  51. super((java.awt.Frame) null, true);
  52. this.setIconImage(Import.loadImage(ImagePreference.Logo, 30, 30));
  53. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  54. setBounds(100, 100, 400, 245);
  55. setLocationRelativeTo(parentFrame);
  56. getContentPane().setLayout(new BorderLayout());
  57. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  58. getContentPane().add(contentPanel, BorderLayout.CENTER);
  59. contentPanel.setLayout(null);
  60. this.setTitle("Add Element to Object");
  61. /* Element Name Textfield and Label */
  62. elementName = new JTextField();
  63. elementName.addKeyListener(new KeyListener() {
  64. @Override
  65. public void keyPressed(KeyEvent arg0) {
  66. }
  67. @Override
  68. public void keyReleased(KeyEvent e) {
  69. }
  70. @Override
  71. public void keyTyped(KeyEvent e) {
  72. elementName.setBackground(Color.WHITE);
  73. }
  74. });
  75. JLabel lblElementName = new JLabel("Element Name:");
  76. lblElementName.setBounds(10, 10, 100, 20);
  77. contentPanel.add(lblElementName);
  78. elementName.setBounds(130, 10, 110, 20);
  79. contentPanel.add(elementName);
  80. elementName.setColumns(10);
  81. /* Add Provided Energy Label and Textfield */
  82. JLabel lblProvidedEnergy = new JLabel("Provided Energy:");
  83. lblProvidedEnergy.setBounds(10, 50, 120, 20);
  84. contentPanel.add(lblProvidedEnergy);
  85. providedEnergy = new JTextField();
  86. providedEnergy.setBounds(130, 50, 110, 20);
  87. contentPanel.add(providedEnergy);
  88. providedEnergy.setColumns(10);
  89. providedEnergy.setText("0");
  90. checkBoxActive = new JCheckBox("Active");
  91. checkBoxActive.setSelected(true);
  92. checkBoxActive.setBounds(250, 50, 115, 20);
  93. contentPanel.add(checkBoxActive);
  94. /* Add Buttons and Actions */
  95. JPanel buttonPane = new JPanel();
  96. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  97. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  98. JButton okButton = new JButton("OK");
  99. okButton.addActionListener(arg0 -> okAction());
  100. okButton.setActionCommand("OK");
  101. buttonPane.add(okButton);
  102. getRootPane().setDefaultButton(okButton);
  103. JButton cancelButton = new JButton("Cancel");
  104. cancelButton.setActionCommand("Cancel");
  105. buttonPane.add(cancelButton);
  106. cancelButton.addActionListener(e -> dispose());
  107. }
  108. /**
  109. * Sets the actual Cps.
  110. *
  111. * @param cps
  112. * 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. /**
  126. * setElement that should be edited
  127. * @param holonElement
  128. */
  129. public void setElement(HolonElement holonElement) {
  130. hl = holonElement;
  131. elementName.setText(hl.getName());
  132. providedEnergy.setText(""+hl.getEnergy());
  133. checkBoxActive.setSelected(hl.active);
  134. }
  135. /**
  136. * Trys to create/edit the Element
  137. */
  138. private void okAction() {
  139. boolean repeated = false;
  140. for (HolonElement e : tempCps.getElements().toList()) {
  141. if (elementName.getText().equals(e.getName())&&(hl == null || hl.getId()!=e.getId())) {
  142. repeated = true;
  143. break;
  144. }
  145. }
  146. if (elementName.getText().length() != 0 && !repeated) {
  147. try {
  148. float energy = Float.parseFloat(providedEnergy.getText());
  149. if(hl == null){
  150. hl = new HolonElement(this.tempCps, elementName.getText(), energy);
  151. } else {
  152. hl.setName(elementName.getText());
  153. hl.setEnergy(energy);
  154. hl.active = checkBoxActive.isSelected();
  155. }
  156. dispose();
  157. } catch (NumberFormatException e) {
  158. JOptionPane.showMessageDialog(new JFrame(), "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. }