AddElementPopUp.java 5.4 KB

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