StoreController.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 ui.model.Model;
  16. import ui.model.idCounter;
  17. public class StoreController {
  18. private Model MODEL;
  19. public StoreController(Model model) {
  20. this.MODEL = model;
  21. }
  22. /**
  23. * Writes the current State of the Modelling into a JSON File which can be
  24. * loaded
  25. *
  26. * @throws IOException
  27. */
  28. public void writeSaveFile(String path) throws IOException {
  29. JSONObject json = new JSONObject();
  30. json.put("MODE", "ALL");
  31. json.put("ID", idCounter.getCounter());
  32. writeCategory(json);
  33. writeCategoryObjects(json);
  34. writeCanvasObjects(json);
  35. writeCategoryElements(json);
  36. writeCanvasElements(json);
  37. writeEdges(json);
  38. writeElementGraph(json);
  39. FileWriter writer = new FileWriter(path);
  40. writer.write(json.toJSONString());
  41. getClass();
  42. writer.flush();
  43. writer.close();
  44. }
  45. public void writeCanvasFile(String path) throws IOException {
  46. JSONObject json = new JSONObject();
  47. json.put("MODE", "CANVAS");
  48. json.put("ID", idCounter.getCounter());
  49. writeCanvasObjects(json);
  50. writeCanvasElements(json);
  51. writeEdges(json);
  52. writeElementGraph(json);
  53. FileWriter writer = new FileWriter(path);
  54. writer.write(json.toJSONString());
  55. getClass();
  56. writer.flush();
  57. writer.close();
  58. }
  59. public void writeCategoryFile(String path) throws IOException {
  60. JSONObject json = new JSONObject();
  61. json.put("MODE", "CATEGORY");
  62. //eventuell muss man ID auch Speichern
  63. writeCategory(json);
  64. writeCategoryObjects(json);
  65. writeCategoryElements(json);
  66. FileWriter writer = new FileWriter(path);
  67. writer.write(json.toJSONString());
  68. getClass();
  69. writer.flush();
  70. writer.close();
  71. }
  72. /**
  73. * writes all Categories into a JSONObject
  74. *
  75. * @param json
  76. * @throws IOException
  77. */
  78. public void writeCategory(JSONObject json) {
  79. JSONArray arr = new JSONArray();
  80. for (Category cat : MODEL.getCategories())
  81. arr.add(cat.getName());
  82. json.put("CG", arr);
  83. }
  84. /**
  85. * writes all Objects in Category into a JSONObject
  86. *
  87. * @param json
  88. */
  89. public void writeCategoryObjects(JSONObject json) {
  90. JSONArray arr = new JSONArray();
  91. int i = 1;
  92. for (Category cats : MODEL.getCategories())
  93. for (AbstractCpsObject cps : cats.getObjects()) {
  94. arr.add(getObjectType(cps));
  95. arr.add(cps.getSav());
  96. arr.add(cps.getObjName());
  97. arr.add(cps.getImage());
  98. json.put("CGO" + i++, arr);
  99. arr = new JSONArray();
  100. }
  101. }
  102. public void writeCanvasObjects(JSONObject json) {
  103. JSONArray arr = new JSONArray();
  104. int i = 1;
  105. for (AbstractCpsObject cps : MODEL.getObjectsOnCanvas()) {
  106. arr.add(getObjectType(cps));
  107. arr.add(cps.getObjName());
  108. arr.add(cps.getName());
  109. arr.add(cps.getID());
  110. arr.add(cps.getImage());
  111. arr.add(cps.getPosition().x);
  112. arr.add(cps.getPosition().y);
  113. json.put("CVSO" + i++, arr);
  114. arr = new JSONArray();
  115. }
  116. }
  117. /**
  118. * writes all Elements in Objects in Category into a JSONObject
  119. *
  120. * @param json
  121. */
  122. public void writeCategoryElements(JSONObject json) {
  123. JSONArray arr = new JSONArray();
  124. int i = 1;
  125. for (Category cats : MODEL.getCategories())
  126. for (AbstractCpsObject cps : cats.getObjects())
  127. if (cps instanceof HolonObject)
  128. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  129. arr.add(ele.getSav());
  130. arr.add(ele.getObj());
  131. arr.add(ele.getEleName());
  132. arr.add(ele.getAmount());
  133. arr.add(ele.getEnergy());
  134. json.put("CGE" + i++, arr);
  135. arr = new JSONArray();
  136. }
  137. }
  138. public void writeCanvasElements(JSONObject json) {
  139. JSONArray arr = new JSONArray();
  140. int i = 1;
  141. for (AbstractCpsObject cps : MODEL.getObjectsOnCanvas()) {
  142. if (cps instanceof HolonObject)
  143. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  144. arr.add(ele.getSav());
  145. arr.add(cps.getID());
  146. arr.add(ele.getEleName());
  147. arr.add(ele.getAmount());
  148. arr.add(ele.getEnergy());
  149. arr.add(Boolean.compare(ele.getActive(), true) + 1);
  150. json.put("CVSE" + i++, arr);
  151. arr = new JSONArray();
  152. }
  153. }
  154. }
  155. /**
  156. * write all Edges into a JSONObject
  157. *
  158. * @param json
  159. */
  160. public void writeEdges(JSONObject json) {
  161. JSONArray arr = new JSONArray();
  162. int i = 1;
  163. for (CpsEdge edge : MODEL.getEdgesOnCanvas()) {
  164. arr.add(edge.getA().getID());
  165. arr.add(edge.getB().getID());
  166. arr.add(edge.getCapacity());
  167. arr.add(edge.getFlow());
  168. json.put("EDGE" + i++, arr);
  169. arr = new JSONArray();
  170. }
  171. }
  172. /**
  173. * writes the Graph from all Elements into a JSONObject
  174. *
  175. * @param json
  176. */
  177. public void writeElementGraph(JSONObject json) {
  178. ListIterator<Point> iterator;
  179. JSONArray arr = new JSONArray();
  180. int i = 1;
  181. for (Category cats : MODEL.getCategories())
  182. for (AbstractCpsObject cps : cats.getObjects())
  183. if (cps instanceof HolonObject)
  184. for (HolonElement ele : ((HolonObject) cps).getElements())
  185. if (!ele.getGraphPoints().isEmpty()) {
  186. iterator = ele.getGraphPoints().listIterator();
  187. arr.add(ele.getSav());
  188. arr.add(ele.getObj());
  189. arr.add(ele.getEleName());
  190. while (iterator.hasNext()) {
  191. Point p = iterator.next();
  192. arr.add((int) p.getX());
  193. arr.add((int) p.getY());
  194. }
  195. json.put("CGGP" + i++, arr);
  196. arr = new JSONArray();
  197. }
  198. i = 1;
  199. for (AbstractCpsObject cps : MODEL.getObjectsOnCanvas()) {
  200. if (cps instanceof HolonObject)
  201. for (HolonElement ele : ((HolonObject) cps).getElements())
  202. if (!ele.getGraphPoints().isEmpty()) {
  203. iterator = ele.getGraphPoints().listIterator();
  204. arr.add(ele.getSav());
  205. arr.add(cps.getID());
  206. arr.add(ele.getEleName());
  207. while (iterator.hasNext()) {
  208. Point p = iterator.next();
  209. arr.add((int) p.getX());
  210. arr.add((int) p.getY());
  211. }
  212. json.put("CVSGP" + i++, arr);
  213. arr = new JSONArray();
  214. }
  215. }
  216. }
  217. /**
  218. * Return the Object Type
  219. *
  220. * @param cps
  221. * @return
  222. */
  223. public String getObjectType(AbstractCpsObject cps) {
  224. if (cps instanceof HolonObject)
  225. return "HolonObject";
  226. if (cps instanceof HolonTransformer)
  227. return "HolonTransformer";
  228. if (cps instanceof HolonSwitch)
  229. return "HolonSwitch";
  230. else
  231. return "CpsNode";
  232. }
  233. }