StoreController.java 6.4 KB

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