LoadStoreController.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package ui.controller;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import org.json.simple.JSONArray;
  9. import org.json.simple.JSONObject;
  10. import com.sun.scenario.effect.impl.sw.java.JSWBlend_COLOR_BURNPeer;
  11. import classes.Category;
  12. import classes.CpsObject;
  13. import classes.HolonElement;
  14. import classes.HolonObject;
  15. import ui.model.Model;
  16. public class LoadStoreController {
  17. private Model MODEL;
  18. public LoadStoreController(Model model) {
  19. this.MODEL = model;
  20. }
  21. public void writeJSONFile() throws IOException {
  22. JSONObject jsonObj = new JSONObject();
  23. writeCategory(jsonObj);
  24. writeCategoryObjects(jsonObj);
  25. writeCategoryElements(jsonObj);
  26. FileWriter file = new FileWriter("C:/Users/krabs/Desktop/Tesst.json");
  27. file.write(jsonObj.toJSONString());
  28. file.flush();
  29. file.close();
  30. }
  31. public void writeCategory(JSONObject jsonObj) throws IOException {
  32. JSONArray arr = new JSONArray();
  33. for (Category cat : MODEL.getCategories())
  34. arr.add(cat.getName());
  35. jsonObj.put("Category", arr);
  36. }
  37. public void writeCategoryObjects(JSONObject jsonObj) {
  38. int i = 1;
  39. JSONArray arr = new JSONArray();
  40. for (Category cats : MODEL.getCategories())
  41. for (CpsObject cps : cats.getObjects()) {
  42. arr.add(cps.getStored());
  43. arr.add(cps.getObjName());
  44. arr.add(cps.getName());
  45. arr.add(cps.getImage());
  46. jsonObj.put("CategoryObject." + i++, arr);
  47. arr = new JSONArray();
  48. }
  49. }
  50. public void writeCategoryElements(JSONObject jsonObj) {
  51. int i = 1;
  52. JSONArray arr = new JSONArray();
  53. for (Category cats : MODEL.getCategories())
  54. for (CpsObject cps : cats.getObjects())
  55. if (cps instanceof HolonObject)
  56. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  57. arr.add(ele.getStored());
  58. arr.add(ele.getEleName());
  59. arr.add(ele.getAmount());
  60. arr.add(ele.getEnergy());
  61. jsonObj.put("CategoryElement." + i++, arr);
  62. arr = new JSONArray();
  63. }
  64. }
  65. public void readFromJSON(File jsonFile) throws IOException {
  66. String line;
  67. BufferedReader reader = new BufferedReader(new FileReader("textfile"));
  68. while ((line = reader.readLine()) != null) {
  69. // mach hier irgendwas mit der Gelesenen Zeile
  70. }
  71. }
  72. }