Pārlūkot izejas kodu

split into LoadController and StoreController

Teh-Hai Julian Zheng 8 gadi atpakaļ
vecāks
revīzija
6bae1021ac

+ 6 - 4
src/ui/controller/Control.java

@@ -25,7 +25,8 @@ public class Control {
 	private final ObjectController objectController;
 	private final CanvasController canvasController;
 	private final GlobalController globalController;
-	private final LoadStoreController loadStoreController;
+	private final StoreController storeController;
+	private final LoadController loadController;
 
 	public Control(Model model) {
 		this.MODEL = model;
@@ -33,7 +34,8 @@ public class Control {
 		this.objectController = new ObjectController(MODEL);
 		this.canvasController = new CanvasController(MODEL);
 		this.globalController = new GlobalController(MODEL);
-		this.loadStoreController = new LoadStoreController(MODEL, categoryController, canvasController, objectController);
+		this.storeController = new StoreController(MODEL);
+		this.loadController = new LoadController(MODEL, categoryController, canvasController, objectController);
 		
 	}
 
@@ -145,11 +147,11 @@ public class Control {
 	
 	/* Operations for Loading and Storing */
 	public void saveFile(String path) throws IOException {
-		loadStoreController.writeJSONFile(path);
+		storeController.writeJSONFile(path);
 	}
 	
 	public void loadFile(String path) throws IOException {
-		loadStoreController.readJSON(path);
+		loadController.readJSON(path);
 	}
 	////////// etc
 	public void initListener(CategoryListener catLis) {

+ 4 - 205
src/ui/controller/LoadStoreController.java → src/ui/controller/LoadController.java

@@ -2,18 +2,15 @@ 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;
 import org.json.simple.parser.JSONParser;
 import org.json.simple.parser.ParseException;
 
-import classes.Category;
 import classes.CpsEdge;
 import classes.CpsNode;
 import classes.CpsObject;
@@ -24,14 +21,14 @@ import classes.HolonTransformer;
 import ui.model.Model;
 import ui.model.idCounter;
 
-public class LoadStoreController {
+public class LoadController {
 
 	private Model MODEL;
 	private CategoryController cgC;
 	private CanvasController cvsC;
 	private ObjectController objC;
 
-	public LoadStoreController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
+	public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
 		this.MODEL = model;
 		this.cgC = cg;
 		this.cvsC = cvs;
@@ -39,205 +36,6 @@ public class LoadStoreController {
 
 	}
 
-	/**
-	 * Writes a Savefile in .json
-	 * 
-	 * @throws IOException
-	 */
-	public void writeJSONFile(String path) throws IOException {
-
-		JSONObject json = new JSONObject();
-
-		json.put("ID", idCounter.getCounter());
-		writeCategory(json);
-		writeObjects(json);
-		writeElements(json);
-		writeEdges(json);
-		writeElementGraph(json);
-
-		FileWriter writer = new FileWriter(path);
-		writer.write(json.toJSONString());
-		writer.flush();
-		writer.close();
-	}
-
-	/**
-	 * writes all Categories into a JSONObject
-	 * 
-	 * @param json
-	 * @throws IOException
-	 */
-	public void writeCategory(JSONObject json) {
-		JSONArray arr = new JSONArray();
-
-		for (Category cat : MODEL.getCategories())
-			arr.add(cat.getName());
-
-		json.put("CG", arr);
-	}
-
-	/**
-	 * writes all Objects in Category into a JSONObject
-	 * 
-	 * @param json
-	 */
-	public void writeObjects(JSONObject json) {
-
-		JSONArray arr = new JSONArray();
-		int i = 1;
-
-		for (Category cats : MODEL.getCategories())
-			for (CpsObject cps : cats.getObjects()) {
-				arr.add(getObjectType(cps));
-				arr.add(cps.getSav());
-				arr.add(cps.getObjName());
-				arr.add(cps.getImage());
-				json.put("CGO" + i++, arr);
-				arr = new JSONArray();
-			}
-
-		i = 1;
-		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);
-			arr.add(cps.getPosition().y);
-			json.put("CVSO" + i++, arr);
-			arr = new JSONArray();
-			;
-
-		}
-	}
-
-	/**
-	 * 
-	 * @param cps
-	 * @return
-	 */
-	public String getObjectType(CpsObject cps) {
-		if (cps instanceof HolonObject)
-			return "HolonObject";
-		if (cps instanceof HolonTransformer)
-			return "HolonTransformer";
-		if (cps instanceof HolonSwitch)
-			return "HolonSwitch";
-		else
-			return "CpsNode";
-	}
-
-	/**
-	 * writes all Elements in Objects in Category into a JSONObject
-	 * 
-	 * @param json
-	 */
-	public void writeElements(JSONObject json) {
-
-		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()) {
-						arr.add(ele.getSav());
-						arr.add(ele.getObj());
-						arr.add(ele.getEleName());
-						arr.add(ele.getAmount());
-						arr.add(ele.getEnergy());
-						json.put("CGE" + i++, arr);
-						arr = new JSONArray();
-					}
-
-		i = 1;
-		for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
-			if (cps instanceof HolonObject)
-				for (HolonElement ele : ((HolonObject) cps).getElements()) {
-					arr.add(ele.getSav());
-					arr.add(cps.getID());
-					arr.add(ele.getEleName());
-					arr.add(ele.getAmount());
-					arr.add(ele.getEnergy());
-					json.put("CVSE" + i++, arr);
-					arr = new JSONArray();
-				}
-		}
-	}
-
-	/**
-	 * write all Edges into a JSONObject
-	 * 
-	 * @param json
-	 */
-	public void writeEdges(JSONObject json) {
-
-		JSONArray arr = new JSONArray();
-		int i = 1;
-
-		for (CpsEdge edge : MODEL.getEdgesOnCanvas()) {
-			arr.add(edge.getA().getID());
-			arr.add(edge.getB().getID());
-			arr.add(edge.getCapacity());
-			arr.add(edge.getFlow());
-			json.put("EDGE" + i++, arr);
-			arr = new JSONArray();
-		}
-	}
-
-	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
@@ -398,4 +196,5 @@ public class LoadStoreController {
 		}
 
 	}
-}
+
+}

+ 230 - 0
src/ui/controller/StoreController.java

@@ -0,0 +1,230 @@
+package ui.controller;
+
+import java.awt.Point;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.ListIterator;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import classes.Category;
+import classes.CpsEdge;
+import classes.CpsObject;
+import classes.HolonElement;
+import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.HolonTransformer;
+import ui.model.Model;
+import ui.model.idCounter;
+
+public class StoreController {
+
+	private Model MODEL;
+
+	public StoreController(Model model) {
+		this.MODEL = model;
+
+	}
+
+	/**
+	 * Writes the current State of the Modelling into a JSON File which can be
+	 * loaded
+	 * 
+	 * @throws IOException
+	 */
+	public void writeJSONFile(String path) throws IOException {
+
+		JSONObject json = new JSONObject();
+
+		json.put("ID", idCounter.getCounter());
+		writeCategory(json);
+		writeObjects(json);
+		writeElements(json);
+		writeEdges(json);
+		writeElementGraph(json);
+
+		FileWriter writer = new FileWriter(path);
+		writer.write(json.toJSONString());
+		writer.flush();
+		writer.close();
+	}
+
+	/**
+	 * writes all Categories into a JSONObject
+	 * 
+	 * @param json
+	 * @throws IOException
+	 */
+	public void writeCategory(JSONObject json) {
+		JSONArray arr = new JSONArray();
+
+		for (Category cat : MODEL.getCategories())
+			arr.add(cat.getName());
+
+		json.put("CG", arr);
+	}
+
+	/**
+	 * writes all Objects in Category into a JSONObject
+	 * 
+	 * @param json
+	 */
+	public void writeObjects(JSONObject json) {
+
+		JSONArray arr = new JSONArray();
+		int i = 1;
+
+		for (Category cats : MODEL.getCategories())
+			for (CpsObject cps : cats.getObjects()) {
+				arr.add(getObjectType(cps));
+				arr.add(cps.getSav());
+				arr.add(cps.getObjName());
+				arr.add(cps.getImage());
+				json.put("CGO" + i++, arr);
+				arr = new JSONArray();
+			}
+
+		i = 1;
+		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);
+			arr.add(cps.getPosition().y);
+			json.put("CVSO" + i++, arr);
+			arr = new JSONArray();
+			;
+
+		}
+	}
+
+	/**
+	 * writes all Elements in Objects in Category into a JSONObject
+	 * 
+	 * @param json
+	 */
+	public void writeElements(JSONObject json) {
+
+		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()) {
+						arr.add(ele.getSav());
+						arr.add(ele.getObj());
+						arr.add(ele.getEleName());
+						arr.add(ele.getAmount());
+						arr.add(ele.getEnergy());
+						json.put("CGE" + i++, arr);
+						arr = new JSONArray();
+					}
+
+		i = 1;
+		for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
+			if (cps instanceof HolonObject)
+				for (HolonElement ele : ((HolonObject) cps).getElements()) {
+					arr.add(ele.getSav());
+					arr.add(cps.getID());
+					arr.add(ele.getEleName());
+					arr.add(ele.getAmount());
+					arr.add(ele.getEnergy());
+					json.put("CVSE" + i++, arr);
+					arr = new JSONArray();
+				}
+		}
+	}
+
+	/**
+	 * write all Edges into a JSONObject
+	 * 
+	 * @param json
+	 */
+	public void writeEdges(JSONObject json) {
+
+		JSONArray arr = new JSONArray();
+		int i = 1;
+
+		for (CpsEdge edge : MODEL.getEdgesOnCanvas()) {
+			arr.add(edge.getA().getID());
+			arr.add(edge.getB().getID());
+			arr.add(edge.getCapacity());
+			arr.add(edge.getFlow());
+			json.put("EDGE" + i++, arr);
+			arr = new JSONArray();
+		}
+	}
+
+	/**
+	 * writes the Graph from all Elements into a JSONObject
+	 * 
+	 * @param json
+	 */
+	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();
+					}
+		}
+	}
+
+	/**
+	 * Return the Object Type
+	 * 
+	 * @param cps
+	 * @return
+	 */
+	public String getObjectType(CpsObject cps) {
+		if (cps instanceof HolonObject)
+			return "HolonObject";
+		if (cps instanceof HolonTransformer)
+			return "HolonTransformer";
+		if (cps instanceof HolonSwitch)
+			return "HolonSwitch";
+		else
+			return "CpsNode";
+	}
+
+}