ソースを参照

changes for saving

Teh-Hai Julian Zheng 8 年 前
コミット
60b6ec0c2b

+ 1 - 1
src/classes/CpsNode.java

@@ -11,7 +11,7 @@ public class CpsNode extends CpsObject {
 		this.setConnections(new ArrayList<CpsEdge>());
 		this.setImage("/Images/node.png");
 		this.setID(idCounter.nextId());
-		this.setStored("Canvas");
+		this.setSav("Canvas");
 	}
 
 }

+ 5 - 5
src/classes/CpsObject.java

@@ -20,7 +20,7 @@ public abstract class CpsObject implements ComparableObject {
 	Position position;
 	/* Energy input and output of each object in the grid */
 	
-	String stored;
+	String sav;
 	
 	float energyIn;
 	float energyOut;
@@ -125,15 +125,15 @@ public abstract class CpsObject implements ComparableObject {
 	/**
 	 * @return the stored
 	 */
-	public String getStored() {
-		return stored;
+	public String getSav() {
+		return sav;
 	}
 
 	/**
 	 * @param stored the stored to set
 	 */
-	public void setStored(String stored) {
-		this.stored = stored;
+	public void setSav(String sav) {
+		this.sav = sav;
 	}
 
 	/* Getter and Setters for the energy input and output */

+ 20 - 5
src/classes/HolonElement.java

@@ -17,7 +17,8 @@ public class HolonElement {
 	/* +: for Consumers and -: Producers */
 	char sign;
 	
-	String stored;
+	String sav;
+	String obj;
 	/*
 	 * Energy at each point of the graph with 50 predefined points. At the
 	 * beginning, it starts with all values at energy
@@ -164,15 +165,29 @@ public class HolonElement {
 	/**
 	 * @return the stored
 	 */
-	public String getStored() {
-		return stored;
+	public String getSav() {
+		return sav;
 	}
 
 	/**
 	 * @param stored the stored to set
 	 */
-	public void setStored(String stored) {
-		this.stored = stored;
+	public void setSav(String sav) {
+		this.sav = sav;
+	}
+
+	/**
+	 * @return the obj
+	 */
+	public String getObj() {
+		return obj;
+	}
+
+	/**
+	 * @param obj the obj to set
+	 */
+	public void setObj(String obj) {
+		this.obj = obj;
 	}
 
 }

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

@@ -30,19 +30,19 @@ public class CategoryController {
 
 		HolonObject powerp = new HolonObject("Power Plant");
 		powerp.setImage("/Images/power-plant.png");
-		powerp.setStored("Energy");
+		powerp.setSav("Energy");
 		
 		HolonObject house = new HolonObject("House");
 		house.setImage("/Images/home.png");
-		house.setStored("Building");
+		house.setSav("Building");
 
 		HolonTransformer transformer = new HolonTransformer("Transformer");
 		transformer.setImage("/Images/transformer.png");
-		transformer.setStored("Component");
+		transformer.setSav("Component");
 		
 		HolonSwitch sw = new HolonSwitch("Switch");
 		sw.setImage("/Images/switch-on.png");
-		sw.setStored("Component");
+		sw.setSav("Component");
 
 		addObject(energy, powerp);
 		addObject(building, house);

+ 81 - 16
src/ui/controller/LoadStoreController.java

@@ -11,6 +11,8 @@ import classes.Category;
 import classes.CpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.HolonTransformer;
 import ui.model.Model;
 
 public class LoadStoreController {
@@ -20,63 +22,126 @@ public class LoadStoreController {
 		this.MODEL = model;
 	}
 
-	
-	
+	/**
+	 * Writes a Savefile in .json
+	 * 
+	 * @throws IOException
+	 */
 	public void writeJSONFile() throws IOException {
 
 		JSONObject json = new JSONObject();
 
-		
 		writeCategory(json);
-		writeCategoryObjects(json);
-		writeCategoryElements(json);
+		writeObjects(json);
+		writeElements(json);
 
 		FileWriter writer = new FileWriter("//Users//zheng//Desktop//Tesst.json");
+		writer.write(json.toJSONString());
 		writer.flush();
 		writer.close();
 	}
 
-	public void writeCategory(JSONObject json) throws IOException {
+	/**
+	 * 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("Category", arr);
 	}
 
-	public void writeCategoryObjects(JSONObject json) throws IOException {
-		
+	/**
+	 * 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(cps.getStored());
+				arr.add(cps.getSav());
 				arr.add(cps.getObjName());
-				arr.add(cps.getName());
 				arr.add(cps.getImage());
 				json.put("CategoryObject." + i++, arr);
 				arr = new JSONArray();
 			}
+
+		i = 1;
+		for (CpsObject cps : MODEL.getObjectsOnCanvas()) {
+			arr.add(getObjectType(cps));
+			arr.add(cps.getSav());
+			arr.add(cps.getObjName());
+			arr.add(cps.getID());
+			arr.add(cps.getImage());
+			arr.add(cps.getPosition().x);
+			arr.add(cps.getPosition().y);
+			json.put("CanvasObject." + i++, arr);
+			arr = new JSONArray();
+
+		}
+	}
+
+	/**
+	 * 
+	 * @param cps
+	 * @return
+	 */
+	public String getObjectType(CpsObject cps) {
+		if (cps instanceof HolonObject)
+			return "HolonObject";
+		else if (cps instanceof HolonTransformer)
+			return "HolonTransformer";
+		else if (cps instanceof HolonSwitch)
+			return "HolonSwtich";
+		else
+			return "CpsNode";
 	}
 
-	public void writeCategoryElements(JSONObject json) throws IOException {
-		
+	/**
+	 * 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.getStored());
+						arr.add(ele.getSav());
+						arr.add(ele.getObj());
 						arr.add(ele.getEleName());
 						arr.add(ele.getAmount());
 						arr.add(ele.getEnergy());
 						json.put("CategoryElement." + 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(ele.getObj());
+					arr.add(ele.getEleName());
+					arr.add(ele.getAmount());
+					arr.add(ele.getEnergy());
+					json.put("CategoryElement." + i++, arr);
+					arr = new JSONArray();
+				}
+		}
 	}
 
 	public void readFromJSON(File jsonFile) throws IOException {

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

@@ -75,7 +75,7 @@ public class ObjectController {
 				break;
 			}
 		}
-		element.setStored(object);
+		element.setSav(object);
 		addElement(searchHolonObject(object, cat.getObjects()), element);
 	}
 

+ 10 - 3
src/ui/view/GUI.java

@@ -1,6 +1,7 @@
 package ui.view;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
@@ -310,9 +311,8 @@ public class GUI implements CategoryListener {
 						&& tempCpsObject.getID() != 0) {
 					addElementPopUp = new AddElementPopUp();
 					addElementPopUp.setVisible(true);
-					controller.addElementCanvasObject(tempCpsObject.getName(),
-							addElementPopUp.getElement().getEleName(), addElementPopUp.getElement().getAmount(),
-							addElementPopUp.getElement().getEnergy());
+					controller.addElementCanvasObject(tempCpsObject.getID(), addElementPopUp.getElement().getEleName(),
+							addElementPopUp.getElement().getAmount(), addElementPopUp.getElement().getEnergy());
 				}
 			}
 		});
@@ -408,6 +408,7 @@ public class GUI implements CategoryListener {
 						}
 
 						h.setPosition(x, y);
+						h.setSav("Canvas");
 						controller.addObjectCanvas(h);
 						for (int i = 0; i < model.getObjectsOnCanvas().size(); i++) {
 							CpsObject temp = model.getObjectsOnCanvas().get(i);
@@ -502,6 +503,12 @@ public class GUI implements CategoryListener {
 		aboutUs.addMouseListener(new MouseAdapter() {
 			@Override
 			public void mousePressed(MouseEvent e) {
+				try {
+					controller.writeFile();
+				} catch (IOException e1) {
+					// TODO Auto-generated catch block
+					e1.printStackTrace();
+				}
 				aboutUsPopUp = new AboutUsPopUp();
 				aboutUsPopUp.setVisible(true);
 			}

+ 0 - 2
src/ui/view/Main.java

@@ -32,8 +32,6 @@ public class Main {
 
 					VIEW.getFrmCyberPhysical().setVisible(true);
 
-					// CONTROL.writeFile();
-
 				} catch (Exception e) {
 					e.printStackTrace();
 				}