CategoryController.java 5.4 KB

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