فهرست منبع

adding of storage elements now possible form within the gui

David Heck 5 سال پیش
والد
کامیت
e771df92ff

+ 1 - 1
src/blackstart/controlAlgorithm.java

@@ -439,7 +439,7 @@ public class controlAlgorithm implements AddOn {
 			}
 		} else { // at leaf
 //			println("Distance to " + currentObject.getId() + ": " + distance);
-//			((HolonObject) currentObject).addElement(new StorageElement("Storage", 1, 0, control.getModel()));
+//			((HolonObject) currentObject).addElement(new StorageElement("Storage", 1, 0, control.getModel(), 10000, 5000 ,5000));
 			for (HolonElement ele : ((HolonObject) currentObject).getElements()) {
 				ele.setDistance(distance);
 				if (ele.getEleName() == "Solar Panels") {// TODO: das wollen wir ja so nicht

+ 7 - 15
src/classes/StorageElement.java

@@ -22,12 +22,12 @@ public class StorageElement extends HolonElement {
 	@Expose
 	private float maxOutRatio;
 
-	public StorageElement(String eleName, int amount, float energy, Model model) {
+	public StorageElement(String eleName, int amount, float energy, Model model, float capacity, float maxInRatio, float maxOutRatio) {
 		super(eleName, amount, energy, model);
-		this.stateOfCharge = 4000;
-		this.maxInRatio = 5000;
-		this.maxOutRatio = 5000;
-		this.capacity = 10000 * 60;// for example tesla power wall 10kwh, 10kw per hour thus * 60 if iteration =
+		this.stateOfCharge = 0;
+		this.maxInRatio = maxInRatio;
+		this.maxOutRatio = maxOutRatio;
+		this.capacity = capacity * 60;// for example tesla power wall 10kwh, 10kw per hour thus * 60 if iteration =
 									// minutes//TODO:!
 		this.status = Mode.STANDBY;
 	}
@@ -166,19 +166,11 @@ public class StorageElement extends HolonElement {
 	}
 
 	public boolean chargeDepleted(){
-		if(stateOfCharge <= 0){
-			return true;
-		}else{
-			return false;
-		}
+		return stateOfCharge <= 0;
 	}
 
 	public boolean fullyCharged(){
-		if(stateOfCharge >= capacity){
-			return true;
-		}else{
-			return false;
-		}
+		return stateOfCharge >= capacity;
 	}
 
 	public enum Mode {

+ 9 - 0
src/ui/controller/Control.java

@@ -413,6 +413,15 @@ public class Control {
         }
     }
 
+    public void addStorageElementCansvasObject(int objectId, StorageElement ele){
+        objectController.addNewStorageElementIntoCanvasObject(objectId, ele);
+        try {
+            autoSave();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
     /**
      * Add a new Element into a Object in Category.
      *

+ 8 - 0
src/ui/controller/ObjectController.java

@@ -79,6 +79,14 @@ public class ObjectController {
         }
     }
 
+    public void addNewStorageElementIntoCanvasObject(int objectId , StorageElement ele){
+        if (mpC.searchByID(objectId) == null) {
+            addElementIntoCanvasObject((HolonObject) model.getSelectedCpsObjects().get(0), ele);
+        } else {
+            addElementIntoCanvasObject((HolonObject) mpC.searchByID(objectId), ele);
+        }
+    }
+
     /**
      * Add Element into a Object in Category.
      *

+ 239 - 0
src/ui/view/AddStorageElementPopUp.java

@@ -0,0 +1,239 @@
+package ui.view;
+
+import classes.AbstractCpsObject;
+import classes.HolonElement;
+import classes.HolonObject;
+import classes.StorageElement;
+import ui.model.Model;
+
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+
+import java.awt.*;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+
+/**
+ * popup for adding an Holon Element to a holon Object.
+ *
+ * @author Gruppe14
+ * @author improved by A.T.M.-B. (Gruppe007)
+ */
+public class AddStorageElementPopUp extends JDialog {
+
+    /**
+     * Serial.
+     */
+    private static final long serialVersionUID = 1L;
+    /* Data */
+    /** Holon Object the Element should be added to */
+    private AbstractCpsObject tempCps;
+    /** Holon Element that should be edited (if in edit Modus */
+    private StorageElement 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 capacity;
+    /** Element is active if checked */
+    JCheckBox checkBoxActive;
+    /** Textfield to enter the amount of this Element */
+    private JTextField amount;
+    /** Textfield to enter the flexible Energy of the Element */
+    private JTextField maxInRatio;
+    private JTextField maxOutRatio;
+    /** Flexible if checkbox is checked */
+    JCheckBox checkboxFlexible;
+    /** Model which is used */
+    private Model model;
+
+    /**
+     * Create the AddElementPopup Dialog
+     * @param parentFrame
+     */
+    AddStorageElementPopUp(JFrame parentFrame, Model model) {
+        super((java.awt.Frame) null, true);
+        this.model = model;
+        this.setIconImage(Util.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(Languages.getLanguage()[64]);
+
+
+        /* 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(Languages.getLanguage()[65]);
+        lblElementName.setBounds(10, 10, 100, 20);
+        contentPanel.add(lblElementName);
+        elementName.setBounds(130, 10, 110, 20);
+        contentPanel.add(elementName);
+        elementName.setColumns(10);
+        elementName.setText("Storage");
+
+        /* Add Provided Energy Label and Textfield */
+        JLabel lblCapacity = new JLabel("Capacity in kWh");
+        lblCapacity.setBounds(10, 50, 120, 20);
+        contentPanel.add(lblCapacity);
+
+        capacity = new JTextField();
+        capacity.setBounds(130, 50, 110, 20);
+        contentPanel.add(capacity);
+        capacity.setColumns(10);
+        capacity.setText("10000");
+
+        checkBoxActive = new JCheckBox("Active");
+        checkBoxActive.setSelected(true);
+        checkBoxActive.setBounds(250, 50, 115, 20);
+        contentPanel.add(checkBoxActive);
+
+        /* Add Flexible Energy Textfield and CheckBox */
+        JLabel lblinRatio = new JLabel("maxInRatio in kW");
+        lblinRatio.setBounds(10, 90, 100, 20);
+        contentPanel.add(lblinRatio);
+
+        maxInRatio = new JTextField();
+        maxInRatio.setText("5000");
+        maxInRatio.setColumns(10);
+        maxInRatio.setBounds(130, 90, 110, 20);
+        contentPanel.add(maxInRatio);
+
+        JLabel lbloutRatio = new JLabel("maxInRatio in kW");
+        lbloutRatio.setBounds(10, 130, 100, 20);
+        contentPanel.add(lbloutRatio);
+
+        maxOutRatio = new JTextField();
+        maxOutRatio.setText("5000");
+        maxOutRatio.setColumns(10);
+        maxOutRatio.setBounds(130, 130, 110, 20);
+        contentPanel.add(maxOutRatio);
+
+//        checkboxFlexible = new JCheckBox("Flexible");
+//        checkboxFlexible.setBounds(250, 90, 115, 20);
+//        contentPanel.add(checkboxFlexible);
+
+        /* Add Amount Textfield and Checkbox */
+        JLabel lblAmount = new JLabel(Languages.getLanguage()[67]);
+        lblAmount.setBounds(10, 170, 100, 14);
+        contentPanel.add(lblAmount);
+
+        amount = new JTextField();
+        amount.setBounds(130, 170, 110, 20);
+        contentPanel.add(amount);
+        amount.setColumns(10);
+        amount.setText("1");
+
+        /* 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(Languages.getLanguage()[71]);
+        cancelButton.setActionCommand("Cancel");
+        buttonPane.add(cancelButton);
+        cancelButton.addActionListener(e -> dispose());
+    }
+
+    /**
+     * Sets the actual Cps.
+     *
+     * @param cps
+     *            actual Cps
+     */
+    void setActualCps(AbstractCpsObject cps) {
+        this.tempCps = cps;
+    }
+
+    /**
+     * Returns the created Element.
+     *
+     * @return the Element
+     */
+    public StorageElement getElement() {
+        return hl;
+    }
+
+    /**
+     * setElement that should be edited
+     * @param storageElement
+     */
+    public void setElement(StorageElement storageElement) {
+        hl = storageElement;
+        elementName.setText(hl.getEleName());
+        amount.setText(""+hl.getAmount());
+        capacity.setText(""+hl.getEnergyPerElement());
+        checkBoxActive.setSelected(hl.isActive());
+    }
+
+    /**
+     * Trys to create/edit the Element
+     */
+    private void okAction() {
+        boolean repeated = false;
+        for (HolonElement e : ((HolonObject) 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 elementCapacity = Float.parseFloat(capacity.getText());
+                float maxIn = Float.parseFloat(maxInRatio.getText());
+                float maxOut = Float.parseFloat(maxOutRatio.getText());
+                int elementAmount = Integer.parseInt(amount.getText());
+                if(hl == null){
+                    hl = new StorageElement(elementName.getText(), elementAmount, 0, model, elementCapacity, maxIn, maxOut);
+                } else {
+                    hl.setEleName(elementName.getText());
+                    hl.setAmount(elementAmount);
+                    hl.setEnergyPerElement(0);
+                    hl.setStatusAndSetEnergy(StorageElement.Mode.STANDBY, 0);
+                }
+                hl.setActive(checkBoxActive.isSelected());
+                dispose();
+            } catch (NumberFormatException e) {
+                JOptionPane.showMessageDialog(new JFrame(), Languages.getLanguage()[68]);
+            }
+        } else {
+            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));
+        }
+    }
+}

+ 30 - 2
src/ui/view/GUI.java

@@ -212,7 +212,7 @@ public class GUI implements CategoryListener {
 	private final JPanel panel = new JPanel();
 	private final JPanel panelHolonEl = new JPanel();
 	// Buttons
-
+
 	private final JButton btnAdd = new JButton("New");
 	private final JPopupMenu btnAddPopUp = new JPopupMenu("New");
 	private final JMenuItem mItemNew = new JMenuItem("New..");
@@ -222,9 +222,10 @@ public class GUI implements CategoryListener {
 	private final JMenuItem mItemBattery = new JMenuItem("Battery");
 	private final JButton btnDel = new JButton("Delete");
 	private final JButton btnAddHolEL = new JButton("New Element");
+	private final JButton btnAddStorageEL = new JButton("New Storage Element");
 	private final JButton btnDelHolEL = new JButton("Delete Element");
 	private final JButton resetGraphBtn = new JButton("Reset");
-
+
 	private final JToolBar toolBar = new JToolBar();
 	private final JToolBar toolBarHolonEl = new JToolBar();
 	private final JToolBar toolBarGraph = new JToolBar();
@@ -277,6 +278,7 @@ public class GUI implements CategoryListener {
 	private AddObjectPopUp addObjectPopUP;
 	private AboutUsPopUp aboutUsPopUp;
 	private AddElementPopUp addElementPopUp;
+	private AddStorageElementPopUp addStorageElementPopUp;
 	// variables
 	private boolean dragging = false;
 	private String actualObjectClicked;
@@ -1120,6 +1122,7 @@ public class GUI implements CategoryListener {
 		scrollElements.add(tableHolonElementScrollPane);
 		panelHolonEl.setLayout(new BoxLayout(panelHolonEl, BoxLayout.X_AXIS));
 		toolBarHolonEl.add(btnAddHolEL);
+		toolBarHolonEl.add(btnAddStorageEL);
 		toolBarHolonEl.add(btnDelHolEL);
 		toolBarHolonEl.setFloatable(false);
 		panelHolonEl.add(toolBarHolonEl);
@@ -1219,6 +1222,31 @@ public class GUI implements CategoryListener {
 				JOptionPane.showMessageDialog(contentPane, "No object selected.\nPlease select a object first." , "Message" , JOptionPane.INFORMATION_MESSAGE);
 			}
 		});
+
+
+		btnAddStorageEL.addActionListener(actionEvent -> {
+			if (model.getSelectedCpsObjects().size() == 1) {
+				AbstractCpsObject tempCpsObject = updCon.getActualCps();
+				if (tempCpsObject != null
+						&& tempCpsObject.getClass() == HolonObject.class
+						&& tempCpsObject.getId() != 0) {
+					addStorageElementPopUp = new AddStorageElementPopUp(holegJFrame, model);
+					addStorageElementPopUp.setActualCps(updCon.getActualCps());
+					addStorageElementPopUp.setVisible(true);
+					StorageElement ele = addStorageElementPopUp.getElement();
+					if (ele != null) {
+						controller.addStorageElementCansvasObject(
+								tempCpsObject.getId(), ele);
+					}
+					controller.calculateStateAndVisualForTimeStep(model
+							.getCurIteration());
+					triggerUpdateController(null);
+					contentPane.updateUI();
+				}
+			}else {
+				JOptionPane.showMessageDialog(contentPane, "No object selected.\nPlease select a object first." , "Message" , JOptionPane.INFORMATION_MESSAGE);
+			}
+		});
 		/*
 		 * Delete the chosen HolonElement of the selected HolonObject,
 		 * Multi-Selection for CpsObjects as well as for HolonElements possible