LoadController.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package ui.controller;
  2. import java.awt.Point;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Collections;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. import org.json.simple.JSONArray;
  10. import org.json.simple.JSONObject;
  11. import org.json.simple.parser.JSONParser;
  12. import org.json.simple.parser.ParseException;
  13. import classes.Category;
  14. import classes.CpsEdge;
  15. import classes.CpsNode;
  16. import classes.CpsObject;
  17. import classes.HolonElement;
  18. import classes.HolonObject;
  19. import classes.HolonSwitch;
  20. import classes.HolonTransformer;
  21. import ui.model.Model;
  22. import ui.model.idCounter;
  23. public class LoadController {
  24. public enum MODE {
  25. ALL, CATEGORY, CANVAS
  26. }
  27. private Model MODEL;
  28. private CategoryController cgC;
  29. private CanvasController cvsC;
  30. private ObjectController objC;
  31. private MultiPurposeController mpC;
  32. public LoadController(Model model, CategoryController cg, CanvasController cvs, ObjectController obj,
  33. MultiPurposeController mp) {
  34. this.MODEL = model;
  35. this.cgC = cg;
  36. this.cvsC = cvs;
  37. this.objC = obj;
  38. this.mpC = mp;
  39. }
  40. /**
  41. * Reads the the JSON File and load the state into the Model
  42. *
  43. * @param path
  44. * @throws IOException
  45. */
  46. public void readJSON(String path) throws IOException {
  47. JSONParser parser = new JSONParser();
  48. JSONObject json = new JSONObject();
  49. try {
  50. json = (JSONObject) parser.parse(new FileReader(path));
  51. } catch (ParseException e1) {
  52. // TODO Auto-generated catch block
  53. e1.printStackTrace();
  54. }
  55. ArrayList<String> obj = new ArrayList<>();
  56. ArrayList<String> ele = new ArrayList<>();
  57. ArrayList<String> edge = new ArrayList<>();
  58. ArrayList<String> gp = new ArrayList<>();
  59. MODE mode = MODE.valueOf(json.get("MODE").toString());
  60. if (mode.equals(MODE.ALL) || mode.equals(MODE.CATEGORY)) {
  61. MODEL.setCgIdx(new HashMap<String, Integer>());
  62. MODEL.setCategories(new ArrayList<Category>());
  63. }
  64. if (mode.equals(MODE.ALL) || mode.equals(MODE.CANVAS)) {
  65. MODEL.setCvsObjIdx(new HashMap<Integer, Integer>());
  66. MODEL.setObjectsOnCanvas(new ArrayList<CpsObject>());
  67. MODEL.setEdgesOnCanvas(new ArrayList<CpsEdge>());
  68. }
  69. for (Object key : json.keySet()) {
  70. if (key.equals("CG"))
  71. readCategory((JSONArray) json.get(key));
  72. else if (key.equals("ID"))
  73. idCounter.setCounter(Integer.parseInt(json.get(key.toString()).toString()));
  74. else if (key.toString().contains("CGO") || key.toString().contains("CVSO"))
  75. obj.add(key.toString());
  76. else if (key.toString().contains("CGE") || key.toString().contains("CVSE"))
  77. ele.add(key.toString());
  78. else if (key.toString().contains("EDGE"))
  79. edge.add(key.toString());
  80. else if (key.toString().contains("CGGP") || key.toString().contains("CVSGP"))
  81. gp.add(key.toString());
  82. }
  83. Collections.sort(obj);
  84. Collections.sort(ele);
  85. Collections.sort(edge);
  86. Collections.sort(gp);
  87. dispatch(obj, json);
  88. dispatch(ele, json);
  89. dispatch(edge, json);
  90. dispatch(gp, json);
  91. }
  92. /**
  93. * dispatch the Keys into the right processing
  94. *
  95. * @param input
  96. * @param json
  97. */
  98. public void dispatch(ArrayList<String> input, JSONObject json) {
  99. for (String str : input) {
  100. if (str.contains("CGO")) {
  101. readCategoryObject((JSONArray) json.get(str));
  102. } else if (str.contains("CVSO")) {
  103. readCanvasObject((JSONArray) json.get(str));
  104. } else if (str.contains("CGE") || str.contains("CVSE")) {
  105. readElement((JSONArray) json.get(str));
  106. } else if (str.contains("EDGE")) {
  107. readEdge((JSONArray) json.get(str));
  108. } else if (str.contains("CGGP") || str.contains("CVSGP")) {
  109. readElementGraph((JSONArray) json.get(str));
  110. }
  111. }
  112. }
  113. /**
  114. * Read all Categories from the JSON file
  115. *
  116. * @param arr
  117. */
  118. public void readCategory(JSONArray arr) {
  119. Iterator<Object> i = arr.iterator();
  120. while (i.hasNext()) {
  121. cgC.addNewCategory(next(i));
  122. }
  123. }
  124. /**
  125. * Read all Objects in Category from the JSON File
  126. *
  127. * @param arr
  128. */
  129. public void readCategoryObject(JSONArray arr) {
  130. Iterator<Object> i = arr.iterator();
  131. String type = next(i);
  132. if (type.equals("HolonObject")) {
  133. cgC.addNewHolonObject(mpC.searchCat(next(i)), next(i), new ArrayList<HolonElement>(), next(i));
  134. } else if (type.equals("HolonTransformer")) {
  135. cgC.addNewHolonTransformer(mpC.searchCat(next(i)), next(i), next(i));
  136. } else if (type.equals("HolonSwitch")) {
  137. cgC.addNewHolonSwitch(mpC.searchCat(next(i)), next(i), next(i));
  138. }
  139. }
  140. /**
  141. * Read all Objects in Canvas from JSON File
  142. *
  143. * @param arr
  144. */
  145. public void readCanvasObject(JSONArray arr) {
  146. Iterator<Object> i = arr.iterator();
  147. CpsObject cps = null;
  148. String type = next(i);
  149. if (type.equals("HolonObject")) {
  150. cps = new HolonObject(next(i));
  151. } else if (type.equals("HolonTransformer")) {
  152. cps = new HolonTransformer(next(i));
  153. } else if (type.equals("HolonSwitch")) {
  154. cps = new HolonSwitch(next(i));
  155. } else if (type.equals("CpsNode")) {
  156. cps = new CpsNode(next(i));
  157. }
  158. cps.setName(next(i));
  159. cps.setID(Integer.parseInt(next(i)));
  160. cps.setImage(next(i));
  161. cps.setPosition(Integer.parseInt(next(i)), Integer.parseInt(next(i)));
  162. cvsC.addNewObject(cps);
  163. }
  164. /**
  165. * Read all Elements in Category and Canvas from JSON File
  166. *
  167. * @param arr
  168. */
  169. public void readElement(JSONArray arr) {
  170. Iterator<Object> i = arr.iterator();
  171. String sav = next(i);
  172. String obj = next(i);
  173. HolonElement ele = new HolonElement(next(i), Integer.parseInt(next(i)), Float.parseFloat(next(i)));
  174. if (sav.equals("CVS")) {
  175. ele.setActive(convert(Integer.parseInt(next(i))));
  176. objC.addElementIntoCanvasObject((HolonObject) mpC.searchByID(Integer.parseInt(obj)), ele);
  177. } else
  178. objC.addElementIntoCategoryObject(sav, obj, ele);
  179. }
  180. /**
  181. *
  182. * @param arr
  183. */
  184. public void readEdge(JSONArray arr) {
  185. Iterator<Object> i = arr.iterator();
  186. CpsEdge edge = new CpsEdge(mpC.searchByID(Integer.parseInt(next(i))),
  187. mpC.searchByID(Integer.parseInt(next(i))));
  188. edge.setCapacity(Float.parseFloat(next(i)));
  189. edge.setFlow(Float.parseFloat(next(i)));
  190. cvsC.addEdgeOnCanvas(edge);
  191. }
  192. public void readElementGraph(JSONArray arr) {
  193. Iterator<Object> i = arr.iterator();
  194. String sav = next(i);
  195. HolonElement ele;
  196. if (sav.equals("CVS")) {
  197. ele = mpC.searchEle((HolonObject) mpC.searchByID(Integer.parseInt(next(i))), next(i));
  198. while (i.hasNext())
  199. ele.getGraphPoints().add(new Point(Integer.parseInt(next(i)), Integer.parseInt(next(i))));
  200. } else {
  201. ele = mpC.searchEle((HolonObject) mpC.searchCatObj(mpC.searchCat(sav), next(i)),
  202. next(i));
  203. while (i.hasNext())
  204. ele.getGraphPoints().add(new Point(Integer.parseInt(next(i)), Integer.parseInt(next(i))));
  205. }
  206. }
  207. /**
  208. * Get the Next Element from an Iterator
  209. *
  210. * @param i
  211. * @return
  212. */
  213. public String next(Iterator<Object> i) {
  214. return i.next().toString();
  215. }
  216. /**
  217. * converting saved Integers into Booleans
  218. *
  219. * @param x
  220. * @return
  221. */
  222. public boolean convert(int x) {
  223. return x == 1 ? true : false;
  224. }
  225. }