123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- package ui.view;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JButton;
- import javax.swing.JCheckBox;
- import javax.swing.JDialog;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.border.EmptyBorder;
- import classes.HolonElement;
- import classes.HolonObject;
- import utility.ImageImport;
- /**
- * popup for adding an Holon Element to a holon Object.
- *
- * @author Gruppe14
- * @author improved by A.T.M.-B. (Gruppe007)
- */
- public class AddElementPopUp extends JDialog {
- /**
- * Serial.
- */
- private static final long serialVersionUID = 1L;
- /* Data */
- /** Holon Object the Element should be added to */
- private HolonObject tempCps;
- /** Holon Element that should be edited (if in edit Modus */
- private HolonElement hl;
-
- /* GUI */
- /** Panel containing everything */
- private final JPanel contentPanel = new JPanel();
- /** Textfield for entering a Name for the Element */
- private JTextField elementName;
- /** Textfield for the energy the Element consumes/produces */
- private JTextField providedEnergy;
- /** Element is active if checked */
- JCheckBox checkBoxActive;
- /**
- * Create the AddElementPopup Dialog
- * @param parentFrame
- */
- AddElementPopUp(JFrame parentFrame) {
- super((java.awt.Frame) null, true);
- this.setIconImage(ImageImport.loadImage("/Images/Holeg.png", 30, 30));
- setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
- setBounds(100, 100, 400, 245);
- setLocationRelativeTo(parentFrame);
- getContentPane().setLayout(new BorderLayout());
- contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
- getContentPane().add(contentPanel, BorderLayout.CENTER);
- contentPanel.setLayout(null);
- this.setTitle("Add Element to Object");
-
- /* Element Name Textfield and Label */
- elementName = new JTextField();
- elementName.addKeyListener(new KeyListener() {
- @Override
- public void keyPressed(KeyEvent arg0) {
- }
- @Override
- public void keyReleased(KeyEvent e) {
- }
- @Override
- public void keyTyped(KeyEvent e) {
- elementName.setBackground(Color.WHITE);
- }
- });
-
- JLabel lblElementName = new JLabel("Element Name:");
- lblElementName.setBounds(10, 10, 100, 20);
- contentPanel.add(lblElementName);
- elementName.setBounds(130, 10, 110, 20);
- contentPanel.add(elementName);
- elementName.setColumns(10);
-
- /* Add Provided Energy Label and Textfield */
- JLabel lblProvidedEnergy = new JLabel("Provided Energy:");
- lblProvidedEnergy.setBounds(10, 50, 120, 20);
- contentPanel.add(lblProvidedEnergy);
-
- providedEnergy = new JTextField();
- providedEnergy.setBounds(130, 50, 110, 20);
- contentPanel.add(providedEnergy);
- providedEnergy.setColumns(10);
- providedEnergy.setText("0");
-
- checkBoxActive = new JCheckBox("Active");
- checkBoxActive.setSelected(true);
- checkBoxActive.setBounds(250, 50, 115, 20);
- contentPanel.add(checkBoxActive);
- /* Add Buttons and Actions */
- JPanel buttonPane = new JPanel();
- buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
- getContentPane().add(buttonPane, BorderLayout.SOUTH);
- JButton okButton = new JButton("OK");
- okButton.addActionListener(arg0 -> okAction());
- okButton.setActionCommand("OK");
- buttonPane.add(okButton);
- getRootPane().setDefaultButton(okButton);
- JButton cancelButton = new JButton("Cancel");
- cancelButton.setActionCommand("Cancel");
- buttonPane.add(cancelButton);
- cancelButton.addActionListener(e -> dispose());
- }
- /**
- * Sets the actual Cps.
- *
- * @param cps
- * actual Cps
- */
- void setActualHolonObject(HolonObject cps) {
- this.tempCps = cps;
- }
- /**
- * Returns the created Element.
- *
- * @return the Element
- */
- public HolonElement getElement() {
- return hl;
- }
- /**
- * setElement that should be edited
- * @param holonElement
- */
- public void setElement(HolonElement holonElement) {
- hl = holonElement;
- elementName.setText(hl.getEleName());
- providedEnergy.setText(""+hl.getEnergy());
- checkBoxActive.setSelected(hl.isActive());
-
- }
-
- /**
- * Trys to create/edit the Element
- */
- private void okAction() {
- boolean repeated = false;
- for (HolonElement e : tempCps.getElements()) {
- if (elementName.getText().equals(e.getEleName())&&(hl == null || hl.getId()!=e.getId())) {
- repeated = true;
- break;
- }
- }
- if (elementName.getText().length() != 0 && !repeated) {
- try {
- float energy = Float.parseFloat(providedEnergy.getText());
- if(hl == null){
- hl = new HolonElement(this.tempCps, elementName.getText(), energy);
- } else {
- hl.setEleName(elementName.getText());
- hl.setEnergyPerElement(energy);
- hl.setActive(checkBoxActive.isSelected());
- }
- dispose();
- } catch (NumberFormatException e) {
- JOptionPane.showMessageDialog(new JFrame(), "Please enter numbers in the Fields amount and Energy");
- }
- } else {
- if (elementName.getText().length() == 0) {
- JLabel errorString = new JLabel("No name");
- errorString.setBounds(240, 8, 100, 20);
- contentPanel.add(errorString);
- } else if (repeated) {
- JLabel errorString = new JLabel("Name already given");
- errorString.setBounds(250, 8, 100, 20);
- contentPanel.add(errorString);
- }
- elementName.setBackground(new Color(255, 50, 50));
- }
- }
- }
|