StoreController.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package ui.controller;
  2. import java.awt.Point;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.ListIterator;
  6. import org.json.simple.JSONArray;
  7. import org.json.simple.JSONObject;
  8. import classes.Category;
  9. import classes.CpsEdge;
  10. import classes.AbstractCpsObject;
  11. import classes.HolonElement;
  12. import classes.HolonObject;
  13. import classes.HolonSwitch;
  14. import classes.HolonTransformer;
  15. import classes.IdCounter;
  16. import ui.model.Model;
  17. /**
  18. * Controller for Storage.
  19. *
  20. * @author Gruppe14
  21. */
  22. public class StoreController {
  23. private Model model;
  24. /**
  25. * Constructor.
  26. *
  27. * @param model
  28. * the Model
  29. */
  30. public StoreController(Model model) {
  31. this.model = model;
  32. }
  33. /**
  34. * Writes the current State of the Modelling into a JSON File which can be
  35. * loaded.
  36. *
  37. * @param path
  38. * the Path
  39. *
  40. * @throws IOException
  41. * exception
  42. */
  43. public void writeSaveFile(String path) throws IOException {
  44. JSONObject json = new JSONObject();
  45. json.put("MODE", "ALL");
  46. json.put("ID", IdCounter.getCounter());
  47. json.put("SIZEX", model.getCanvasX());
  48. json.put("SIZEY", model.getCanvasY());
  49. writeCategory(json);
  50. writeCategoryObjects(json);
  51. writeCanvasObjects(json);
  52. writeCategoryElements(json);
  53. writeCanvasElements(json);
  54. writeEdges(json);
  55. writeElementGraph(json);
  56. FileWriter writer = new FileWriter(path);
  57. writer.write(json.toJSONString());
  58. getClass();
  59. writer.flush();
  60. writer.close();
  61. }
  62. /**
  63. * Write the Canvas File.
  64. *
  65. * @param path
  66. * the Path
  67. * @throws IOException
  68. * Exception
  69. */
  70. public void writeCanvasFile(String path) throws IOException {
  71. JSONObject json = new JSONObject();
  72. json.put("MODE", "CANVAS");
  73. json.put("ID", IdCounter.getCounter());
  74. writeCanvasObjects(json);
  75. writeCanvasElements(json);
  76. writeEdges(json);
  77. writeElementGraph(json);
  78. FileWriter writer = new FileWriter(path);
  79. writer.write(json.toJSONString());
  80. getClass();
  81. writer.flush();
  82. writer.close();
  83. }
  84. /**
  85. * Write the Category File.
  86. *
  87. * @param path
  88. * the Path
  89. * @throws IOException
  90. * exception
  91. */
  92. public void writeCategoryFile(String path) throws IOException {
  93. JSONObject json = new JSONObject();
  94. json.put("MODE", "CATEGORY");
  95. // eventuell muss man ID auch Speichern
  96. writeCategory(json);
  97. writeCategoryObjects(json);
  98. writeCategoryElements(json);
  99. FileWriter writer = new FileWriter(path);
  100. writer.write(json.toJSONString());
  101. getClass();
  102. writer.flush();
  103. writer.close();
  104. }
  105. /**
  106. * writes all Categories into a JSONObject.
  107. *
  108. * @param json
  109. * JSON Object
  110. * @throws IOException
  111. * exception
  112. */
  113. public void writeCategory(JSONObject json) {
  114. JSONArray arr = new JSONArray();
  115. for (Category cat : model.getCategories())
  116. arr.add(cat.getName());
  117. json.put("CG", arr);
  118. }
  119. /**
  120. * writes all Objects in Category into a JSONObject.
  121. *
  122. * @param json
  123. * JSON Object
  124. */
  125. public void writeCategoryObjects(JSONObject json) {
  126. JSONArray arr = new JSONArray();
  127. int i = 1;
  128. for (Category cats : model.getCategories())
  129. for (AbstractCpsObject cps : cats.getObjects()) {
  130. arr.add(getObjectType(cps));
  131. arr.add(cps.getSav());
  132. arr.add(cps.getObjName());
  133. arr.add(cps.getImage());
  134. json.put("CGO" + i++, arr);
  135. arr = new JSONArray();
  136. }
  137. }
  138. /**
  139. * Wrte Canvas Objects.
  140. *
  141. * @param json
  142. * JSON Object
  143. */
  144. public void writeCanvasObjects(JSONObject json) {
  145. JSONArray arr = new JSONArray();
  146. int i = 1;
  147. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  148. arr.add(getObjectType(cps));
  149. arr.add(cps.getObjName());
  150. arr.add(cps.getName());
  151. arr.add(cps.getID());
  152. arr.add(cps.getImage());
  153. arr.add(cps.getPosition().x);
  154. arr.add(cps.getPosition().y);
  155. json.put("CVSO" + i++, arr);
  156. arr = new JSONArray();
  157. }
  158. }
  159. /**
  160. * writes all Elements in Objects in Category into a JSONObject.
  161. *
  162. * @param json
  163. * JSON Object
  164. */
  165. public void writeCategoryElements(JSONObject json) {
  166. JSONArray arr = new JSONArray();
  167. int i = 1;
  168. for (Category cats : model.getCategories())
  169. for (AbstractCpsObject cps : cats.getObjects())
  170. if (cps instanceof HolonObject)
  171. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  172. arr.add(ele.getSav());
  173. arr.add(ele.getObj());
  174. arr.add(ele.getEleName());
  175. arr.add(ele.getAmount());
  176. arr.add(ele.getEnergy());
  177. json.put("CGE" + i++, arr);
  178. arr = new JSONArray();
  179. }
  180. }
  181. /**
  182. * Write Canvas Elements.
  183. *
  184. * @param json JSON Objects
  185. */
  186. public void writeCanvasElements(JSONObject json) {
  187. JSONArray arr = new JSONArray();
  188. int i = 1;
  189. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  190. if (cps instanceof HolonObject)
  191. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  192. arr.add(ele.getSav());
  193. arr.add(cps.getID());
  194. arr.add(ele.getEleName());
  195. arr.add(ele.getAmount());
  196. arr.add(ele.getEnergy());
  197. arr.add(Boolean.compare(ele.getActive(), true) + 1);
  198. json.put("CVSE" + i++, arr);
  199. arr = new JSONArray();
  200. }
  201. }
  202. }
  203. /**
  204. * write all Edges into a JSONObject.
  205. *
  206. * @param json JSON Object
  207. */
  208. public void writeEdges(JSONObject json) {
  209. JSONArray arr = new JSONArray();
  210. int i = 1;
  211. for (CpsEdge edge : model.getEdgesOnCanvas()) {
  212. arr.add(edge.getA().getID());
  213. arr.add(edge.getB().getID());
  214. arr.add(edge.getCapacity());
  215. arr.add(edge.getFlow());
  216. json.put("EDGE" + i++, arr);
  217. arr = new JSONArray();
  218. }
  219. }
  220. /**
  221. * writes the Graph from all Elements into a JSONObject.
  222. *
  223. * @param json JSON Object
  224. */
  225. public void writeElementGraph(JSONObject json) {
  226. ListIterator<Point> iterator;
  227. JSONArray arr = new JSONArray();
  228. int i = 1;
  229. for (Category cats : model.getCategories())
  230. for (AbstractCpsObject cps : cats.getObjects())
  231. if (cps instanceof HolonObject)
  232. for (HolonElement ele : ((HolonObject) cps).getElements())
  233. if (!ele.getGraphPoints().isEmpty()) {
  234. iterator = ele.getGraphPoints().listIterator();
  235. arr.add(ele.getSav());
  236. arr.add(ele.getObj());
  237. arr.add(ele.getEleName());
  238. while (iterator.hasNext()) {
  239. Point p = iterator.next();
  240. arr.add((int) p.getX());
  241. arr.add((int) p.getY());
  242. }
  243. json.put("CGGP" + i++, arr);
  244. arr = new JSONArray();
  245. }
  246. i = 1;
  247. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  248. if (cps instanceof HolonObject)
  249. for (HolonElement ele : ((HolonObject) cps).getElements())
  250. if (!ele.getGraphPoints().isEmpty()) {
  251. iterator = ele.getGraphPoints().listIterator();
  252. arr.add(ele.getSav());
  253. arr.add(cps.getID());
  254. arr.add(ele.getEleName());
  255. while (iterator.hasNext()) {
  256. Point p = iterator.next();
  257. arr.add((int) p.getX());
  258. arr.add((int) p.getY());
  259. }
  260. json.put("CVSGP" + i++, arr);
  261. arr = new JSONArray();
  262. }
  263. }
  264. }
  265. /**
  266. * Return the Object Type.
  267. *
  268. * @param cps AbstractCpsObject
  269. * @return The Object Type
  270. */
  271. public String getObjectType(AbstractCpsObject cps) {
  272. if (cps instanceof HolonObject)
  273. return "HolonObject";
  274. if (cps instanceof HolonTransformer)
  275. return "HolonTransformer";
  276. if (cps instanceof HolonSwitch)
  277. return "HolonSwitch";
  278. else
  279. return "CpsNode";
  280. }
  281. }