Преглед на файлове

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

Kevin Trometer преди 8 години
родител
ревизия
6ab4f2b0aa

+ 2 - 0
src/classes/CpsEdge.java

@@ -11,6 +11,8 @@ public class CpsEdge {
 	public CpsEdge(CpsObject A, CpsObject B){
 		setA(A);
 		setB(B);
+		this.A.AddConnection(this);
+		this.B.AddConnection(this);
 		this.maxCapacity = 100;
 	}
 	

+ 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");
 		
 	}
 

+ 7 - 5
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);
 		
 	}
 
@@ -55,7 +57,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) {
@@ -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) {

+ 200 - 0
src/ui/controller/LoadController.java

@@ -0,0 +1,200 @@
+package ui.controller;
+
+import java.awt.Point;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
+import classes.CpsEdge;
+import classes.CpsNode;
+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 LoadController {
+
+	private Model MODEL;
+	private CategoryController cgC;
+	private CanvasController cvsC;
+	private ObjectController objC;
+
+	public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
+		this.MODEL = model;
+		this.cgC = cg;
+		this.cvsC = cvs;
+		this.objC = obj;
+
+	}
+
+	/**
+	 * 
+	 * @param path
+	 * @throws IOException
+	 */
+	public void readJSON(String path) throws IOException {
+		JSONParser parser = new JSONParser();
+		MODEL.setCategories(new ArrayList<>());
+		MODEL.setObjectsOnCanvas(new ArrayList<>());
+
+		ArrayList<String> obj = new ArrayList<>();
+		ArrayList<String> ele = new ArrayList<>();
+		ArrayList<String> edge = new ArrayList<>();
+		ArrayList<String> gp = new ArrayList<>();
+
+		try {
+
+			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()));
+			}
+			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
+			e.printStackTrace();
+		}
+
+	}
+
+	public void dispatch(ArrayList<String> input, JSONObject json) {
+
+		for (String str : input) {
+			if (str.contains("CGO")) {
+				readCategoryObject((JSONArray) json.get(str));
+			} else if (str.contains("CVSO")) {
+				readCanvasObject((JSONArray) json.get(str));
+			} else if (str.contains("CGE") || str.contains("CVSE")) {
+				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));
+			}
+		}
+
+	}
+
+	public void readCategory(JSONArray arr) {
+
+		Iterator<Object> i = arr.iterator();
+
+		while (i.hasNext()) {
+			cgC.addNewCategory(i.next().toString());
+		}
+	}
+
+	public void readCategoryObject(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+
+		String type = i.next().toString();
+
+		if (type.equals("HolonObject")) {
+			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());
+		}
+
+	}
+
+	public void readCanvasObject(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+		CpsObject cps = null;
+
+		String type = i.next().toString();
+
+		if (type.equals("HolonObject")) {
+			cps = new HolonObject(i.next().toString());
+		} else if (type.equals("HolonTransformer")) {
+			cps = new HolonTransformer(i.next().toString());
+		} else if (type.equals("HolonSwitch")) {
+			cps = new HolonSwitch(i.next().toString());
+		} 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()));
+
+		cvsC.addObjectIntoCanvas(cps);
+	}
+
+	public void readElement(JSONArray arr) {
+		Iterator<Object> i = arr.iterator();
+
+		String sav = i.next().toString();
+		if (sav.equals("Canvas")) {
+			objC.addNewElementIntoCanvasObject(Integer.parseInt(i.next().toString()), i.next().toString(),
+					Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
+		} else
+			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(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()));
+
+		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())));
+		}
+
+	}
+
+}

+ 0 - 326
src/ui/controller/LoadStoreController.java

@@ -1,326 +0,0 @@
-package ui.controller;
-
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Iterator;
-
-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;
-import classes.HolonElement;
-import classes.HolonObject;
-import classes.HolonSwitch;
-import classes.HolonTransformer;
-import ui.model.Model;
-import ui.model.idCounter;
-
-public class LoadStoreController {
-
-	private Model MODEL;
-	private CategoryController categoryController;
-	private CanvasController canvasController;
-	private ObjectController objectController;
-
-	public LoadStoreController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj) {
-		this.MODEL = model;
-		this.categoryController = cg;
-		this.canvasController = cvs;
-		this.objectController = obj;
-
-	}
-
-	/**
-	 * 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);
-		
-
-		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.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();
-				}
-		}
-	}
-	
-	/**
-	 * Writes all Graph Points into JSONObject
-	 * @param json
-	 */
-	public void writeGraph(JSONObject json) {
-		
-	}
-
-	/**
-	 * 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 readJSON(String path) throws IOException {
-		JSONParser parser = new JSONParser();
-		MODEL.setCategories(new ArrayList<>());
-		MODEL.setObjectsOnCanvas(new ArrayList<>());
-
-		ArrayList<String> obj = new ArrayList<>();
-		ArrayList<String> ele = new ArrayList<>();
-		ArrayList<String> edge = new ArrayList<>();
-
-		try {
-
-			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 idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
-			}
-
-			dispatch(obj, json);
-			dispatch(ele, json);
-			dispatch(edge, json);
-
-		} catch (ParseException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-
-	}
-
-	public void dispatch(ArrayList<String> input, JSONObject json) {
-
-		for (String str : input) {
-			if (str.contains("CGO")) {
-				readCategoryObject((JSONArray) json.get(str));
-			} else if (str.contains("CVSO")) {
-				readCanvasObject((JSONArray) json.get(str));
-			} else if (str.contains("CGE") || str.contains("CVSE")) {
-				readElement((JSONArray) json.get(str));
-			} else if (str.contains("EDGE")) {
-				readEdge((JSONArray) json.get(str));
-			}
-		}
-
-	}
-
-	public void readCategory(JSONArray arr) {
-
-		Iterator<Object> i = arr.iterator();
-
-		while (i.hasNext()) {
-			categoryController.addNewCategory(i.next().toString());
-		}
-	}
-
-	public void readCategoryObject(JSONArray arr) {
-		Iterator<Object> i = arr.iterator();
-
-		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());
-		}
-
-	}
-
-	public void readCanvasObject(JSONArray arr) {
-		Iterator<Object> i = arr.iterator();
-		CpsObject cps = null;
-
-		String type = i.next().toString();
-
-		if (type.equals("HolonObject")) {
-			cps = new HolonObject(i.next().toString());
-		}
-		if (type.equals("HolonTransformer")) {
-			cps = new HolonTransformer(i.next().toString());
-		}
-		if (type.equals("HolonSwitch")) {
-			cps = new HolonSwitch(i.next().toString());
-		}
-		if (type.equals("CpsNode")) {
-			cps = new CpsNode(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);
-	}
-
-	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(),
-					Integer.parseInt(i.next().toString()), Float.parseFloat(i.next().toString()));
-		} else
-			objectController.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())));
-		edge.setCapacity(Float.parseFloat(i.next().toString()));
-		edge.setFlow(Float.parseFloat(i.next().toString()));
-
-		canvasController.addEdgeOnCanvas(edge);
-	}
-}

+ 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";
+	}
+
+}

+ 18 - 1
src/ui/view/AddElementPopUp.java

@@ -18,7 +18,10 @@ import javax.swing.JComboBox;
 import javax.swing.DefaultComboBoxModel;
 import javax.swing.ImageIcon;
 
+import classes.CpsObject;
 import classes.HolonElement;
+import classes.HolonObject;
+import ui.model.Model;
 
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
@@ -32,12 +35,14 @@ public class AddElementPopUp extends JDialog {
 	private JTextField providedEnergy;
 	private JTextField amount;
 	private HolonElement hl;
+	private CpsObject tempCps;
 
 	/**
 	 * Launch the application.
 	 */
 	public static void main(String[] args) {
 		try {
+
 			AddElementPopUp dialog = new AddElementPopUp();
 			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 			dialog.setVisible(true);
@@ -120,7 +125,14 @@ public class AddElementPopUp extends JDialog {
 				JButton okButton = new JButton("OK");
 				okButton.addActionListener(new ActionListener() {
 					public void actionPerformed(ActionEvent arg0) {
-						if (elementName.getText().length() != 0) {
+						boolean repeated = false;
+						for (HolonElement e : ((HolonObject) tempCps).getElements()) {
+							if (elementName.getText().equals(e.getEleName())) {
+								repeated = true;
+								break;
+							}
+						}
+						if (elementName.getText().length() != 0 && !repeated) {
 							try {
 								float energy = Float.parseFloat(providedEnergy.getText().toString());
 								int elementAmount = Integer.parseInt(amount.getText().toString());
@@ -137,6 +149,7 @@ public class AddElementPopUp extends JDialog {
 							// JOptionPane.showMessageDialog(new JFrame(),
 							// "Please enter a Name");
 							elementName.setBackground(new Color(255, 50, 50));
+							// Jlabel repeatedString = new JLabel();
 						}
 					}
 				});
@@ -157,6 +170,10 @@ public class AddElementPopUp extends JDialog {
 		}
 	}
 
+	public void setActualCps(CpsObject cps) {
+		this.tempCps = cps;
+	}
+
 	public HolonElement getElement() {
 		return hl;
 	}

+ 160 - 86
src/ui/view/GUI.java

@@ -41,6 +41,7 @@ import javax.swing.JTabbedPane;
 import javax.swing.JTable;
 import javax.swing.JToolBar;
 import javax.swing.JTree;
+import javax.swing.SwingUtilities;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 import javax.swing.table.DefaultTableModel;
@@ -58,6 +59,7 @@ import classes.HolonSwitch;
 import classes.HolonTransformer;
 import ui.controller.Control;
 import ui.model.Model;
+import ui.view.PropertyTable;;
 
 public class GUI<E> implements CategoryListener {
 
@@ -78,6 +80,8 @@ public class GUI<E> implements CategoryListener {
 	private final JScrollPane scrollPane_1 = new JScrollPane();
 	private final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
 	private final JScrollPane scrollPane_2 = new JScrollPane();
+	private JPopupMenu popmenuEdit = new JPopupMenu();
+	private JMenuItem editItem = new JMenuItem("Edit Object");
 
 	private final JLabel maxGraph = new JLabel("100%");
 	private final JLabel medGraph = new JLabel("50%");
@@ -99,7 +103,7 @@ public class GUI<E> implements CategoryListener {
 	// HolonObject with consumption/production, name and amount.
 
 	private JTable tableHolonElement = new JTable();
-	private DefaultTableModel tableModelHolonElement = new DefaultTableModel();
+	private PropertyTable tableModelHolonElement = new PropertyTable();
 	private final JPanel scrollElements = new JPanel();
 	JScrollPane tableHolonElementScrollPane = new JScrollPane();
 
@@ -164,6 +168,8 @@ public class GUI<E> implements CategoryListener {
 	private final JComboBox comboBoxAlgo = new JComboBox();
 	private int yTHIS;
 	private int xTHIS;
+	private int yBTHIS;
+	private int xBTHIS;
 
 	/**
 	 * Create the application.
@@ -257,18 +263,26 @@ public class GUI<E> implements CategoryListener {
 
 		scrollPane_2.setViewportView(dtrpnHereWillBe);
 
-		/*
-		 * Right Container Set up
-		 */
-
+		/********************
+		 * RIGHT CONTAINER (INFORMATION)
+		 **********************/
 		// Set up of the HolonElements section
-		Object[] columnNames = { "Device", "Energy", "Quantity" };
+		Object[] columnNames = { "Device", "Energy", "Quantity", "Activated" };
 		tableModelHolonElement.setColumnIdentifiers(columnNames);
 		tableHolonElement.setBorder(null);
 		tableHolonElement.setModel(tableModelHolonElement);
 		tableHolonElement.setFillsViewportHeight(true);
 		tableHolonElement.setCellSelectionEnabled(true);
 		tableHolonElement.setColumnSelectionAllowed(true);
+		tableHolonElementScrollPane.setViewportView(tableHolonElement);
+		scrollElements.setLayout(new BorderLayout(0, 0));
+		scrollElements.add(panel_HolonEl, BorderLayout.NORTH);
+		scrollElements.add(tableHolonElementScrollPane);
+		panel_HolonEl.setLayout(new BoxLayout(panel_HolonEl, BoxLayout.X_AXIS));
+		toolBarHolonEl.add(btnAddHolEL);
+		toolBarHolonEl.add(btnDelHolEL);
+		toolBarHolonEl.setFloatable(false);
+		panel_HolonEl.add(toolBarHolonEl);
 
 		// Set up of the Properties section
 		Object[] colNames = { "Field", "Information" };
@@ -278,62 +292,51 @@ public class GUI<E> implements CategoryListener {
 		tableProperties.setFillsViewportHeight(true);
 		tableProperties.setCellSelectionEnabled(true);
 		tableProperties.setColumnSelectionAllowed(true);
-		// Set up of the Graph section
+		scrollProperties.setViewportView(tableProperties);
 
+		// Set up of the Graph section
 		Object[] tempText = { "Here comes the graph for each clicked HolonElement" };
 		tableModelGraph.setColumnIdentifiers(tempText);
 		tableGraph.setModel(tableModelGraph);
 		tableGraph.setFillsViewportHeight(true);
 		tableGraph.setCellSelectionEnabled(true);
 		tableGraph.setColumnSelectionAllowed(true);
-
-		/*
-		 * End of right container setup
-		 */
-		scrollProperties.setViewportView(tableProperties);
-		tableHolonElementScrollPane.setViewportView(tableHolonElement);
 		scrollGraph.setViewportView(unitGraph);
 		graphLabel.setLayout(new BorderLayout(0, 10));
 		graphLabel.add(maxGraph, BorderLayout.NORTH);
 		graphLabel.add(medGraph, BorderLayout.CENTER);
 		graphLabel.add(minGraph, BorderLayout.SOUTH);
 		toolBarGraph.add(elementGraph);
-		// comboBoxGraph.setModel(new DefaultComboBoxModel(new String[] { "Day",
-		// "Month", "Year" }));
-		// toolBarGraph.add(comboBoxGraph);
 		toolBarGraph.add(resetGraphBtn);
 		toolBarGraph.setFloatable(false);
 		scrollGraph.setRowHeaderView(graphLabel);
 		scrollGraph.setColumnHeaderView(toolBarGraph);
-		scrollElements.setLayout(new BorderLayout(0, 0));
-		scrollElements.add(panel_HolonEl, BorderLayout.NORTH);
-		scrollElements.add(tableHolonElementScrollPane);
-
-		panel_HolonEl.setLayout(new BoxLayout(panel_HolonEl, BoxLayout.X_AXIS));
-		toolBarHolonEl.setFloatable(false);
 
-		panel_HolonEl.add(toolBarHolonEl);
-		toolBarHolonEl.add(btnAddHolEL);
+		/***********************
+		 * HolonElement Table Actions
+		 **********************/
+		/*
+		 * Add HolonElement to given HolonObject
+		 */
 		btnAddHolEL.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 				CpsObject tempCpsObject = getActualCps();
 				if (tempCpsObject != null && tempCpsObject.getClass() == HolonObject.class
 						&& tempCpsObject.getID() != 0) {
 					addElementPopUp = new AddElementPopUp();
+					addElementPopUp.setActualCps(getActualCps());
 					addElementPopUp.setVisible(true);
-					controller.addElementCanvasObject(tempCpsObject.getID(), addElementPopUp.getElement().getEleName(),
-							addElementPopUp.getElement().getAmount(), addElementPopUp.getElement().getEnergy());
+					HolonElement ele = addElementPopUp.getElement();
+					controller.addElementCanvasObject(tempCpsObject.getID(), ele.getEleName(), ele.getAmount(),
+							ele.getEnergy());
 					refreshTableHolonElement();
 					refreshTableProperties();
 				}
 			}
 		});
-		resetGraphBtn.addActionListener(new ActionListener() {
-			public void actionPerformed(ActionEvent arg0) {
-				unitGraph.reset();
-			}
-		});
-		toolBarHolonEl.add(btnDelHolEL);
+		/*
+		 * Delete the choosen HolonElement of the selected HolonObject
+		 */
 		btnDelHolEL.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 				if (getActualCps().getClass() == HolonObject.class) {
@@ -347,7 +350,9 @@ public class GUI<E> implements CategoryListener {
 				}
 			}
 		});
-
+		/*
+		 * Communication between HolonElement Table and displayed Graph
+		 */
 		tableHolonElement.addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {
 				HolonObject obj = (HolonObject) getActualCps();
@@ -359,13 +364,70 @@ public class GUI<E> implements CategoryListener {
 				} else {
 					elementGraph.setText("None ");
 				}
+				// if any HolonElement is double-clicked --> Edit instance of
+				// selected HolonElement
 				if (e.getClickCount() == 2) {
 					yTHIS = e.getY();
 					xTHIS = e.getX();
 				}
+
+				yBTHIS = e.getY();
+				xBTHIS = e.getX();
+
+			}
+		});
+		/*
+		 * If the HolonElement Table enters to editing instance, than is the
+		 * propertyChangeListener triggered
+		 */
+		tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
+			@Override
+			public void propertyChange(PropertyChangeEvent evt) {
+				try {
+					int yMouse = yTHIS;
+					int yBMouse = yBTHIS;
+					int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 4));
+					int selectedValueY = (int) Math.floor(yMouse / 16);
+					int selectedValueBX = (int) Math.floor(xBTHIS / (tableHolonElement.getWidth() / 4));
+					int selectedValueBY = (int) Math.floor(yBMouse / 16);
+					if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
+						if (selectedValueBX == 3) {
+							HolonElement eleBTemp = getActualHolonElement((HolonObject) getActualCps(), yBMouse);
+							String newBStuff = tableModelHolonElement.getValueAt(selectedValueBY, selectedValueBX)
+									.toString();
+							Boolean bTemp = Boolean.parseBoolean(newBStuff);
+							eleBTemp.setActive(bTemp);
+						} else {
+							HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
+							String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX)
+									.toString();
+							if (selectedValueX == 0) {
+								eleTemp.setEleName(newStuff);
+							} else if (selectedValueX == 1) {
+								Float ftemp = Float.parseFloat(newStuff);
+								eleTemp.setEnergy(ftemp);
+							} else if (selectedValueX == 2) {
+								Integer iTemp = Integer.parseInt(newStuff);
+								eleTemp.setAmount(iTemp);
+							}
+						}
+						refreshTableProperties();
+						tableModelHolonElement.fireTableDataChanged();
+					}
+				} catch (Exception e) {
+
+				}
 			}
 		});
 
+		/***********************
+		 * HolonElement Properties Actions
+		 **********************/
+		/*
+		 * If any value at the Properties Table enters to editing instance, than
+		 * is PropertyChangeListener triggered (For HolonObject, the only value
+		 * to be edited is the name and for CpsEdge the Max.flow)
+		 */
 		tableProperties.addPropertyChangeListener(new PropertyChangeListener() {
 			@Override
 			public void propertyChange(PropertyChangeEvent evt) {
@@ -385,32 +447,20 @@ public class GUI<E> implements CategoryListener {
 			}
 		});
 
-		tableHolonElement.addPropertyChangeListener(new PropertyChangeListener() {
-			@Override
-			public void propertyChange(PropertyChangeEvent evt) {
-				try {
-					int yMouse = yTHIS;
-					int selectedValueX = (int) Math.floor(xTHIS / (tableHolonElement.getWidth() / 3));
-					int selectedValueY = (int) Math.floor(yMouse / 16);
-					if (getActualCps() != null && getActualCps().getClass() == HolonObject.class) {
-						HolonElement eleTemp = getActualHolonElement((HolonObject) getActualCps(), yMouse);
-						String newStuff = tableModelHolonElement.getValueAt(selectedValueY, selectedValueX).toString();
-						if (selectedValueX == 0) {
-							eleTemp.setEleName(newStuff);
-						} else if (selectedValueX == 1) {
-							Float ftemp = Float.parseFloat(newStuff);
-							eleTemp.setEnergy(ftemp);
-						} else if (selectedValueX == 2) {
-							Integer iTemp = Integer.parseInt(newStuff);
-							eleTemp.setAmount(iTemp);
-						}
-						tableModelHolonElement.fireTableDataChanged();
-					}
-				} catch (Exception e) {
-
-				}
+		/***********************
+		 * HolonElement Graph Actions
+		 **********************/
+		/*
+		 * Reset the graph of the selected HolonElement
+		 */
+		resetGraphBtn.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent arg0) {
+				unitGraph.reset();
 			}
 		});
+		/*****************************
+		 * RIGHT CONTAINER DONE
+		 *****************************/
 
 		frmCyberPhysical.getContentPane().setLayout(new BorderLayout(0, 0));
 
@@ -488,6 +538,13 @@ public class GUI<E> implements CategoryListener {
 			}
 		});
 
+		popmenuEdit.add(editItem);
+		editItem.setEnabled(false);
+		editItem.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+			}
+		});
 		tree.addMouseListener(new MouseAdapter() {
 			public void mousePressed(MouseEvent e) {
 				try {
@@ -500,28 +557,37 @@ public class GUI<E> implements CategoryListener {
 						CpsObject selected = controller.searchObjInCat(selectedNode.toString(),
 								selectedNode.getParent().toString());
 						deleteRows();
-//						if (selected instanceof HolonObject && selected != null) {
-//							selected = (HolonObject) selected;
-//							fillElementTable(((HolonObject) selected).getElements());
-//						}
+						// if (selected instanceof HolonObject && selected !=
+						// null) {
+						// selected = (HolonObject) selected;
+						// fillElementTable(((HolonObject)
+						// selected).getElements());
+						// }
 					}
-					for (Category cat : model.getCategories()) {
-						for (CpsObject cps : cat.getObjects()) {
-							if (actualObjectClicked.compareTo(cps.getCompareName()) == 0) {
-								File checkPath = new File(cps.getImage());
-								if (checkPath.exists()) {
-									img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
-											controller.getScale(), controller.getScale(), java.awt.Image.SCALE_SMOOTH);
-								} else {
-									img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
-											.getScaledInstance(controller.getScale(), controller.getScale(),
-													java.awt.Image.SCALE_SMOOTH);
+					if (SwingUtilities.isRightMouseButton(e)) {
+						editItem.setEnabled(true);
+						System.out.println("HERE");
+						popmenuEdit.show(e.getComponent(), e.getX(), e.getY());
+					} else {
+						for (Category cat : model.getCategories()) {
+							for (CpsObject cps : cat.getObjects()) {
+								if (actualObjectClicked.compareTo(cps.getCompareName()) == 0) {
+									File checkPath = new File(cps.getImage());
+									if (checkPath.exists()) {
+										img = new ImageIcon(cps.getImage()).getImage().getScaledInstance(
+												controller.getScale(), controller.getScale(),
+												java.awt.Image.SCALE_SMOOTH);
+									} else {
+										img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage()
+												.getScaledInstance(controller.getScale(), controller.getScale(),
+														java.awt.Image.SCALE_SMOOTH);
+									}
+									tempCps = cps;
+									dragging = true;
+									Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
+											"Image");
+									frmCyberPhysical.setCursor(cursor);
 								}
-								tempCps = cps;
-								dragging = true;
-								Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(img, new Point(0, 0),
-										"Image");
-								frmCyberPhysical.setCursor(cursor);
 							}
 						}
 					}
@@ -901,23 +967,24 @@ public class GUI<E> implements CategoryListener {
 		}
 	}
 
-	/*
-	 * Refresh the Table displaying the HolonElements
+	/**
+	 * Update the HolonElement Table, that means erase all rows and add the new
+	 * rows with new data
 	 */
 	public void refreshTableHolonElement() {
 		// Update of the Information about the HolonElements - only for
 		// HolonObjects
+
 		deleteRows();
 		if (getActualCps() != null) {
 			fillElementTable(((HolonObject) getActualCps()).getElements());
-		} /**
-			 * hinzugef�gt damit man auch nach dem objekt platziert wurde
-			 * elemente von Objekten in Kategorien ansehen kann
-			 */
+		}
 		tableModelHolonElement.fireTableDataChanged();
-
 	}
 
+	/**
+	 * Erase all information of the HolonElement Model
+	 */
 	public void deleteRows() {
 		if (tableModelHolonElement.getRowCount() > 0) {
 			for (int i = tableModelHolonElement.getRowCount() - 1; i > -1; i--) {
@@ -926,15 +993,22 @@ public class GUI<E> implements CategoryListener {
 		}
 	}
 
+	/**
+	 * Add the Information of the given ArrayList in the HolonElement Model as
+	 * Name,Energy and Amount
+	 * 
+	 * @param elements
+	 *            ArrayList to be displayed
+	 */
 	public void fillElementTable(ArrayList<HolonElement> elements) {
 		for (HolonElement he : elements) {
-			Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount() };
+			Object[] temp = { he.getEleName(), he.getEnergy(), he.getAmount(), he.getActive() };
 			tableModelHolonElement.addRow(temp);
 		}
 	}
 
 	/**
-	 * Update the information about properties of the selected CpsObject
+	 * Update the information concerning properties of the selected CpsObject
 	 */
 	public void refreshTableProperties() {
 		CpsObject tempCps = getActualCps();

+ 10 - 10
src/ui/view/MyCanvas.java

@@ -364,8 +364,8 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				}
 				if (newEdge) {
 					e = new CpsEdge(cps, tempCps);
-					cps.AddConnection(e);
-					tempCps.AddConnection(e);
+//					cps.AddConnection(e);
+//					tempCps.AddConnection(e);
 					controller.AddEdgeOnCanvas(e);
 				}
 			}
@@ -390,16 +390,16 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 				k = p.getB();
 
 				e = new CpsEdge(n, tempCps);
-				n.AddConnection(e);
-				tempCps.AddConnection(e);
+//				n.AddConnection(e);
+//				tempCps.AddConnection(e);
 
 				e1 = new CpsEdge(n, r);
-				n.AddConnection(e1);
-				r.AddConnection(e1);
+//				n.AddConnection(e1);
+//				r.AddConnection(e1);
 
 				e2 = new CpsEdge(n, k);
-				n.AddConnection(e2);
-				k.AddConnection(e2);
+//				n.AddConnection(e2);
+//				k.AddConnection(e2);
 
 				p.getA().getConnections().remove(p);
 				p.getB().getConnections().remove(p);
@@ -421,8 +421,8 @@ class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
 
 			e = new CpsEdge(n, tempCps);
 
-			n.AddConnection(e);
-			tempCps.AddConnection(e);
+//			n.AddConnection(e);
+//			tempCps.AddConnection(e);
 			controller.AddEdgeOnCanvas(e);
 			System.out.println("node ID: " + n.getID());
 		}

+ 19 - 0
src/ui/view/PropertyTable.java

@@ -0,0 +1,19 @@
+package ui.view;
+
+import java.util.Vector;
+
+import javax.swing.table.DefaultTableModel;
+
+public class PropertyTable extends DefaultTableModel {
+
+	@Override
+	public Class<?> getColumnClass(int columnIndex) {
+		Class clazz = String.class;
+		switch (columnIndex) {
+		case 3:
+			clazz = Boolean.class;
+			break;
+		}
+		return clazz;
+	}
+}