CategoryController.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. initCategories();
  32. }
  33. /**
  34. * init default category and objects.
  35. */
  36. public void initCategories() {
  37. Category energy = createCategoryWithName("Energy");
  38. Category building = createCategoryWithName("Building");
  39. Category component = createCategoryWithName("Component");
  40. HolonObject powerPlant = addNewHolonObject(energy, "Power Plant", new ArrayList<HolonElement>(),
  41. "/Images/power-plant.png");
  42. HolonObject house = addNewHolonObject(building, "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
  43. addNewHolonSwitch(component, "Switch", "/Images/switch-on.png");
  44. powerPlant.addElement(new HolonElement(null, "Power", 10000));
  45. energy.getObjects().add(powerPlant);
  46. house.addElement(new HolonElement(null, "TV", -250));
  47. house.addElement(new HolonElement(null, "TV", -250));
  48. house.addElement(new HolonElement(null, "Fridge", -500));
  49. house.addElement(new HolonElement(null, "Radio", -100));
  50. house.addElement(new HolonElement(null, "PC", -250));
  51. house.addElement(new HolonElement(null, "PC", -250));
  52. house.addElement(new HolonElement(null, "PC", -250));
  53. house.addElement(new HolonElement(null, "Light", -50));
  54. house.addElement(new HolonElement(null, "Light", -50));
  55. house.addElement(new HolonElement(null, "Light", -50));
  56. house.addElement(new HolonElement(null, "Light", -50));
  57. house.addElement(new HolonElement(null, "Light", -50));
  58. house.addElement(new HolonElement(null, "Solar Panel", 300));
  59. building.getObjects().add(house);
  60. OnCategoryChanged.broadcast();
  61. }
  62. public Category createCategoryWithName(String categoryName) {
  63. int i = 0;
  64. while (findCategoryWithName(categoryName).isPresent()) {
  65. if (categoryName.contains("_")) {
  66. categoryName = (categoryName.substring(0, categoryName.indexOf('_')));
  67. }
  68. categoryName += "_" + i;
  69. i++;
  70. }
  71. Category cat = new Category(categoryName);
  72. GuiSettings.getCategories().add(cat);
  73. OnCategoryChanged.broadcast();
  74. return cat;
  75. }
  76. /**
  77. * remove a Category from Model.
  78. *
  79. * @param c
  80. * Category
  81. */
  82. public void removeCategory(Category c) {
  83. GuiSettings.getCategories().remove(c);
  84. OnCategoryChanged.broadcast();
  85. }
  86. /**
  87. * Add Object into a Category.
  88. *
  89. * @param category
  90. * Category
  91. * @param object
  92. * Object
  93. */
  94. public void addObject(Category category, AbstractCanvasObject object) {
  95. int i = 0;
  96. boolean updateElementSaves = false;
  97. String name = "";
  98. //TODO(Tom2021-12-1) remove/redo this search
  99. while (category.findObjectWithName(object.getName()).isPresent()) {
  100. updateElementSaves = true;
  101. if (object.getName().contains("_"))
  102. object.setName(object.getName().substring(0, object.getName().indexOf('_')));
  103. name = object.getName() + "_" + i;
  104. object.setName(name);
  105. i++;
  106. }
  107. if(updateElementSaves && object instanceof HolonObject hO&&hO.getElements()!=null){
  108. for(HolonElement e: hO.getElements()){
  109. e.setSaving(new SimpleEntry<String,String>(e.getSaving().getKey(), name));
  110. }
  111. }
  112. category.getObjects().add(object);
  113. }
  114. /**
  115. * Add new Holon Object to a Category.
  116. *
  117. * @param category
  118. * Category
  119. * @param object
  120. * New Object Name
  121. * @param list
  122. * Array of Elements
  123. * @param image
  124. * the image Path
  125. */
  126. public HolonObject addNewHolonObject(Category category, String object, List<HolonElement> list, String image) {
  127. HolonObject obj = new HolonObject(object);
  128. obj.setImage(image);
  129. obj.setElements(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. }