Teh-Hai Julian Zheng 8 years ago
parent
commit
414309584d
2 changed files with 39 additions and 14 deletions
  1. 3 0
      src/classes/CpsNode.java
  2. 36 14
      src/ui/controller/LoadStoreController.java

+ 3 - 0
src/classes/CpsNode.java

@@ -1,11 +1,14 @@
 package classes;
 
+import java.util.ArrayList;
+
 import ui.model.idCounter;
 
 public class CpsNode extends CpsObject {
 
 	public CpsNode(String objName) {
 		super(objName);
+		this.setConnections(new ArrayList<CpsEdge>());
 		this.setImage("/Images/node.png");
 		this.setID(idCounter.nextId());
 		this.setStored("Canvas");

+ 36 - 14
src/ui/controller/LoadStoreController.java

@@ -14,6 +14,8 @@ import com.sun.scenario.effect.impl.sw.java.JSWBlend_COLOR_BURNPeer;
 
 import classes.Category;
 import classes.CpsObject;
+import classes.HolonElement;
+import classes.HolonObject;
 import ui.model.Model;
 
 public class LoadStoreController {
@@ -29,6 +31,8 @@ public class LoadStoreController {
 		JSONObject jsonObj = new JSONObject();
 
 		writeCategory(jsonObj);
+		writeCategoryObjects(jsonObj);
+		writeCategoryElements(jsonObj);
 
 		FileWriter file = new FileWriter("//Users//zheng//Desktop//Tesst.json");
 		file.write(jsonObj.toJSONString());
@@ -39,29 +43,47 @@ public class LoadStoreController {
 	}
 
 	public void writeCategory(JSONObject jsonObj) throws IOException {
-		
-		int objI = 1;
 
 		JSONArray arr = new JSONArray();
-		for (Category cat : MODEL.getCategories()) {
+		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) {
+	public void writeCategoryObjects(JSONObject jsonObj) {
+		int i = 1;
 		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);
+
+		for (Category cats : MODEL.getCategories())
+			for (CpsObject cps : cats.getObjects()) {
+				arr.add(cps.getStored());
+				arr.add(cps.getObjName());
+				arr.add(cps.getName());
+				arr.add(cps.getImage());
+				jsonObj.put("CategoryObject." + i++, arr);
+				arr = new JSONArray();
+			}
+
+	}
+
+	public void writeCategoryElements(JSONObject jsonObj) {
+		int i = 1;
+		JSONArray arr = new JSONArray();
+
+		for (Category cats : MODEL.getCategories())
+			for (CpsObject cps : cats.getObjects())
+				if (cps instanceof HolonObject)
+					for (HolonElement ele : ((HolonObject) cps).getElements()) {
+						arr.add(ele.getStored());
+						arr.add(ele.getEleName());
+						arr.add(ele.getAmount());
+						arr.add(ele.getEnergy());
+						jsonObj.put("CategoryElement." + i++, arr);
+						arr = new JSONArray();
+					}
+
 	}
 
 	public void readFromJSON(File jsonFile) throws IOException {