Teh-Hai Julian Zheng 8 years ago
parent
commit
741f2e3b7e

+ 1 - 1
src/ui/controller/CategoryController.java

@@ -33,7 +33,7 @@ public class CategoryController {
 		addNewHolonObject(searchCatNode("Energy"), "Power Plant", new ArrayList<>(), "/Images/power-plant.png");
 		addNewHolonObject(searchCatNode("Building"), "House", new ArrayList<>(), "/Images/home-2.png");
 		addNewHolonTransformer(searchCatNode("Component"), "Transformer", "/Images/transformer-1.png");
-		addNewHolonSwitch(searchCatNode("Component"), "Switch", "/Images/switch-on.png");
+		addNewHolonSwitch(searchCatNode("Component"), "Switch", "/Images/switch-off.png");
 		
 	}
 

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

@@ -55,7 +55,7 @@ public class Control {
 	}
 
 	public void addSwitch(Category cat, String objName) {
-		categoryController.addNewHolonSwitch(cat, objName, "/Images/switch-on.png");
+		categoryController.addNewHolonSwitch(cat, objName, "/Images/switch-off.png");
 	}
 
 	public Category searchCategory(String name) {

+ 117 - 42
src/ui/controller/LoadStoreController.java

@@ -1,10 +1,12 @@
 package ui.controller;
 
+import java.awt.Point;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.ListIterator;
 
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
@@ -25,15 +27,15 @@ import ui.model.idCounter;
 public class LoadStoreController {
 
 	private Model MODEL;
-	private CategoryController categoryController;
-	private CanvasController canvasController;
-	private ObjectController objectController;
+	private CategoryController cgC;
+	private CanvasController cvsC;
+	private ObjectController objC;
 
 	public LoadStoreController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
 		this.MODEL = model;
-		this.categoryController = cg;
-		this.canvasController = cvs;
-		this.objectController = obj;
+		this.cgC = cg;
+		this.cvsC = cvs;
+		this.objC = obj;
 
 	}
 
@@ -51,7 +53,7 @@ public class LoadStoreController {
 		writeObjects(json);
 		writeElements(json);
 		writeEdges(json);
-		
+		writeElementGraph(json);
 
 		FileWriter writer = new FileWriter(path);
 		writer.write(json.toJSONString());
@@ -98,6 +100,7 @@ public class LoadStoreController {
 		for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
 			arr.add(getObjectType(cps));
 			arr.add(cps.getObjName());
+			arr.add(cps.getName());
 			arr.add(cps.getID());
 			arr.add(cps.getImage());
 			arr.add(cps.getPosition().x);
@@ -162,14 +165,6 @@ public class LoadStoreController {
 				}
 		}
 	}
-	
-	/**
-	 * Writes all Graph Points into JSONObject
-	 * @param json
-	 */
-	public void writeGraph(JSONObject json) {
-		
-	}
 
 	/**
 	 * write all Edges into a JSONObject
@@ -191,6 +186,63 @@ public class LoadStoreController {
 		}
 	}
 
+	public void writeElementGraph(JSONObject json) {
+
+		ListIterator<Point> iterator;
+		JSONArray arr = new JSONArray();
+		int i = 1;
+
+		for (Category cats : MODEL.getCategories())
+			for (CpsObject cps : cats.getObjects())
+				if (cps instanceof HolonObject)
+					for (HolonElement ele : ((HolonObject) cps).getElements())
+						if (!ele.getGraphPoints().isEmpty()) {
+							iterator = ele.getGraphPoints().listIterator();
+
+							arr.add(ele.getSav());
+							arr.add(ele.getObj());
+							arr.add(ele.getEleName());
+
+							while (iterator.hasNext()) {
+								Point p = iterator.next();
+								arr.add((int)p.getX());
+								arr.add((int)p.getY());
+							}
+							json.put("CGGP" + i++, arr);
+							arr = new JSONArray();
+
+						}
+
+		i = 1;
+		for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
+			if (cps instanceof HolonObject)
+				for (HolonElement ele : ((HolonObject) cps).getElements())
+					if (!ele.getGraphPoints().isEmpty()) {
+						iterator = ele.getGraphPoints().listIterator();
+
+						arr.add(ele.getSav());
+						arr.add(cps.getID());
+						arr.add(ele.getEleName());
+
+						while (iterator.hasNext()) {
+							Point p = iterator.next();
+							arr.add((int)p.getX());
+							arr.add((int)p.getY());
+						}
+						json.put("CVSGP" + i++, arr);
+						arr = new JSONArray();
+
+					}
+
+		}
+
+	}
+
+	/**
+	 * 
+	 * @param path
+	 * @throws IOException
+	 */
 	public void readJSON(String path) throws IOException {
 		JSONParser parser = new JSONParser();
 		MODEL.setCategories(new ArrayList<>());
@@ -199,6 +251,7 @@ public class LoadStoreController {
 		ArrayList<String> obj = new ArrayList<>();
 		ArrayList<String> ele = new ArrayList<>();
 		ArrayList<String> edge = new ArrayList<>();
+		ArrayList<String> gp = new ArrayList<>();
 
 		try {
 
@@ -214,12 +267,16 @@ public class LoadStoreController {
 					ele.add(key.toString());
 				else if (key.toString().contains("EDGE"))
 					edge.add(key.toString());
-				else idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
+				else if (key.toString().contains("CGGP") || key.toString().contains("CVSGP"))
+					gp.add(key.toString());
+				else
+					idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
 			}
-
+			System.out.println(gp.get(0));
 			dispatch(obj, json);
 			dispatch(ele, json);
 			dispatch(edge, json);
+			dispatch(gp, json);
 
 		} catch (ParseException e) {
 			// TODO Auto-generated catch block
@@ -239,6 +296,8 @@ public class LoadStoreController {
 				readElement((JSONArray) json.get(str));
 			} else if (str.contains("EDGE")) {
 				readEdge((JSONArray) json.get(str));
+			} else if (str.contains("CGGP") || str.contains("CVSGP")) {
+				readElementGraph((JSONArray) json.get(str));
 			}
 		}
 
@@ -249,7 +308,7 @@ public class LoadStoreController {
 		Iterator<Object> i = arr.iterator();
 
 		while (i.hasNext()) {
-			categoryController.addNewCategory(i.next().toString());
+			cgC.addNewCategory(i.next().toString());
 		}
 	}
 
@@ -259,16 +318,13 @@ public class LoadStoreController {
 		String type = i.next().toString();
 
 		if (type.equals("HolonObject")) {
-			categoryController.addNewHolonObject(categoryController.searchCatNode(i.next().toString()),
-					i.next().toString(), new ArrayList<>(), i.next().toString());
-		}
-		if (type.equals("HolonTransformer")) {
-			categoryController.addNewHolonTransformer(categoryController.searchCatNode(i.next().toString()),
-					i.next().toString(), i.next().toString());
-		}
-		if (type.equals("HolonSwitch")) {
-			categoryController.addNewHolonSwitch(categoryController.searchCatNode(i.next().toString()),
-					i.next().toString(), i.next().toString());
+			cgC.addNewHolonObject(cgC.searchCatNode(i.next().toString()), i.next().toString(), new ArrayList<>(),
+					i.next().toString());
+		} else if (type.equals("HolonTransformer")) {
+			cgC.addNewHolonTransformer(cgC.searchCatNode(i.next().toString()), i.next().toString(),
+					i.next().toString());
+		} else if (type.equals("HolonSwitch")) {
+			cgC.addNewHolonSwitch(cgC.searchCatNode(i.next().toString()), i.next().toString(), i.next().toString());
 		}
 
 	}
@@ -281,46 +337,65 @@ public class LoadStoreController {
 
 		if (type.equals("HolonObject")) {
 			cps = new HolonObject(i.next().toString());
-		}
-		if (type.equals("HolonTransformer")) {
+		} else if (type.equals("HolonTransformer")) {
 			cps = new HolonTransformer(i.next().toString());
-		}
-		if (type.equals("HolonSwitch")) {
+		} else if (type.equals("HolonSwitch")) {
 			cps = new HolonSwitch(i.next().toString());
-		}
-		if (type.equals("CpsNode")) {
+		} else if (type.equals("CpsNode")) {
 			cps = new CpsNode(i.next().toString());
 		}
 
+		cps.setName(i.next().toString());
 		cps.setID(Integer.parseInt(i.next().toString()));
 		cps.setImage(i.next().toString());
 		cps.setPosition(Integer.parseInt(i.next().toString()), Integer.parseInt(i.next().toString()));
 
-		canvasController.addObjectIntoCanvas(cps);
+		cvsC.addObjectIntoCanvas(cps);
 	}
 
 	public void readElement(JSONArray arr) {
 		Iterator<Object> i = arr.iterator();
 
 		String sav = i.next().toString();
-		System.out.println(sav);
-
 		if (sav.equals("Canvas")) {
-			objectController.addNewElementIntoCanvasObject(Integer.parseInt(i.next().toString()), i.next().toString(),
+			objC.addNewElementIntoCanvasObject(Integer.parseInt(i.next().toString()), i.next().toString(),
 					Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
 		} else
-			objectController.addNewElementIntoCategoryObject(sav, i.next().toString(), i.next().toString(),
+			objC.addNewElementIntoCategoryObject(sav, i.next().toString(), i.next().toString(),
 					Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
 	}
 
 	public void readEdge(JSONArray arr) {
 		Iterator<Object> i = arr.iterator();
 
-		CpsEdge edge = new CpsEdge(objectController.searchByID(Integer.parseInt(i.next().toString())),
-				objectController.searchByID(Integer.parseInt(i.next().toString())));
+		CpsEdge edge = new CpsEdge(objC.searchByID(Integer.parseInt(i.next().toString())),
+				objC.searchByID(Integer.parseInt(i.next().toString())));
 		edge.setCapacity(Float.parseFloat(i.next().toString()));
 		edge.setFlow(Float.parseFloat(i.next().toString()));
 
-		canvasController.addEdgeOnCanvas(edge);
+		cvsC.addEdgeOnCanvas(edge);
+	}
+
+	public void readElementGraph(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+
+		String sav = i.next().toString();
+		HolonElement ele;
+
+		if (sav.equals("Canvas")) {
+			ele = objC.searchHolonElement((HolonObject) objC.searchByID(Integer.parseInt(i.next().toString())),
+					i.next().toString());
+			while (i.hasNext())
+				ele.getGraphPoints()
+						.add(new Point(Integer.parseInt(i.next().toString()), Integer.parseInt(i.next().toString())));
+			System.out.println(ele.getGraphPoints().size());
+		} else {
+			ele = objC.searchHolonElement(objC.searchHolonObject(i.next().toString(),
+					objC.searchCategory(sav, MODEL.getCategories()).getObjects()), i.next().toString());
+			while (i.hasNext())
+				ele.getGraphPoints()
+						.add(new Point(Integer.parseInt(i.next().toString()), Integer.parseInt(i.next().toString())));
+		}
+
 	}
 }