123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package ui.view;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.Image;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextField;
- import javax.swing.ImageIcon;
- import classes.AbstractCpsObject;
- import classes.HolonElement;
- import classes.HolonObject;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.ActionEvent;
- /**
- * popup for adding an Holon Element to a holon Object.
- *
- * @author Gruppe14
- */
- public class AddElementPopUp extends JDialog {
- /**
- * Serial.
- */
- private static final long serialVersionUID = 1L;
- private final JPanel contentPanel = new JPanel();
- private JTextField elementName;
- private JTextField providedEnergy;
- private JTextField amount;
- private HolonElement hl;
- private AbstractCpsObject tempCps;
- /**
- * Launch the application.
- *
- * @param args
- * standard
- */
- public static void main(String[] args) {
- try {
- AddElementPopUp dialog = new AddElementPopUp();
- dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- dialog.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Create the dialog.
- */
- public AddElementPopUp() {
- super((java.awt.Frame) null, true);
- this.setIconImage(new ImageIcon(this.getClass().getResource("/Images/Dummy_House.png")).getImage()
- .getScaledInstance(30, 30, Image.SCALE_SMOOTH));
- setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
- setBounds(100, 100, 400, 190);
- getContentPane().setLayout(new BorderLayout());
- contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
- getContentPane().add(contentPanel, BorderLayout.CENTER);
- contentPanel.setLayout(null);
- this.setTitle(Languages.getLanguage()[64]);
- JLabel lblElementName = new JLabel(Languages.getLanguage()[65]);
- lblElementName.setBounds(10, 11, 100, 14);
- contentPanel.add(lblElementName);
- JLabel lblProvidedEnergy = new JLabel(Languages.getLanguage()[66]);
- lblProvidedEnergy.setBounds(10, 49, 120, 14);
- contentPanel.add(lblProvidedEnergy);
- JLabel lblAmount = new JLabel(Languages.getLanguage()[67]);
- lblAmount.setBounds(10, 84, 100, 14);
- contentPanel.add(lblAmount);
- elementName = new JTextField();
- elementName.addKeyListener(new KeyListener() {
- @Override
- public void keyPressed(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent e) {
- elementName.setBackground(Color.WHITE);
- }
- });
- elementName.setBounds(130, 8, 110, 20);
- contentPanel.add(elementName);
- elementName.setColumns(10);
- providedEnergy = new JTextField();
- providedEnergy.setBounds(130, 46, 110, 20);
- contentPanel.add(providedEnergy);
- providedEnergy.setColumns(10);
- providedEnergy.setText("0");
- amount = new JTextField();
- amount.setBounds(130, 81, 110, 20);
- contentPanel.add(amount);
- amount.setColumns(10);
- amount.setText("1");
- {
- JPanel buttonPane = new JPanel();
- buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
- getContentPane().add(buttonPane, BorderLayout.SOUTH);
- {
- JButton okButton = new JButton("OK");
- okButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0) {
- boolean repeated = false;
- for (HolonElement e : ((HolonObject) tempCps).getElements()) {
- if (elementName.getText().equals(e.getEleName())) {
- repeated = true;
- break;
- }
- }
- if (elementName.getText().length() != 0 && !repeated) {
- try {
- float energy = Float.parseFloat(providedEnergy.getText().toString());
- int elementAmount = Integer.parseInt(amount.getText().toString());
- hl = new HolonElement(elementName.getText().toString(), elementAmount, energy);
- dispose();
- } catch (NumberFormatException e) {
- JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[68]);
- }
- } else {
- // JOptionPane.showMessageDialog(new JFrame(),
- // "Please enter a Name");
- if (elementName.getText().length() == 0) {
- JLabel errorString = new JLabel(Languages.getLanguage()[69]);
- errorString.setBounds(240, 8, 100, 20);
- contentPanel.add(errorString);
- } else if (repeated) {
- JLabel errorString = new JLabel(Languages.getLanguage()[70]);
- errorString.setBounds(250, 8, 100, 20);
- contentPanel.add(errorString);
- }
- elementName.setBackground(new Color(255, 50, 50));
- }
- }
- });
- okButton.setActionCommand("OK");
- buttonPane.add(okButton);
- getRootPane().setDefaultButton(okButton);
- }
- {
- JButton cancelButton = new JButton(Languages.getLanguage()[71]);
- cancelButton.setActionCommand("Cancel");
- buttonPane.add(cancelButton);
- cancelButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- dispose();
- }
- });
- }
- }
- }
- /**
- * Sets the actual Cps.
- *
- * @param cps
- * actual Cps
- */
- public void setActualCps(AbstractCpsObject cps) {
- this.tempCps = cps;
- }
- /**
- * Returns the created Element.
- *
- * @return the Element
- */
- public HolonElement getElement() {
- return hl;
- }
- }
|