CategoryController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package holeg.ui.controller;
  2. import java.util.AbstractMap.SimpleEntry;
  3. import holeg.model.AbstractCanvasObject;
  4. import holeg.model.HolonElement;
  5. import holeg.model.HolonObject;
  6. import holeg.model.HolonSwitch;
  7. import holeg.ui.model.GuiSettings;
  8. import holeg.ui.view.main.Category;
  9. import holeg.utility.events.Event;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.Optional;
  13. /**
  14. * Controller for the Categories.
  15. *
  16. * @author Gruppe14
  17. */
  18. public class CategoryController {
  19. private final Event OnCategoryChanged;
  20. /**
  21. * Constructor.
  22. *
  23. * @param model
  24. * the Model
  25. * @param mp
  26. * the MultiPurposeController
  27. * @param control
  28. */
  29. public CategoryController(Control control) {
  30. this.OnCategoryChanged = control.OnCategoryChanged;
  31. }
  32. /**
  33. * init default category and objects.
  34. */
  35. public void initCategories() {
  36. Category energy = createCategoryWithName("Energy");
  37. Category building = createCategoryWithName("Building");
  38. Category component = createCategoryWithName("Component");
  39. HolonObject powerPlant = addNewHolonObject(energy, "Power Plant", new ArrayList<HolonElement>(),
  40. "/images/canvas/power_plant.png");
  41. HolonObject house = addNewHolonObject(building, "House", new ArrayList<HolonElement>(), "/images/canvas/home.png");
  42. addNewHolonSwitch(component, "Switch", "/images/canvas/switch_on.png");
  43. powerPlant.add(new HolonElement(null, "Power", 10000));
  44. energy.getObjects().add(powerPlant);
  45. house.add(new HolonElement(null, "TV", -250));
  46. house.add(new HolonElement(null, "TV", -250));
  47. house.add(new HolonElement(null, "Fridge", -500));
  48. house.add(new HolonElement(null, "Radio", -100));
  49. house.add(new HolonElement(null, "PC", -250));
  50. house.add(new HolonElement(null, "PC", -250));
  51. house.add(new HolonElement(null, "PC", -250));
  52. house.add(new HolonElement(null, "Light", -50));
  53. house.add(new HolonElement(null, "Light", -50));
  54. house.add(new HolonElement(null, "Light", -50));
  55. house.add(new HolonElement(null, "Light", -50));
  56. house.add(new HolonElement(null, "Light", -50));
  57. house.add(new HolonElement(null, "Solar Panel", 300));
  58. building.getObjects().add(house);
  59. OnCategoryChanged.broadcast();
  60. }
  61. public Category createCategoryWithName(String categoryName) {
  62. Optional<Category> category = findCategoryWithName(categoryName);
  63. if(category.isEmpty()) {
  64. Category cat = new Category(categoryName);
  65. GuiSettings.getCategories().add(cat);
  66. OnCategoryChanged.broadcast();
  67. return cat;
  68. }else {
  69. return category.get();
  70. }
  71. }
  72. /**
  73. * remove a Category from Model.
  74. *
  75. * @param c
  76. * Category
  77. */
  78. public void removeCategory(Category c) {
  79. GuiSettings.getCategories().remove(c);
  80. OnCategoryChanged.broadcast();
  81. }
  82. public void clearCategories() {
  83. GuiSettings.getCategories().clear();
  84. }
  85. /**
  86. * Add Object into a Category.
  87. *
  88. * @param category
  89. * Category
  90. * @param object
  91. * Object
  92. */
  93. public void addObject(Category category, AbstractCanvasObject object) {
  94. int i = 0;
  95. boolean updateElementSaves = false;
  96. String name = "";
  97. //TODO(Tom2021-12-1) remove/redo this search
  98. while (category.findObjectWithName(object.getName()).isPresent()) {
  99. updateElementSaves = true;
  100. if (object.getName().contains("_"))
  101. object.setName(object.getName().substring(0, object.getName().indexOf('_')));
  102. name = object.getName() + "_" + i;
  103. object.setName(name);
  104. i++;
  105. }
  106. if(updateElementSaves && object instanceof HolonObject hO&&hO.getElements()!=null){
  107. for(HolonElement hE : hO.getElements().toList()) {
  108. hE.setSaving(new SimpleEntry<String,String>(hE.getSaving().getKey(), name));
  109. }
  110. }
  111. category.getObjects().add(object);
  112. }
  113. /**
  114. * Add new Holon Object to a Category.
  115. *
  116. * @param category
  117. * Category
  118. * @param object
  119. * New Object Name
  120. * @param list
  121. * Array of Elements
  122. * @param image
  123. * the image Path
  124. */
  125. public HolonObject addNewHolonObject(Category category, String object, List<HolonElement> list, String image) {
  126. HolonObject obj = new HolonObject(object);
  127. obj.setImage(image);
  128. obj.clearElements();
  129. obj.add(list);
  130. obj.setSav(category.getName());
  131. addObject(category, obj);
  132. return obj;
  133. }
  134. /**
  135. * Add new Holon Switch.
  136. *
  137. * @param cat
  138. * Category
  139. * @param objName
  140. * New Object Name
  141. * @param image
  142. * the Image Path
  143. */
  144. public HolonSwitch addNewHolonSwitch(Category cat, String objName, String image) {
  145. HolonSwitch holonSwitch = new HolonSwitch(objName);
  146. holonSwitch.setImage(image);
  147. holonSwitch.setSav(cat.getName());
  148. addObject(cat, holonSwitch);
  149. return holonSwitch;
  150. }
  151. /**
  152. * Removes an Object from a Category.
  153. * @param category Category
  154. * @param cps the Object
  155. */
  156. public void removeObject(Category category, AbstractCanvasObject cps) {
  157. category.getObjects().remove(cps);
  158. OnCategoryChanged.broadcast();
  159. }
  160. public Optional<Category> findCategoryWithName(String categoryName) {
  161. return GuiSettings.getCategories().stream().filter(cat -> cat.getName().equals(categoryName)).findAny();
  162. }
  163. }