CategoryController.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import classes.Category;
  4. import classes.AbstractCpsObject;
  5. import classes.HolonElement;
  6. import classes.HolonObject;
  7. import classes.HolonSwitch;
  8. import classes.HolonTransformer;
  9. import interfaces.CategoryListener;
  10. import ui.model.Model;
  11. /**
  12. * Controller for the Categories.
  13. *
  14. * @author Gruppe14
  15. */
  16. public class CategoryController {
  17. private Model model;
  18. private MultiPurposeController mpC;
  19. /**
  20. * Constructor.
  21. *
  22. * @param model
  23. * the Model
  24. * @param mp
  25. * the MultiPurposeController
  26. */
  27. public CategoryController(Model model, MultiPurposeController mp) {
  28. this.model = model;
  29. this.mpC = mp;
  30. initCategories();
  31. }
  32. /**
  33. * init default category and objects.
  34. */
  35. public void initCategories() {
  36. addNewCategory("Energy");
  37. addNewCategory("Building");
  38. addNewCategory("Component");
  39. addNewHolonObject(mpC.searchCat("Energy"), "Power Plant", new ArrayList<HolonElement>(),
  40. "/Images/power-plant.png");
  41. addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
  42. addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
  43. }
  44. /**
  45. * Adds Category into Model if a Category with the same name already exists
  46. <<<<<<< HEAD
  47. * Add Category_+1
  48. *
  49. * @param toAdd
  50. * neue Kategorie
  51. =======
  52. * Add Category_+1.
  53. *
  54. * @param category
  55. * the new Category
  56. >>>>>>> 90bc29b29bece3f981eca7b6bd629093b2c90382
  57. */
  58. public void addCategory(Category category) {
  59. // int number = 0;
  60. // String name = toAdd.getName();
  61. // while (sC.containsInList(MODEL.getCategories(), toAdd)) {
  62. // number++;
  63. // toAdd.setName(name + "_" + number);
  64. // }
  65. int i = 0;
  66. while (mpC.searchCat(category.getName()) != null) {
  67. if (category.getName().contains("_"))
  68. category.setName(category.getName().substring(0, category.getName().indexOf('_')));
  69. category.setName(category.getName() + "_" + i);
  70. i++;
  71. }
  72. model.getCgIdx().put(category.getName(), model.getCategories().size());
  73. model.getCategories().add(category);
  74. notifyCatListeners();
  75. }
  76. /**
  77. * Adds New Category into Model.
  78. *
  79. * @param name
  80. * Bezeichnung der neuen Kategorie
  81. */
  82. public void addNewCategory(String name) {
  83. addCategory(new Category(name));
  84. }
  85. /**
  86. * remove a Category from Model.
  87. *
  88. * @param c
  89. * Category
  90. */
  91. public void removeCategory(Category c) {
  92. mpC.decIdx(c.getName(), model.getCgIdx());
  93. model.getCgIdx().remove(c.getName());
  94. model.getCategories().remove(c);
  95. notifyCatListeners();
  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, AbstractCpsObject object) {
  115. int i = 0;
  116. while (mpC.searchCatObj(category, object.getObjName()) != null) {
  117. if (object.getObjName().contains("_"))
  118. object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
  119. String name = object.getObjName() + "_" + i;
  120. object.setObjName(name);
  121. object.setName(name);
  122. i++;
  123. }
  124. category.getObjIdx().put(object.getObjName(), category.getObjects().size());
  125. category.getObjects().add(object);
  126. notifyCatListeners();
  127. }
  128. /**
  129. * Add new Holon Object to a Category.
  130. *
  131. * @param category
  132. * Category
  133. * @param object
  134. * New Object Name
  135. * @param elements
  136. * Array of Elements
  137. * @param image
  138. * the image Path
  139. */
  140. public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
  141. HolonObject obj = new HolonObject(object);
  142. obj.setImage(image);
  143. obj.setElements(elements);
  144. obj.setSav(category.getName());
  145. addObject(category, obj);
  146. }
  147. /**
  148. * Add new Holon Transformer.
  149. *
  150. * @param cat
  151. * Category
  152. * @param objName
  153. * New Object Name
  154. * @param image
  155. * the image Path
  156. */
  157. public void addNewHolonTransformer(Category cat, String objName, String image) {
  158. HolonTransformer transformer = new HolonTransformer(objName);
  159. transformer.setImage(image);
  160. transformer.setSav(cat.getName());
  161. addObject(cat, transformer);
  162. }
  163. /**
  164. * Add new Holon Switch.
  165. *
  166. * @param cat
  167. * Category
  168. * @param objName
  169. * New Object Name
  170. * @param image
  171. * the Image Path
  172. */
  173. public void addNewHolonSwitch(Category cat, String objName, String image) {
  174. HolonSwitch holonSwitch = new HolonSwitch(objName);
  175. holonSwitch.setImage(image);
  176. holonSwitch.setSav(cat.getName());
  177. addObject(cat, holonSwitch);
  178. }
  179. /**
  180. * Removes an Object from a Category.
  181. * @param category Category
  182. * @param cps the Object
  183. */
  184. public void removeObject(Category category, AbstractCpsObject cps) {
  185. mpC.decIdx(cps.getObjName(), category.getObjIdx());
  186. category.getObjIdx().remove(cps.getObjName());
  187. category.getObjects().remove(cps);
  188. notifyCatListeners();
  189. }
  190. /**
  191. * Delete an Object from a Category.
  192. *
  193. * @param category
  194. * the Category
  195. * @param obj
  196. * the Object
  197. */
  198. public void deleteObject(String category, String obj) {
  199. Category cat = mpC.searchCat(category);
  200. removeObject(cat, mpC.searchCatObj(cat, obj));
  201. }
  202. /**
  203. * Init the CategoryListener.
  204. *
  205. * @param catLis
  206. * the CategoryListener
  207. */
  208. public void addCategoryListener(CategoryListener catLis) {
  209. model.getCategoryListeners().add(catLis);
  210. }
  211. /**
  212. * notifies all listeners about changes in the Categories.
  213. */
  214. public void notifyCatListeners() {
  215. for (CategoryListener l : model.getCategoryListeners()) {
  216. l.onChange(model.getCategories());
  217. }
  218. }
  219. }