|
@@ -1,5 +1,6 @@
|
|
|
package ui.controller;
|
|
|
|
|
|
+import java.awt.Color;
|
|
|
import java.awt.Point;
|
|
|
import java.io.FileReader;
|
|
|
import java.io.IOException;
|
|
@@ -7,12 +8,23 @@ import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collector;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
-import org.json.simple.JSONArray;
|
|
|
-import org.json.simple.JSONObject;
|
|
|
import org.json.simple.parser.JSONParser;
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.GsonBuilder;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParser;
|
|
|
+import com.google.gson.stream.JsonReader;
|
|
|
+
|
|
|
+import TypeAdapter.AbstractCpsObjectAdapter;
|
|
|
+import TypeAdapter.ColorAdapter;
|
|
|
+import TypeAdapter.PositionAdapter;
|
|
|
import classes.Category;
|
|
|
import classes.CpsEdge;
|
|
|
import classes.CpsNode;
|
|
@@ -22,6 +34,7 @@ import classes.HolonObject;
|
|
|
import classes.HolonSwitch;
|
|
|
import classes.HolonTransformer;
|
|
|
import classes.IdCounter;
|
|
|
+import classes.Position;
|
|
|
import ui.model.Model;
|
|
|
|
|
|
|
|
@@ -33,16 +46,34 @@ public class LoadController {
|
|
|
|
|
|
* enum Mode.
|
|
|
*/
|
|
|
- public enum Mode {
|
|
|
- ALL, CATEGORY, CANVAS
|
|
|
+ public enum MODE {
|
|
|
+ COMPLETE, PARTIAL
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum TYPE {
|
|
|
+ CATEGORY, CANVAS
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum EDGETYPE {
|
|
|
+ CANVAS, CONNECTION, NODE, OLD
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum NUMTYPE {
|
|
|
+ CATEGORY, OBJECT, ELEMENT, EDGE, CONNECTION, NODEEDGE, OLDEDGE, UNITGRAPH
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum GRAPHTYPE {
|
|
|
+ SWITCH, ELEMENT
|
|
|
}
|
|
|
|
|
|
private Model model;
|
|
|
private CategoryController cgC;
|
|
|
private CanvasController cvsC;
|
|
|
private ObjectController objC;
|
|
|
- private GlobalController global;
|
|
|
private MultiPurposeController mpC;
|
|
|
+ private Gson gson;
|
|
|
+
|
|
|
+ ArrayList<String> jCat, jObj, jEle, jEdge, jConn, jNodeEdge, jOldEdge, jUnitGraph;
|
|
|
|
|
|
|
|
|
* Constructor.
|
|
@@ -58,15 +89,13 @@ public class LoadController {
|
|
|
* @param mp
|
|
|
* MultiPurposeController
|
|
|
*/
|
|
|
- public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj,
|
|
|
- GlobalController global, MultiPurposeController mp) {
|
|
|
+ public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj, MultiPurposeController mp) {
|
|
|
this.model = model;
|
|
|
this.cgC = cg;
|
|
|
this.cvsC = cvs;
|
|
|
this.objC = obj;
|
|
|
- this.global = global;
|
|
|
this.mpC = mp;
|
|
|
-
|
|
|
+ initGson();
|
|
|
}
|
|
|
|
|
|
|
|
@@ -78,62 +107,88 @@ public class LoadController {
|
|
|
* exception
|
|
|
*/
|
|
|
public void readJson(String path) throws IOException {
|
|
|
- JSONParser parser = new JSONParser();
|
|
|
- JSONObject json = new JSONObject();
|
|
|
- try {
|
|
|
- json = (JSONObject) parser.parse(new FileReader(path));
|
|
|
- } catch (ParseException ex) {
|
|
|
-
|
|
|
- ex.printStackTrace();
|
|
|
+
|
|
|
+ JsonParser parser = new JsonParser();
|
|
|
+ JsonObject json = (JsonObject) parser.parse(new FileReader(path));
|
|
|
+
|
|
|
+
|
|
|
+ List<String> keys = json.entrySet().stream().map(i -> i.getKey())
|
|
|
+ .collect(Collectors.toCollection(ArrayList::new));
|
|
|
+ initialize(keys, json);
|
|
|
+ distribute(keys, json, parser);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void distribute(List<String> keys, JsonObject json, JsonParser parser) {
|
|
|
+
|
|
|
+ for (String key : keys) {
|
|
|
+ if(key.contains("CATEGORY"))
|
|
|
+ loadCategory(json.get(key));
|
|
|
+ if(key.contains("CGOBJECT"))
|
|
|
+ loadCategoryObject(TYPE.CATEGORY, json.get(key).getAsJsonObject());
|
|
|
}
|
|
|
|
|
|
- ArrayList<String> obj = new ArrayList<>();
|
|
|
- ArrayList<String> ele = new ArrayList<>();
|
|
|
- ArrayList<String> edge = new ArrayList<>();
|
|
|
- ArrayList<String> gp = new ArrayList<>();
|
|
|
|
|
|
- Mode mode = Mode.valueOf(json.get("MODE").toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ private void loadCategoryObject(TYPE type, JsonObject jsonElement) {
|
|
|
+
|
|
|
+ AbstractCpsObject temp = gson.fromJson(jsonElement., AbstractCpsObject.class);
|
|
|
+ cgC.addObject(mpC.searchCat(temp.getSav()), temp);
|
|
|
+ }
|
|
|
|
|
|
- if (mode.equals(Mode.ALL) || mode.equals(Mode.CATEGORY)) {
|
|
|
+ private void loadCategory(JsonElement jsonElement) {
|
|
|
+
|
|
|
+ cgC.addCategory(new Category(jsonElement.getAsString()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initialize(List<String> keys, JsonObject json) {
|
|
|
+
|
|
|
+ switch (MODE.valueOf(json.get("MODE").getAsString())) {
|
|
|
+ case COMPLETE:
|
|
|
model.setCgIdx(new HashMap<String, Integer>());
|
|
|
model.setCategories(new ArrayList<Category>());
|
|
|
- }
|
|
|
- if (mode.equals(Mode.ALL) || mode.equals(Mode.CANVAS)) {
|
|
|
model.setCvsObjIdx(new HashMap<Integer, Integer>());
|
|
|
model.setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
|
|
|
model.setEdgesOnCanvas(new ArrayList<CpsEdge>());
|
|
|
- }
|
|
|
-
|
|
|
- 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());
|
|
|
+ model.setCanvasX(json.get("CANVAS_SIZE_X").getAsInt());
|
|
|
+ model.setCanvasX(json.get("CANVAS_SIZE_Y").getAsInt());
|
|
|
+ break;
|
|
|
+ case PARTIAL:
|
|
|
+ model.setCvsObjIdx(new HashMap<Integer, Integer>());
|
|
|
+ model.setObjectsOnCanvas(new ArrayList<AbstractCpsObject>());
|
|
|
+ model.setEdgesOnCanvas(new ArrayList<CpsEdge>());
|
|
|
+ break;
|
|
|
|
|
|
+ default:
|
|
|
+ break;
|
|
|
}
|
|
|
-
|
|
|
- Collections.sort(obj);
|
|
|
- Collections.sort(ele);
|
|
|
- Collections.sort(edge);
|
|
|
- Collections.sort(gp);
|
|
|
-
|
|
|
- dispatch(obj, json);
|
|
|
- dispatch(ele, json);
|
|
|
- dispatch(edge, json);
|
|
|
- dispatch(gp, json);
|
|
|
-
|
|
|
+ IdCounter.setCounter(json.get("IDCOUNTER").getAsInt());
|
|
|
}
|
|
|
|
|
|
|
|
@@ -311,5 +366,23 @@ public class LoadController {
|
|
|
public boolean convert(int x) {
|
|
|
return x == 1 ? true : false;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ * Initialize the Gson with wanted parameters
|
|
|
+ */
|
|
|
+ private void initGson() {
|
|
|
+
|
|
|
+ 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());
|
|
|
+
|
|
|
+ this.gson = builder.create();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initArrays() {
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|