AddElementPopUp.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.utility.ImageImport;
  20. /**
  21. * popup for adding an Holon Element to a holon Object.
  22. *
  23. * @author Gruppe14
  24. * @author improved by A.T.M.-B. (Gruppe007)
  25. */
  26. public class AddElementPopUp extends JDialog {
  27. /**
  28. * Serial.
  29. */
  30. private static final long serialVersionUID = 1L;
  31. /* Data */
  32. /** Holon Object the Element should be added to */
  33. private HolonObject tempCps;
  34. /** Holon Element that should be edited (if in edit Modus */
  35. private HolonElement hl;
  36. /* GUI */
  37. /** Panel containing everything */
  38. private final JPanel contentPanel = new JPanel();
  39. /** Textfield for entering a Name for the Element */
  40. private JTextField elementName;
  41. /** Textfield for the energy the Element consumes/produces */
  42. private JTextField providedEnergy;
  43. /** Element is active if checked */
  44. JCheckBox checkBoxActive;
  45. /**
  46. * Create the AddElementPopup Dialog
  47. * @param parentFrame
  48. */
  49. AddElementPopUp(JFrame parentFrame) {
  50. super((java.awt.Frame) null, true);
  51. this.setIconImage(ImageImport.loadImage("/Images/Holeg.png", 30, 30));
  52. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  53. setBounds(100, 100, 400, 245);
  54. setLocationRelativeTo(parentFrame);
  55. getContentPane().setLayout(new BorderLayout());
  56. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  57. getContentPane().add(contentPanel, BorderLayout.CENTER);
  58. contentPanel.setLayout(null);
  59. this.setTitle("Add Element to Object");
  60. /* Element Name Textfield and Label */
  61. elementName = new JTextField();
  62. elementName.addKeyListener(new KeyListener() {
  63. @Override
  64. public void keyPressed(KeyEvent arg0) {
  65. }
  66. @Override
  67. public void keyReleased(KeyEvent e) {
  68. }
  69. @Override
  70. public void keyTyped(KeyEvent e) {
  71. elementName.setBackground(Color.WHITE);
  72. }
  73. });
  74. JLabel lblElementName = new JLabel("Element Name:");
  75. lblElementName.setBounds(10, 10, 100, 20);
  76. contentPanel.add(lblElementName);
  77. elementName.setBounds(130, 10, 110, 20);
  78. contentPanel.add(elementName);
  79. elementName.setColumns(10);
  80. /* Add Provided Energy Label and Textfield */
  81. JLabel lblProvidedEnergy = new JLabel("Provided Energy:");
  82. lblProvidedEnergy.setBounds(10, 50, 120, 20);
  83. contentPanel.add(lblProvidedEnergy);
  84. providedEnergy = new JTextField();
  85. providedEnergy.setBounds(130, 50, 110, 20);
  86. contentPanel.add(providedEnergy);
  87. providedEnergy.setColumns(10);
  88. providedEnergy.setText("0");
  89. checkBoxActive = new JCheckBox("Active");
  90. checkBoxActive.setSelected(true);
  91. checkBoxActive.setBounds(250, 50, 115, 20);
  92. contentPanel.add(checkBoxActive);
  93. /* Add Buttons and Actions */
  94. JPanel buttonPane = new JPanel();
  95. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  96. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  97. JButton okButton = new JButton("OK");
  98. okButton.addActionListener(arg0 -> okAction());
  99. okButton.setActionCommand("OK");
  100. buttonPane.add(okButton);
  101. getRootPane().setDefaultButton(okButton);
  102. JButton cancelButton = new JButton("Cancel");
  103. cancelButton.setActionCommand("Cancel");
  104. buttonPane.add(cancelButton);
  105. cancelButton.addActionListener(e -> dispose());
  106. }
  107. /**
  108. * Sets the actual Cps.
  109. *
  110. * @param cps
  111. * actual Cps
  112. */
  113. void setActualHolonObject(HolonObject cps) {
  114. this.tempCps = cps;
  115. }
  116. /**
  117. * Returns the created Element.
  118. *
  119. * @return the Element
  120. */
  121. public HolonElement getElement() {
  122. return hl;
  123. }
  124. /**
  125. * setElement that should be edited
  126. * @param holonElement
  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.getElements()) {
  140. if (elementName.getText().equals(e.getName())&&(hl == null || hl.getId()!=e.getId())) {
  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(), "Please enter numbers in the Fields amount and Energy");
  158. }
  159. } else {
  160. if (elementName.getText().isEmpty()) {
  161. JLabel errorString = new JLabel("No name");
  162. errorString.setBounds(240, 8, 100, 20);
  163. contentPanel.add(errorString);
  164. } else if (repeated) {
  165. JLabel errorString = new JLabel("Name already given");
  166. errorString.setBounds(250, 8, 100, 20);
  167. contentPanel.add(errorString);
  168. }
  169. elementName.setBackground(ColorPreference.Dialog.BackgroundColor);
  170. }
  171. }
  172. }