Browse Source

Objects into clipboard works. Edges not finished

Teh-Hai Julian Zheng 8 years ago
parent
commit
216bd54fb5

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

@@ -312,7 +312,7 @@ public class CanvasController {
 			queue.add(edge);
 		}
 		while (!queue.isEmpty()) {
-			e = queue.poll();
+			e = queue.pop();
 			e.getA().getConnections().remove(e);
 			e.getB().getConnections().remove(e);
 		}

+ 150 - 3
src/ui/controller/ClipboardController.java

@@ -1,11 +1,158 @@
 package ui.controller;
 
+import java.awt.Color;
+import java.awt.Toolkit;
+import java.awt.datatransfer.Clipboard;
+import java.awt.datatransfer.StringSelection;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonPrimitive;
+
+import TypeAdapter.AbstractCpsObjectAdapter;
+import TypeAdapter.ColorAdapter;
+import TypeAdapter.PositionAdapter;
+import classes.AbstractCpsObject;
+import classes.CpsEdge;
+import classes.CpsUpperNode;
+import classes.HolonObject;
+import classes.HolonSwitch;
+import classes.IdCounter;
+import classes.Position;
+import ui.controller.StoreController.EDGETYPE;
+import ui.controller.StoreController.GRAPHTYPE;
+import ui.controller.StoreController.NUMTYPE;
+import ui.controller.StoreController.TYPE;
 import ui.model.Model;
 
 public class ClipboardController {
-	
+
 	private Model model;
-	
-	
+	private StoreController store;
+	private LoadController load;
+	private Gson gson;
+
+	public ClipboardController(Model model, StoreController store, LoadController load) {
+		this.model = model;
+		this.store = store;
+		this.load = load;
+		initGson();
+
+	}
+
+	public void copy(CpsUpperNode upperNode) {
+
+		ArrayList<AbstractCpsObject> foundObj = (upperNode == null ? model.getObjectsOnCanvas() : upperNode.getNodes());
+		ArrayList<CpsEdge> foundedge = (upperNode == null ? model.getEdgesOnCanvas() : upperNode.getNodeEdges());
+
+		JsonObject file = new JsonObject();
+		ArrayDeque<AbstractCpsObject> queue = new ArrayDeque<>();
+		HashMap<Integer, Integer> idMap = new HashMap<>();
+		AbstractCpsObject u = null;
+
+		store.initNumeration();
+
+		for (AbstractCpsObject abs : model.getSelectedCpsObjects()) {
+			queue.add(abs);
+		}
+
+		while (!queue.isEmpty()) {
+
+			u = queue.pop();
+
+			String key = "CVSOBJECT" + store.getNumerator(NUMTYPE.OBJECT);
+			idMapping(u, idMap);
+			file.add(key, gson.toJsonTree(u, AbstractCpsObject.class));
+			if (u instanceof HolonObject)
+				store.elementsToJson(TYPE.CANVAS, file, u);
+
+			if (u instanceof HolonSwitch)
+				if (((HolonSwitch) u).getGraphPoints().size() != 0)
+					store.unitgraphToJson(GRAPHTYPE.SWITCH, file, u.getID(), ((HolonSwitch) u).getGraphPoints());
+
+			if (u instanceof CpsUpperNode) {
+				for (AbstractCpsObject adjacent : ((CpsUpperNode) u).getNodes()) {
+					queue.add(adjacent);
+				}
+			}
+		}
+
+		for (CpsEdge edge : foundedge) {
+			if(model.getSelectedCpsObjects().contains(edge.getA()) && model.getSelectedCpsObjects().contains(edge.getB()))
+				return;
+		}
+
+		StringSelection selection = new StringSelection(gson.toJson(file));
+		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
+		clipboard.setContents(selection, selection);
+
+	}
+
+	private void idMapping(AbstractCpsObject u, HashMap<Integer, Integer> idMap) {
+		// TODO Auto-generated method stub
+		int id = u.getID();
+		u.setID(IdCounter.nextId());
+		idMap.put(id, u.getID());
+	}
+
+	public void paste() {
+
+	}
+
+	private void edgeToJson(EDGETYPE type, JsonObject file, int id, CpsEdge edge) {
+		// TODO Auto-generated method stub
+		String k = null;
+		boolean b = false;
+		JsonObject temp = new JsonObject();
+
+
+			// add properties and only the ids from a and b
+			temp.add("properties", gson.toJsonTree(edge));
+			temp.add("A", new JsonPrimitive(edge.getA().getID()));
+			temp.add("B", new JsonPrimitive(edge.getB().getID()));
+
+			// Key and occasionally the id of Uppernode
+			switch (type) {
+			case CANVAS:
+				k = "CVSEDGE" + store.getNumerator(NUMTYPE.EDGE);
+				break;
+			case CONNECTION:
+				k = "CONNEDGE" + store.getNumerator(NUMTYPE.CONNECTION);
+				break;
+			case NODE:
+				temp.add("ID", new JsonPrimitive(id));
+				k = "NODEEDGE" + store.getNumerator(NUMTYPE.NODEEDGE);
+				break;
+			case OLD:
+				temp.add("ID", new JsonPrimitive(id));
+				k = "OLDEDGE" + store.getNumerator(NUMTYPE.OLDEDGE);
+				break;
+			default:
+				break;
+			}
+			// lookup if the CVS, NODE or OLDEDGE are also connections
+			if (edge.getA().getConnections().contains(edge) && edge.getA().getConnections().contains(edge)
+					&& !type.equals(EDGETYPE.CANVAS))
+				b = true;
+			temp.add("connection", new JsonPrimitive(b));
+			file.add(k, gson.toJsonTree(temp));
+			temp = new JsonObject();
+		
+	}
+
+	private void initGson() {
+		// TODO Auto-generated method stub
+		GsonBuilder builder = new GsonBuilder();
+		builder.excludeFieldsWithoutExposeAnnotation().serializeNulls().setPrettyPrinting();
+		builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter());
+		builder.registerTypeAdapter(Position.class, new PositionAdapter());
+		builder.registerTypeAdapter(Color.class, new ColorAdapter());
+		// use the builder and make a instance of the Gson
+		this.gson = builder.create();
+	}
 
 }

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

@@ -42,6 +42,7 @@ public class Control {
 	private SimulationManager simulationManager;
 	private final StatsController statsController;
 	private final NodeController nodeController;
+	private final ClipboardController clipboardController;
 	private String autoPath = "";
 
 	/**
@@ -66,6 +67,7 @@ public class Control {
 		this.autoSaveController = new AutoSaveController(model);
 		this.consoleController = new ConsoleController(model);
 		this.statsController = new StatsController(model);
+		this.clipboardController = new ClipboardController(model, storeController, loadController);
 
 		autoPath = System.getProperty("user.home") + "/HolonGUI/Autosave/";
 		File dest = new File(autoPath);
@@ -570,7 +572,8 @@ public class Control {
 	 * Copy all Selected Objects.
 	 */
 	public void copyObjects() {
-		canvasController.copyObjects();
+//		canvasController.copyObjects();
+		clipboardController.copy(null);
 	}
 
 	/**

+ 5 - 5
src/ui/controller/StoreController.java

@@ -216,7 +216,7 @@ public class StoreController {
 	 * @param file
 	 * @param obj
 	 */
-	private void elementsToJson(TYPE type, JsonObject file, AbstractCpsObject obj) {
+	public void elementsToJson(TYPE type, JsonObject file, AbstractCpsObject obj) {
 		// TODO Auto-generated method stub
 		JsonObject temp = new JsonObject();
 		String key = null;
@@ -249,7 +249,7 @@ public class StoreController {
 	 * 
 	 * @param ele
 	 */
-	private void unitgraphToJson(GRAPHTYPE type, JsonObject file, int id, LinkedList<Point> graph) {
+	public void unitgraphToJson(GRAPHTYPE type, JsonObject file, int id, LinkedList<Point> graph) {
 
 		JsonObject temp = new JsonObject();
 		String key = null;
@@ -282,7 +282,7 @@ public class StoreController {
 	 * @param id
 	 * @param arr
 	 */
-	private void edgeToJson(EDGETYPE type, JsonObject file, int id, ArrayList<CpsEdge> arr) {
+	public void edgeToJson(EDGETYPE type, JsonObject file, int id, ArrayList<CpsEdge> arr) {
 		// TODO Auto-generated method stub
 		String k = null;
 		boolean b = false;
@@ -341,7 +341,7 @@ public class StoreController {
 	/**
 	 * Just initialize the Numerators for the Json Keys. Maybe bad Style..
 	 */
-	private void initNumeration() {
+	public void initNumeration() {
 		this.nCat = this.nObj = this.nEle = this.nEdge = this.nConn = this.nNodeEdge = this.nOldEdge = 0;
 	}
 
@@ -351,7 +351,7 @@ public class StoreController {
 	 * @param type
 	 * @return
 	 */
-	private int getNumerator(NUMTYPE type) {
+	public int getNumerator(NUMTYPE type) {
 
 		switch (type) {
 		case CATEGORY: