StoreController.java 7.2 KB

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