AddElementPopUp.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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
  42. * standard
  43. */
  44. public static void main(String[] args) {
  45. try {
  46. AddElementPopUp dialog = new AddElementPopUp();
  47. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  48. dialog.setVisible(true);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. /**
  54. * Create the dialog.
  55. */
  56. public AddElementPopUp() {
  57. super((java.awt.Frame) null, true);
  58. this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
  59. .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
  60. setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
  61. setBounds(100, 100, 400, 190);
  62. getContentPane().setLayout(new BorderLayout());
  63. contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
  64. getContentPane().add(contentPanel, BorderLayout.CENTER);
  65. contentPanel.setLayout(null);
  66. this.setTitle(Languages.getLanguage()[64]);
  67. JLabel lblElementName = new JLabel(Languages.getLanguage()[65]);
  68. lblElementName.setBounds(10, 11, 100, 14);
  69. contentPanel.add(lblElementName);
  70. JLabel lblProvidedEnergy = new JLabel(Languages.getLanguage()[66]);
  71. lblProvidedEnergy.setBounds(10, 49, 120, 14);
  72. contentPanel.add(lblProvidedEnergy);
  73. JLabel lblAmount = new JLabel(Languages.getLanguage()[67]);
  74. lblAmount.setBounds(10, 84, 100, 14);
  75. contentPanel.add(lblAmount);
  76. elementName = new JTextField();
  77. elementName.addKeyListener(new KeyListener() {
  78. @Override
  79. public void keyPressed(KeyEvent arg0) {
  80. // TODO Auto-generated method stub
  81. }
  82. @Override
  83. public void keyReleased(KeyEvent e) {
  84. // TODO Auto-generated method stub
  85. }
  86. @Override
  87. public void keyTyped(KeyEvent e) {
  88. elementName.setBackground(Color.WHITE);
  89. }
  90. });
  91. elementName.setBounds(130, 8, 110, 20);
  92. contentPanel.add(elementName);
  93. elementName.setColumns(10);
  94. providedEnergy = new JTextField();
  95. providedEnergy.setBounds(130, 46, 110, 20);
  96. contentPanel.add(providedEnergy);
  97. providedEnergy.setColumns(10);
  98. providedEnergy.setText("0");
  99. amount = new JTextField();
  100. amount.setBounds(130, 81, 110, 20);
  101. contentPanel.add(amount);
  102. amount.setColumns(10);
  103. amount.setText("1");
  104. {
  105. JPanel buttonPane = new JPanel();
  106. buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
  107. getContentPane().add(buttonPane, BorderLayout.SOUTH);
  108. {
  109. JButton okButton = new JButton("OK");
  110. okButton.addActionListener(new ActionListener() {
  111. public void actionPerformed(ActionEvent arg0) {
  112. boolean repeated = false;
  113. for (HolonElement e : ((HolonObject) tempCps).getElements()) {
  114. if (elementName.getText().equals(e.getEleName())) {
  115. repeated = true;
  116. break;
  117. }
  118. }
  119. if (elementName.getText().length() != 0 && !repeated) {
  120. try {
  121. float energy = Float.parseFloat(providedEnergy.getText().toString());
  122. int elementAmount = Integer.parseInt(amount.getText().toString());
  123. hl = new HolonElement(elementName.getText().toString(), elementAmount, energy);
  124. dispose();
  125. } catch (NumberFormatException e) {
  126. JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[68]);
  127. }
  128. } else {
  129. // JOptionPane.showMessageDialog(new JFrame(),
  130. // "Please enter a Name");
  131. if (elementName.getText().length() == 0) {
  132. JLabel errorString = new JLabel(Languages.getLanguage()[69]);
  133. errorString.setBounds(240, 8, 100, 20);
  134. contentPanel.add(errorString);
  135. } else if (repeated) {
  136. JLabel errorString = new JLabel(Languages.getLanguage()[70]);
  137. errorString.setBounds(250, 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(Languages.getLanguage()[71]);
  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
  164. * actual Cps
  165. */
  166. public void setActualCps(AbstractCpsObject cps) {
  167. this.tempCps = cps;
  168. }
  169. /**
  170. * Returns the created Element.
  171. *
  172. * @return the Element
  173. */
  174. public HolonElement getElement() {
  175. return hl;
  176. }
  177. }