浏览代码

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

dominik.rieder 8 年之前
父节点
当前提交
2a79bc5c69
共有 2 个文件被更改,包括 96 次插入40 次删除
  1. 51 39
      src/ui/controller/LoadController.java
  2. 45 1
      src/ui/controller/StoreController.java

+ 51 - 39
src/ui/controller/LoadController.java

@@ -26,6 +26,10 @@ import ui.model.idCounter;
 
 public class LoadController {
 
+	public enum MODE {
+		ALL, CATEGORY, CANVAS
+	}
+
 	private Model MODEL;
 	private CategoryController cgC;
 	private CanvasController cvsC;
@@ -50,52 +54,55 @@ public class LoadController {
 	 */
 	public void readJSON(String path) throws IOException {
 		JSONParser parser = new JSONParser();
-		MODEL.setCgIdx(new HashMap<String,Integer>());
-		MODEL.setCvsObjIdx(new HashMap<Integer,Integer>());
-		MODEL.setCategories(new ArrayList<Category>());
-		MODEL.setObjectsOnCanvas(new ArrayList<CpsObject>());
-
+		JSONObject json = new JSONObject();
+		try {
+			json = (JSONObject) parser.parse(new FileReader(path));
+		} catch (ParseException e1) {
+			// TODO Auto-generated catch block
+			e1.printStackTrace();
+		}
 
 		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("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
-					idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
-			}
-			
-			Collections.sort(obj);
-			Collections.sort(ele);
-			Collections.sort(edge);
-			Collections.sort(gp);
-
-			dispatch(obj, json);
-			dispatch(ele, json);
-			dispatch(edge, json);
-			dispatch(gp, json);
-
-		} catch (Exception e) {
-			// Should be ParseException, but gradle won't work with it
-			// TODO Auto-generated catch block
-			e.printStackTrace();
+		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>());
+		}
+
+		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());
+
+		}
+
+		Collections.sort(obj);
+		Collections.sort(ele);
+		Collections.sort(edge);
+		Collections.sort(gp);
+
+		dispatch(obj, json);
+		dispatch(ele, json);
+		dispatch(edge, json);
+		dispatch(gp, json);
 
 	}
 
@@ -201,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);
@@ -213,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)));
 
@@ -241,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
 	 */

+ 45 - 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);
@@ -53,6 +60,43 @@ public class StoreController {
 		writer.flush();
 		writer.close();
 	}
+	
+	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();
+	}
 
 	/**
 	 * writes all Categories into a JSONObject
@@ -141,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());