CategoryController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.Model;
  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 Model model;
  20. private MultiPurposeController mpC;
  21. private final Event OnCategoryChanged;
  22. /**
  23. * Constructor.
  24. *
  25. * @param model
  26. * the Model
  27. * @param mp
  28. * the MultiPurposeController
  29. * @param control
  30. */
  31. public CategoryController(Model model, MultiPurposeController mp, Control control) {
  32. this.model = model;
  33. this.mpC = mp;
  34. this.OnCategoryChanged = control.OnCategoryChanged;
  35. initCategories();
  36. }
  37. /**
  38. * init default category and objects.
  39. */
  40. public void initCategories() {
  41. addNewCategory("Energy");
  42. addNewCategory("Building");
  43. addNewCategory("Component");
  44. addNewHolonObject(mpC.searchCat("Energy"), "Power Plant", new ArrayList<HolonElement>(),
  45. "/Images/power-plant.png");
  46. addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
  47. addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
  48. OnCategoryChanged.broadcast();
  49. }
  50. /**
  51. * Adds Category into Model if a Category with the same name already exists
  52. *
  53. * @param category
  54. * the new Category
  55. */
  56. public void addCategory(Category category) {
  57. int i = 0;
  58. while (mpC.searchCat(category.getName()) != null) {
  59. if (category.getName().contains("_"))
  60. category.setName(category.getName().substring(0, category.getName().indexOf('_')));
  61. category.setName(category.getName() + "_" + i);
  62. i++;
  63. }
  64. model.getCgIdx().put(category.getName(), model.getCategories().size());
  65. model.getCategories().add(category);
  66. OnCategoryChanged.broadcast();
  67. }
  68. /**
  69. * Adds New Category into Model.
  70. *
  71. * @param name
  72. * Bezeichnung der neuen Kategorie
  73. */
  74. public void addNewCategory(String name) {
  75. addCategory(new Category(name));
  76. }
  77. /**
  78. * remove a Category from Model.
  79. *
  80. * @param c
  81. * Category
  82. */
  83. public void removeCategory(Category c) {
  84. mpC.decIdx(c.getName(), model.getCgIdx());
  85. model.getCgIdx().remove(c.getName());
  86. model.getCategories().remove(c);
  87. OnCategoryChanged.broadcast();
  88. }
  89. /**
  90. * get All Categories
  91. *
  92. * @return the ArrayList of all Categories
  93. */
  94. public ArrayList<Category> getCategories()
  95. {
  96. return model.getCategories();
  97. }
  98. /**
  99. * delete a given Category.
  100. *
  101. * @param category
  102. * the Category
  103. */
  104. public void deleteCategory(String category) {
  105. removeCategory(mpC.searchCat(category));
  106. }
  107. /**
  108. * Add Object into a Category.
  109. *
  110. * @param category
  111. * Category
  112. * @param object
  113. * Object
  114. */
  115. public void addObject(Category category, AbstractCanvasObject object) {
  116. int i = 0;
  117. boolean updateElementSaves = false;
  118. String name = "";
  119. //TODO(Tom2021-12-1) remove/redo this search
  120. while (mpC.searchCatObj(category, object.getName()).isPresent()) {
  121. updateElementSaves = true;
  122. if (object.getName().contains("_"))
  123. object.setName(object.getName().substring(0, object.getName().indexOf('_')));
  124. name = object.getName() + "_" + i;
  125. object.setName(name);
  126. i++;
  127. }
  128. if(updateElementSaves && object instanceof HolonObject hO&&hO.getElements()!=null){
  129. for(HolonElement e: hO.getElements()){
  130. e.setSaving(new SimpleEntry<String,String>(e.getSaving().getKey(), name));
  131. }
  132. }
  133. category.getObjIdx().put(object.getName(), category.getObjects().size());
  134. category.getObjects().add(object);
  135. }
  136. /**
  137. * Add new Holon Object to a Category.
  138. *
  139. * @param category
  140. * Category
  141. * @param object
  142. * New Object Name
  143. * @param list
  144. * Array of Elements
  145. * @param image
  146. * the image Path
  147. */
  148. public void addNewHolonObject(Category category, String object, List<HolonElement> list, String image) {
  149. HolonObject obj = new HolonObject(object);
  150. obj.setImage(image);
  151. obj.setElements(list);
  152. obj.setSav(category.getName());
  153. addObject(category, obj);
  154. }
  155. /**
  156. * Add new Holon Switch.
  157. *
  158. * @param cat
  159. * Category
  160. * @param objName
  161. * New Object Name
  162. * @param image
  163. * the Image Path
  164. */
  165. public void addNewHolonSwitch(Category cat, String objName, String image) {
  166. HolonSwitch holonSwitch = new HolonSwitch(objName);
  167. holonSwitch.setImage(image);
  168. holonSwitch.setSav(cat.getName());
  169. addObject(cat, holonSwitch);
  170. }
  171. /**
  172. * Removes an Object from a Category.
  173. * @param category Category
  174. * @param cps the Object
  175. */
  176. public void removeObject(Category category, AbstractCanvasObject cps) {
  177. mpC.decIdx(cps.getName(), category.getObjIdx());
  178. category.getObjIdx().remove(cps.getName());
  179. category.getObjects().remove(cps);
  180. OnCategoryChanged.broadcast();
  181. }
  182. /**
  183. * Delete an Object from a Category.
  184. *
  185. * @param category
  186. * the Category
  187. * @param objectName
  188. * the Object
  189. */
  190. public void deleteObject(String category, String objectName) {
  191. Category cat = mpC.searchCat(category);
  192. Optional<AbstractCanvasObject> object = mpC.searchCatObj(cat, objectName);
  193. object.ifPresent(obj -> removeObject(cat, obj));
  194. }
  195. }