Browse Source

loading and storing canvas or category only

Teh-Hai Julian Zheng 8 years ago
parent
commit
08fc80fa92
2 changed files with 88 additions and 49 deletions
  1. 47 48
      src/ui/controller/LoadController.java
  2. 41 1
      src/ui/controller/StoreController.java

+ 47 - 48
src/ui/controller/LoadController.java

@@ -25,11 +25,9 @@ import ui.model.Model;
 import ui.model.idCounter;
 
 public class LoadController {
-	
+
 	public enum MODE {
-		ALL,
-		CATEGORY,
-		CANVAS
+		ALL, CATEGORY, CANVAS
 	}
 
 	private Model MODEL;
@@ -56,60 +54,56 @@ public class LoadController {
 	 */
 	public void readJSON(String path) throws IOException {
 		JSONParser parser = new JSONParser();
+		JSONObject json = new JSONObject();
+		try {
+			json = (JSONObject) parser.parse(new FileReader(path));
+		} catch (ParseException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		}
 
-		MODE mode = null;
 		ArrayList<String> obj = new ArrayList<>();
 		ArrayList<String> ele = new ArrayList<>();
 		ArrayList<String> edge = new ArrayList<>();
 		ArrayList<String> gp = new ArrayList<>();
 
-		try {
+		MODE mode = MODE.valueOf(json.get("MODE").toString());
 
-			JSONObject json = (JSONObject) parser.parse(new FileReader(path));
-
-			for (Object key : json.keySet()) {
-				if(key.equals("MODE"))
-					mode = MODE.valueOf(json.get("MODE").toString());
-				else if (key.equals("CG"))
-					readCategory((JSONArray) json.get(key));
-				else if (key.toString().contains("CGO") || key.toString().contains("CVSO"))
-					obj.add(key.toString());
-				else if (key.toString().contains("CGE") || key.toString().contains("CVSE"))
-					ele.add(key.toString());
-				else if (key.toString().contains("EDGE"))
-					edge.add(key.toString());
-				else if (key.toString().contains("CGGP") || key.toString().contains("CVSGP"))
-					gp.add(key.toString());
-				else if (key.equals("ID"))
-					idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
-			}
-			
-			Collections.sort(obj);
-			Collections.sort(ele);
-			Collections.sort(edge);
-			Collections.sort(gp);
-			
-			if(mode.equals(MODE.ALL) || mode.equals(MODE.CATEGORY)){
-				MODEL.setCgIdx(new HashMap<String,Integer>());
-				MODEL.setCategories(new ArrayList<Category>());
-			}
-			if(mode.equals(MODE.ALL) || mode.equals(MODE.CATEGORY)){
-				MODEL.setCvsObjIdx(new HashMap<Integer,Integer>());
-				MODEL.setObjectsOnCanvas(new ArrayList<CpsObject>());
-			}
-			
+		if (mode.equals(MODE.ALL) || mode.equals(MODE.CATEGORY)) {
+			MODEL.setCgIdx(new HashMap<String, Integer>());
+			MODEL.setCategories(new ArrayList<Category>());
+		}
+		if (mode.equals(MODE.ALL) || mode.equals(MODE.CATEGORY)) {
+			MODEL.setCvsObjIdx(new HashMap<Integer, Integer>());
+			MODEL.setObjectsOnCanvas(new ArrayList<CpsObject>());
+		}
 
-			dispatch(obj, json);
-			dispatch(ele, json);
-			dispatch(edge, json);
-			dispatch(gp, json);
+		for (Object key : json.keySet()) {
+			if (key.equals("CG"))
+				readCategory((JSONArray) json.get(key));
+			else if (key.equals("ID"))
+				idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
+			else if (key.toString().contains("CGO") || key.toString().contains("CVSO"))
+				obj.add(key.toString());
+			else if (key.toString().contains("CGE") || key.toString().contains("CVSE"))
+				ele.add(key.toString());
+			else if (key.toString().contains("EDGE"))
+				edge.add(key.toString());
+			else if (key.toString().contains("CGGP") || key.toString().contains("CVSGP"))
+				gp.add(key.toString());
 
-		} catch (Exception e) {
-			// Should be ParseException, but gradle won't work with it
-			// TODO Auto-generated catch block
-			e.printStackTrace();
 		}
 
+		Collections.sort(obj);
+		Collections.sort(ele);
+		Collections.sort(edge);
+		Collections.sort(gp);
+
+		dispatch(obj, json);
+		dispatch(ele, json);
+		dispatch(edge, json);
+		dispatch(gp, json);
+
 	}
 
 	/**
@@ -214,6 +208,7 @@ public class LoadController {
 
 		if (sav.equals("CVS")) {
 			ele.setActive(convert(Integer.parseInt(next(i))));
+			System.out.println(ele.getActive());
 			objC.addElementIntoCanvasObject((HolonObject) mpC.searchByID(Integer.parseInt(obj)), ele);
 		} else
 			objC.addElementIntoCategoryObject(sav, obj, ele);
@@ -226,7 +221,8 @@ public class LoadController {
 	public void readEdge(JSONArray arr) {
 		Iterator<Object> i = arr.iterator();
 
-		CpsEdge edge = new CpsEdge(mpC.searchByID(Integer.parseInt(next(i))), mpC.searchByID(Integer.parseInt(next(i))));
+		CpsEdge edge = new CpsEdge(mpC.searchByID(Integer.parseInt(next(i))),
+				mpC.searchByID(Integer.parseInt(next(i))));
 		edge.setCapacity(Float.parseFloat(next(i)));
 		edge.setFlow(Float.parseFloat(next(i)));
 
@@ -254,14 +250,17 @@ public class LoadController {
 
 	/**
 	 * Get the Next Element from an Iterator
+	 * 
 	 * @param i
 	 * @return
 	 */
 	public String next(Iterator<Object> i) {
 		return i.next().toString();
 	}
+
 	/**
 	 * converting saved Integers into Booleans
+	 * 
 	 * @param x
 	 * @return
 	 */

+ 41 - 1
src/ui/controller/StoreController.java

@@ -19,6 +19,12 @@ import ui.model.Model;
 import ui.model.idCounter;
 
 public class StoreController {
+	
+	public enum MODE {
+		ALL,
+		CATEGORY,
+		CANVAS
+	}
 
 	private Model MODEL;
 
@@ -37,6 +43,7 @@ public class StoreController {
 
 		JSONObject json = new JSONObject();
 
+		json.put("MODE", "ALL");
 		json.put("ID", idCounter.getCounter());
 		writeCategory(json);
 		writeCategoryObjects(json);
@@ -56,6 +63,39 @@ public class StoreController {
 	
 	public void writeCanvasFile(String path) throws IOException {
 		
+		JSONObject json = new JSONObject();
+		
+		json.put("MODE", "CANVAS");
+		json.put("ID", idCounter.getCounter());
+		writeCanvasObjects(json);
+		writeCanvasElements(json);
+		writeEdges(json);
+		writeElementGraph(json);
+		
+		FileWriter writer = new FileWriter(path);
+		writer.write(json.toJSONString());
+		getClass();
+
+		writer.flush();
+		writer.close();
+	}
+	
+	public void writeCategoryFile(String path) throws IOException {
+		
+		JSONObject json = new JSONObject();
+		
+		json.put("MODE", "CATEGORY");
+		//eventuell muss man ID auch Speichern
+		writeCategory(json);
+		writeCategoryObjects(json);
+		writeCategoryElements(json);
+		
+		FileWriter writer = new FileWriter(path);
+		writer.write(json.toJSONString());
+		getClass();
+
+		writer.flush();
+		writer.close();
 	}
 
 	/**
@@ -145,7 +185,7 @@ public class StoreController {
 			if (cps instanceof HolonObject)
 				for (HolonElement ele : ((HolonObject) cps).getElements()) {
 					arr.add(ele.getSav());
-					arr.add(ele.getObj());
+					arr.add(cps.getID());
 					arr.add(ele.getEleName());
 					arr.add(ele.getAmount());
 					arr.add(ele.getEnergy());