Browse Source

Json Merge branch 'Ohne_Drag_and_Drop' of https://git.tk.informatik.tu-darmstadt.de/carlos.garcia/praktikum-holons into Ohne_Drag_and_Drop

# Conflicts:
#	src/ui/controller/LoadStoreController.java
Teh-Hai Julian Zheng 7 years ago
parent
commit
f9b23a89d3

+ 1 - 0
.classpath

@@ -5,5 +5,6 @@
 	<classpathentry excluding="src/|res/" kind="src" path=""/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+	<classpathentry kind="lib" path="jars/json-simple-1.1.1.jar"/>
 	<classpathentry kind="output" path="bin"/>
 </classpath>

BIN
jars/json-simple-1.1.1.jar


+ 1 - 0
src/classes/CpsNode.java

@@ -8,6 +8,7 @@ public class CpsNode extends CpsObject {
 		super(objName);
 		this.setImage("/Images/node.png");
 		this.setID(idCounter.nextId());
+		this.setStored("Canvas");
 	}
 
 }

+ 17 - 2
src/classes/CpsObject.java

@@ -20,6 +20,9 @@ public abstract class CpsObject implements ComparableObject {
 	/* Position with a X and Y value */
 	Position position;
 	/* Energy input and output of each object in the grid */
+	
+	String stored;
+	
 	float energyIn;
 	float energyOut;
 
@@ -29,8 +32,6 @@ public abstract class CpsObject implements ComparableObject {
 	public CpsObject(String objName) {
 		setObjName(objName);
 		setName(objName);
-		setConnections(new ArrayList<CpsEdge>());
-		setPosition(new Position());
 		setImage("/Images/Dummy_House.png");
 	}
 
@@ -122,6 +123,20 @@ public abstract class CpsObject implements ComparableObject {
 		return position;
 	}
 
+	/**
+	 * @return the stored
+	 */
+	public String getStored() {
+		return stored;
+	}
+
+	/**
+	 * @param stored the stored to set
+	 */
+	public void setStored(String stored) {
+		this.stored = stored;
+	}
+
 	/* Getter and Setters for the energy input and output */
 	public float getEnergyIn() {
 		return energyIn;

+ 16 - 0
src/classes/HolonElement.java

@@ -16,6 +16,8 @@ public class HolonElement {
 	String image;
 	/* +: for Consumers and -: Producers */
 	char sign;
+	
+	String stored;
 	/*
 	 * Energy at each point of the graph with 50 predefined points. At the
 	 * beginning, it starts with all values at energy
@@ -159,4 +161,18 @@ public class HolonElement {
 			this.sign = '+';
 	}
 
+	/**
+	 * @return the stored
+	 */
+	public String getStored() {
+		return stored;
+	}
+
+	/**
+	 * @param stored the stored to set
+	 */
+	public void setStored(String stored) {
+		this.stored = stored;
+	}
+
 }

+ 6 - 0
src/ui/controller/CategoryController.java

@@ -35,13 +35,19 @@ public class CategoryController {
 
 		HolonObject powerp = new HolonObject("Power Plant");
 		powerp.setImage("/Images/power-plant.png");
+		powerp.setStored("Energy");
+		
 		HolonObject house = new HolonObject("House");
 		house.setImage("/Images/home.png");
+		house.setStored("Building");
 
 		HolonTransformer transformer = new HolonTransformer("Transformer");
 		transformer.setImage("/Images/transformer.png");
+		transformer.setStored("Component");
+		
 		HolonSwitch sw = new HolonSwitch("Switch");
 		sw.setImage("/Images/switch-on.png");
+		sw.setStored("Component");
 
 		addObject(energy, powerp);
 		addObject(building, house);

+ 7 - 1
src/ui/controller/Control.java

@@ -2,6 +2,7 @@ package ui.controller;
 
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.LinkedList;
 
@@ -120,7 +121,12 @@ public class Control {
 	public void setScale(int s) {
 		globalController.setScale(s);
 	}
-
+	
+	
+	/* Operations for Loading and Storing */
+	public void writeFile() throws IOException{
+		loadStoreController.writeJSONFile();
+	}
 	////////// etc
 	public void initListener(CategoryListener catLis) {
 		categoryController.addCategoryListener(catLis);

+ 50 - 6
src/ui/controller/LoadStoreController.java

@@ -2,10 +2,18 @@ package ui.controller;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
+import java.util.ArrayList;
 
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+
+import com.sun.scenario.effect.impl.sw.java.JSWBlend_COLOR_BURNPeer;
+
+import classes.Category;
+import classes.CpsObject;
 import ui.model.Model;
 
 public class LoadStoreController {
@@ -16,7 +24,47 @@ public class LoadStoreController {
 		this.MODEL = model;
 	}
 
-	public void readFromTextFile(File textFile) throws IOException {
+	public void writeJSONFile() throws IOException {
+
+		JSONObject jsonObj = new JSONObject();
+
+		writeCategory(jsonObj);
+
+		FileWriter file = new FileWriter("//Users//zheng//Desktop//Tesst.json");
+		file.write(jsonObj.toJSONString());
+
+		file.flush();
+		file.close();
+
+	}
+
+	public void writeCategory(JSONObject jsonObj) throws IOException {
+		
+		int objI = 1;
+
+		JSONArray arr = new JSONArray();
+		for (Category cat : MODEL.getCategories()) {
+			arr.add(cat.getName());
+			writeObjects(jsonObj, cat.getObjects(), objI);
+		}
+
+		jsonObj.put("Category", arr);
+
+	}
+
+	public void writeObjects(JSONObject jsonObj, ArrayList<CpsObject> objects, int objI) {
+		JSONArray arr = new JSONArray();
+		for (CpsObject cps : objects) {
+			arr.add(cps.getStored() );
+			arr.add(cps.getObjName());
+			arr.add( cps.getName());
+			arr.add(cps.getImage());
+			
+		}
+		jsonObj.put("CategoryObject." + objI++, arr);
+	}
+
+	public void readFromJSON(File jsonFile) throws IOException {
 
 		String line;
 		BufferedReader reader = new BufferedReader(new FileReader("textfile"));
@@ -24,8 +72,4 @@ public class LoadStoreController {
 			// mach hier irgendwas mit der Gelesenen Zeile
 		}
 	}
-
-	public void unZip(File zipFile) throws IOException {
-
-	}
 }

+ 2 - 0
src/ui/view/Main.java

@@ -34,6 +34,8 @@ public class Main {
 					
 
 					VIEW.getFrmCyberPhysical().setVisible(true);
+					
+					CONTROL.writeFile();
 
 				} catch (Exception e) {
 					e.printStackTrace();