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