AddElementPopUp.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package ui.view;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.FlowLayout;
  5. import java.awt.Image;
  6. import javax.swing.JButton;
  7. import javax.swing.JDialog;
  8. import javax.swing.JPanel;
  9. import javax.swing.border.EmptyBorder;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JTextField;
  14. import javax.swing.ImageIcon;
  15. import classes.AbstractCpsObject;
  16. import classes.HolonElement;
  17. import classes.HolonObject;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.KeyEvent;
  20. import java.awt.event.KeyListener;
  21. import java.awt.event.ActionEvent;
  22. /**
  23. * popup for adding an Holon Element to a holon Object.
  24. *
  25. * @author Gruppe14
  26. */
  27. public class AddElementPopUp extends JDialog {
  28. /**
  29. * Serial.
  30. */
  31. private static final long serialVersionUID = 1L;
  32. private final JPanel contentPanel = new JPanel();
  33. private JTextField elementName;
  34. private JTextField providedEnergy;
  35. private JTextField amount;
  36. private HolonElement hl;
  37. private AbstractCpsObject tempCps;
  38. /**
  39. * Launch the application.
  40. *
  41. * @param args standard
  42. */
  43. public static void main(String[] args) {
  44. try {
  45. AddElementPopUp dialog = new AddElementPopUp();
  46. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  47. dialog.setVisible(true);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. /**
  53. * Create the dialog.
  54. */
  55. public AddElementPopUp() {
  56. super((java.awt.Frame) null, true);
  57. this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  58. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  59. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  60. setBounds(100, 100, 400, 190);
  61. getContentPane().setLayout(new BorderLayout());
  62. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  63. getContentPane().add(contentPanel, BorderLayout.CENTER);
  64. contentPanel.setLayout(null);
  65. this.setTitle("Add default Element to Object");
  66. JLabel lblElementName = new JLabel("Element Name:");
  67. lblElementName.setBounds(10, 11, 100, 14);
  68. contentPanel.add(lblElementName);
  69. JLabel lblProvidedEnergy = new JLabel("Provided Energy:");
  70. lblProvidedEnergy.setBounds(10, 49, 120, 14);
  71. contentPanel.add(lblProvidedEnergy);
  72. JLabel lblAmount = new JLabel("Amount:");
  73. lblAmount.setBounds(10, 84, 100, 14);
  74. contentPanel.add(lblAmount);
  75. elementName = new JTextField();
  76. elementName.addKeyListener(new KeyListener() {
  77. @Override
  78. public void keyPressed(KeyEvent arg0) {
  79. // TODO Auto-generated method stub
  80. }
  81. @Override
  82. public void keyReleased(KeyEvent e) {
  83. // TODO Auto-generated method stub
  84. }
  85. @Override
  86. public void keyTyped(KeyEvent e) {
  87. elementName.setBackground(Color.WHITE);
  88. }
  89. });
  90. elementName.setBounds(130, 8, 110, 20);
  91. contentPanel.add(elementName);
  92. elementName.setColumns(10);
  93. providedEnergy = new JTextField();
  94. providedEnergy.setBounds(130, 46, 110, 20);
  95. contentPanel.add(providedEnergy);
  96. providedEnergy.setColumns(10);
  97. providedEnergy.setText("0");
  98. amount = new JTextField();
  99. amount.setBounds(130, 81, 110, 20);
  100. contentPanel.add(amount);
  101. amount.setColumns(10);
  102. amount.setText("1");
  103. {
  104. JPanel buttonPane = new JPanel();
  105. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  106. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  107. {
  108. JButton okButton = new JButton("OK");
  109. okButton.addActionListener(new ActionListener() {
  110. public void actionPerformed(ActionEvent arg0) {
  111. boolean repeated = false;
  112. for (HolonElement e : ((HolonObject) tempCps).getElements()) {
  113. if (elementName.getText().equals(e.getEleName())) {
  114. repeated = true;
  115. break;
  116. }
  117. }
  118. if (elementName.getText().length() != 0 && !repeated) {
  119. try {
  120. float energy = Float.parseFloat(providedEnergy.getText().toString());
  121. int elementAmount = Integer.parseInt(amount.getText().toString());
  122. hl = new HolonElement(elementName.getText().toString(), elementAmount, energy);
  123. dispose();
  124. } catch (NumberFormatException e) {
  125. JOptionPane.showMessageDialog(new JFrame(),
  126. "Please enter numbers in the Fields amount and providedEnergy");
  127. }
  128. } else {
  129. // JOptionPane.showMessageDialog(new JFrame(),
  130. // "Please enter a Name");
  131. if (elementName.getText().length() == 0) {
  132. JLabel errorString = new JLabel("No name");
  133. errorString.setBounds(200, 8, 200, 20);
  134. contentPanel.add(errorString);
  135. } else if (repeated) {
  136. JLabel errorString = new JLabel("Name already given");
  137. errorString.setBounds(240, 8, 100, 20);
  138. contentPanel.add(errorString);
  139. }
  140. elementName.setBackground(new Color(255, 50, 50));
  141. }
  142. }
  143. });
  144. okButton.setActionCommand("OK");
  145. buttonPane.add(okButton);
  146. getRootPane().setDefaultButton(okButton);
  147. }
  148. {
  149. JButton cancelButton = new JButton("Cancel");
  150. cancelButton.setActionCommand("Cancel");
  151. buttonPane.add(cancelButton);
  152. cancelButton.addActionListener(new ActionListener() {
  153. public void actionPerformed(ActionEvent e) {
  154. dispose();
  155. }
  156. });
  157. }
  158. }
  159. }
  160. /**
  161. * Sets the actual Cps.
  162. *
  163. * @param cps actual Cps
  164. */
  165. public void setActualCps(AbstractCpsObject cps) {
  166. this.tempCps = cps;
  167. }
  168. /**
  169. * Returns the created Element.
  170. *
  171. * @return the Element
  172. */
  173. public HolonElement getElement() {
  174. return hl;
  175. }
  176. }