Browse Source

HolonBattery Basic Gui and Object no Function

tolatesry 6 years ago
parent
commit
298ea60c5d

+ 0 - 12
.settings/org.eclipse.jdt.core.prefs

@@ -1,12 +0,0 @@
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
-org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
-org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
-org.eclipse.jdt.core.compiler.compliance=1.8
-org.eclipse.jdt.core.compiler.debug.lineNumber=generate
-org.eclipse.jdt.core.compiler.debug.localVariable=generate
-org.eclipse.jdt.core.compiler.debug.sourceFile=generate
-org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
-org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
-org.eclipse.jdt.core.compiler.source=1.8

BIN
res/Images/battery.png


+ 74 - 0
src/classes/HolonBattery.java

@@ -0,0 +1,74 @@
+package classes;
+
+public class HolonBattery extends AbstractCpsObject{
+	
+	private float inRatio;
+	private float outRatio;
+	private float capasity;
+	private float stateOfCharge;
+	
+	
+	/** Constructor for a unique ID.
+	 * @param ObjName
+	 */
+	public HolonBattery(String ObjName)
+	{
+		super(ObjName);
+		inRatio = 0;
+		outRatio = 0;
+		capasity = 0;
+		stateOfCharge = 0;
+	}
+	/** Constructor to Copy a Battery
+	 * @param obj Object to copy.
+	 */
+	public HolonBattery(AbstractCpsObject obj)
+	{
+		super(obj);
+		
+		
+	}
+	/**
+	 * TestFunction for functionality
+	 */
+	public void HelloWorld()
+	{
+		System.out.println("JO");
+	}
+	public float getStateOfCharge() {
+		return stateOfCharge;
+	}
+	public void setStateOfCharge(float stateOfCharge) {
+		if(stateOfCharge > capasity) //state of Charege can not more than the capacity
+		{
+			stateOfCharge = capasity;
+		}
+		this.stateOfCharge = stateOfCharge;
+	}
+	public float getCapasity() {
+		return capasity;
+	}
+	public void setCapasity(float capasity) {
+		this.capasity = capasity;
+	}
+	public float getOutRatio() {
+		return outRatio;
+	}
+	public void setOutRatio(float outRatio) {
+		if(outRatio < 0) //
+		{
+			outRatio = 0;
+		}
+		this.outRatio = outRatio;
+	}
+	public float getInRatio() {
+		return inRatio;
+	}
+	public void setInRatio(float inRatio) {
+		if(inRatio < 0)
+		{
+			inRatio = 0;
+		}
+		this.inRatio = inRatio;
+	}
+}

+ 18 - 4
src/ui/controller/CategoryController.java

@@ -3,6 +3,7 @@ package ui.controller;
 import java.util.ArrayList;
 
 import classes.Category;
+import classes.HolonBattery;
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
@@ -42,12 +43,11 @@ public class CategoryController {
 		addNewCategory("Energy");
 		addNewCategory("Building");
 		addNewCategory("Component");
-
 		addNewHolonObject(mpC.searchCat("Energy"), "Power Plant", new ArrayList<HolonElement>(),
 				"/Images/power-plant.png");
 		addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
 		addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
-
+		addNewHolonBattery(mpC.searchCat("Component"), "Battery", "/Images/battery.png");
 	}
 
 	/**
@@ -182,12 +182,26 @@ public class CategoryController {
 	 */
 	public void addNewHolonSwitch(Category cat, String objName, String image) {
 		HolonSwitch holonSwitch = new HolonSwitch(objName);
-
+		
 		holonSwitch.setImage(image);
 		holonSwitch.setSav(cat.getName());
 		addObject(cat, holonSwitch);
 	}
-
+	/**
+	 * Add new Holon Battery
+	 * @param cat 
+	 * 			Categorx
+	 * @param objName
+	 * 			New Object Name
+	 * @param image
+	 * 			The Image Path
+	 */
+	public void addNewHolonBattery(Category cat, String objName, String image) {
+		HolonBattery holonBat = new HolonBattery(objName);
+		holonBat.setImage(image);
+		holonBat.setSav(cat.getName());
+		addObject(cat, holonBat);
+	}
 	/**
 	 * Removes an Object from a Category.
 	 * @param category Category

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

@@ -225,6 +225,17 @@ public class Control {
         categoryController.addNewHolonSwitch(cat, obj, "/Images/switch-on.png");
         saveCategory();
     }
+    
+    /**
+     * Add new HolonBattery to a Category
+     * @param cat
+     * @param obj
+     * @throws IOException
+     */
+    public void addBattery(Category cat, String obj) throws IOException {
+        categoryController.addNewHolonBattery(cat, obj, "/Images/battery.png");
+        saveCategory();
+    }
 
     /**
      * delete a given Category.

+ 134 - 17
src/ui/view/AddObjectPopUp.java

@@ -1,11 +1,12 @@
 package ui.view;
 
 import classes.AbstractCpsObject;
+import classes.HolonBattery;
 import classes.HolonElement;
 import classes.HolonObject;
 import classes.Pair;
 import ui.controller.Control;
-
+import java.lang.NumberFormatException;
 import javax.swing.*;
 import javax.swing.border.EmptyBorder;
 import javax.swing.filechooser.FileNameExtensionFilter;
@@ -42,21 +43,21 @@ public class AddObjectPopUp extends JDialog {
 	private boolean editState;
 	private boolean imageChanged = false;
 
-//	/**
-//	 * Launch the application.
-//	 *
-//	 * @param args
-//	 *            standard
-//	 */
-//	public static void main(String[] args) {
-//		try {
-//			AddObjectPopUp dialog = new AddObjectPopUp(false, null, null);
-//			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
-//			dialog.setVisible(true);
-//		} catch (Exception e) {
-//			e.printStackTrace();
-//		}
-//	}
+	/**
+	 * Launch the application.
+	 *
+	 * @param args
+	 *            standard
+	 */
+	public static void main(String[] args) {
+		try {
+			AddObjectPopUp dialog = new AddObjectPopUp(false,new HolonBattery("jo") , "hei", null);
+			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
+			dialog.setVisible(true);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
 
 	/**
 	 * Create the dialog.
@@ -69,6 +70,11 @@ public class AddObjectPopUp extends JDialog {
 	 *            the categorie
 	 */
     AddObjectPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
+    	if(obj  instanceof HolonBattery)
+    	{
+    		BatteryPopUp(edit, obj, cat, parentFrame);
+    		return;
+    	}
         toEdit = obj;
 		editState = edit;
 		this.setIconImage(Util.loadImage(this, "/Images/Dummy_House.png",30,30));
@@ -275,7 +281,118 @@ public class AddObjectPopUp extends JDialog {
 			}
 		}
 	}
-
+    protected void BatteryPopUp(boolean edit, AbstractCpsObject obj, String cat, JFrame parentFrame) {
+    	//TODO: Click mich <3 
+    	HolonBattery editBat = (HolonBattery) obj;
+    	//Window Settings
+    	this.setIconImage(Util.loadImage(this, "/Images/battery.png",30,30));
+    	this.setTitle("Edit Battery");
+    	setBounds(0, 0, 285, 290);
+        setLocationRelativeTo(parentFrame);
+		
+        //Labels, TextFiels, Buttons
+        JPanel myPanel = new JPanel();
+        myPanel.setLayout(null);
+        int beginBoxX = 140, beginLabelX = 30;
+        int beginColumAtY = 20, newColumY = 40;
+        JLabel batteryNameLabel = new JLabel("Name:");
+        JTextField batteryNameBox = new JTextField(10);
+        batteryNameBox.setText(editBat.getName());
+        JLabel batteryInRateLabel = new JLabel("In ratio:");
+        JTextField batteryInRateBox = new JTextField(10);
+        batteryInRateBox.setText(Float.toString(editBat.getInRatio()));
+        JLabel batteryOutRateLabel = new JLabel("Out ratio:");
+        JTextField batteryOutRateBox = new JTextField(10);
+        batteryOutRateBox.setText(Float.toString(editBat.getOutRatio()));
+        JLabel batteryCapasityLabel = new JLabel("Capasity:");
+        JTextField batteryCapasityBox = new JTextField(10);
+        batteryCapasityBox.setText(Float.toString(editBat.getCapasity()));
+        JLabel batterySOCLabel = new JLabel("State of charge:"); 
+        JTextField batterySOCBox = new JTextField(10);
+        batterySOCBox.setText(Float.toString(editBat.getStateOfCharge()));
+        
+    
+        batteryNameLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
+        batteryNameBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
+        myPanel.add(batteryNameLabel);
+        myPanel.add(batteryNameBox);
+        beginColumAtY += newColumY;      
+    
+        batteryInRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
+        batteryInRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
+        myPanel.add(batteryInRateLabel);
+        myPanel.add(batteryInRateBox);
+        beginColumAtY += newColumY;
+		
+        batteryOutRateLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
+        batteryOutRateBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
+        myPanel.add(batteryOutRateLabel);
+        myPanel.add(batteryOutRateBox);
+        beginColumAtY += newColumY;
+        
+        batteryCapasityLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
+        batteryCapasityBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
+        myPanel.add(batteryCapasityLabel);
+        myPanel.add(batteryCapasityBox);
+        beginColumAtY += newColumY;
+        
+        batterySOCLabel.setBounds(beginLabelX, beginColumAtY, 200, 20);
+        batterySOCBox.setBounds(beginBoxX, beginColumAtY, 100, 20);
+        myPanel.add(batterySOCLabel);
+        myPanel.add(batterySOCBox);
+        
+        //Save , Cancel
+        JPanel bottomPanel = new JPanel();
+        bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
+        
+        JButton saveButton = new JButton("Save");
+        bottomPanel.add(saveButton);
+        
+        saveButton.addActionListener(e -> {
+        	//SaveButten Event:
+        	if(batteryNameBox.getText().isEmpty()){ //Check for valid name
+        		JOptionPane.showMessageDialog(getContentPane(), "Name is empty." , "Wrong input" , JOptionPane.WARNING_MESSAGE);
+        	}else{
+        		editBat.setName(batteryNameBox.getText());
+        		try{//Check for floats inputs
+        			float changedInRatio = Float.valueOf(batteryInRateBox.getText());
+        			float changedOutRatio = Float.valueOf(batteryOutRateBox.getText());
+        			float changedCapasity = Float.valueOf(batteryCapasityBox.getText());
+        			float changedSOC = Float.valueOf(batterySOCBox.getText());
+        			//Saving:
+        			editBat.setInRatio(changedInRatio);
+        			editBat.setOutRatio(changedOutRatio);
+        			editBat.setCapasity(changedCapasity);
+        			editBat.setStateOfCharge(changedSOC);
+					try {
+						controller.saveCategory();
+						controller.notifyAll();
+					} catch (Exception e1) { //Controller Exceptions
+					}
+        			dispose();
+        		}catch(NumberFormatException FloatException){
+        			String wrong= FloatException.getMessage();
+        			String message;
+        			if(wrong.startsWith("empty")){
+        				message = "A field is empty.";
+        			}else{
+        				message = wrong.substring(18) + " is not a valid Number.";
+        			}
+        			JOptionPane.showMessageDialog(getContentPane(), message, "Wrong input" , JOptionPane.WARNING_MESSAGE);
+        		}
+        	}
+        });
+        
+        JButton cancelButton = new JButton("Cancel");
+        bottomPanel.add(cancelButton);
+		cancelButton.addActionListener(e -> dispose());
+		
+		
+		
+        getContentPane().setLayout(new BorderLayout());
+		getContentPane().add(myPanel, BorderLayout.CENTER);
+		getContentPane().add(bottomPanel, BorderLayout.SOUTH);
+    }
 //	/**
 //	 * Get Jar Containing Folder.
 //	 *

+ 4 - 1
src/ui/view/GUI.java

@@ -197,7 +197,7 @@ public class GUI implements CategoryListener {
 	private final String[] columnNamesSingle = { "Nr.", "Device", "Energy",
 			"Flexible Energy Available", "Quantity", "Activated", "Flexible" };
 	private final ArrayList<PropertyTable> tables = new ArrayList<>();
-	private final String[] comboBoxCat = { "Category", "Object", "Switch" };
+	private final String[] comboBoxCat = { "Category", "Object", "Switch", "Battery" };
 	private final UpdateController updCon;
 	private final JSplitPane splitPane_1 = new JSplitPane();
 	private final JSlider holonBodySizeSlider = new JSlider();
@@ -2287,6 +2287,9 @@ public class GUI implements CategoryListener {
 						case "Switch":
 							controller.addSwitch(cat, objname);
 							break;
+						case "Battery":
+							controller.addBattery(cat, objname);
+							break;
 						}
 					} catch (Exception e) {
 					}

+ 2 - 2
src/ui/view/Util.java

@@ -53,7 +53,7 @@ public class Util {
 		try {
 			return ImageIO.read(loadStream(origin, url));
 		} catch (IOException e) {
-			System.err.println("Warning: '" + url + "' loading failed!");
+			//System.err.println("Warning: '" + url + "' loading failed!");
 			e.printStackTrace();
 			return defaultImage;
 		}
@@ -63,7 +63,7 @@ public class Util {
 		InputStream o=origin.getClass().getResourceAsStream(url);
 		if(o!=null)return o;
 		else{
-			System.out.println("Loading of \""+url+"\" as local resource failed.\nWill attempt to load as File.");
+			//System.out.println("Loading of \""+url+"\" as local resource failed.\nWill attempt to load as File.");
 			boolean rootSymbol=false;	//Whether url starts with a / or \
 			switch(url.charAt(0)){		//So we can make sure to construct res/path correctly.
 			case '/':case '\\':rootSymbol=true;