CategoryController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. *
  47. * @param category
  48. * the new Category
  49. */
  50. public void addCategory(Category category) {
  51. // int number = 0;
  52. // String name = toAdd.getName();
  53. // while (sC.containsInList(MODEL.getCategories(), toAdd)) {
  54. // number++;
  55. // toAdd.setName(name + "_" + number);
  56. // }
  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. notifyCatListeners();
  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. notifyCatListeners();
  88. }
  89. /**
  90. * delete a given Category.
  91. *
  92. * @param category
  93. * the Category
  94. */
  95. public void deleteCategory(String category) {
  96. removeCategory(mpC.searchCat(category));
  97. }
  98. /**
  99. * Add Object into a Category.
  100. *
  101. * @param category
  102. * Category
  103. * @param object
  104. * Object
  105. */
  106. public void addObject(Category category, AbstractCpsObject object) {
  107. int i = 0;
  108. while (mpC.searchCatObj(category, object.getObjName()) != null) {
  109. if (object.getObjName().contains("_"))
  110. object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
  111. String name = object.getObjName() + "_" + i;
  112. object.setObjName(name);
  113. object.setName(name);
  114. i++;
  115. }
  116. category.getObjIdx().put(object.getObjName(), category.getObjects().size());
  117. category.getObjects().add(object);
  118. notifyCatListeners();
  119. }
  120. /**
  121. * Add new Holon Object to a Category.
  122. *
  123. * @param category
  124. * Category
  125. * @param object
  126. * New Object Name
  127. * @param elements
  128. * Array of Elements
  129. * @param image
  130. * the image Path
  131. */
  132. public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
  133. HolonObject obj = new HolonObject(object);
  134. obj.setImage(image);
  135. obj.setElements(elements);
  136. obj.setSav(category.getName());
  137. addObject(category, obj);
  138. }
  139. /**
  140. * Add new Holon Transformer.
  141. *
  142. * @param cat
  143. * Category
  144. * @param objName
  145. * New Object Name
  146. * @param image
  147. * the image Path
  148. */
  149. public void addNewHolonTransformer(Category cat, String objName, String image) {
  150. HolonTransformer transformer = new HolonTransformer(objName);
  151. transformer.setImage(image);
  152. transformer.setSav(cat.getName());
  153. addObject(cat, transformer);
  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, AbstractCpsObject cps) {
  177. mpC.decIdx(cps.getObjName(), category.getObjIdx());
  178. category.getObjIdx().remove(cps.getObjName());
  179. category.getObjects().remove(cps);
  180. notifyCatListeners();
  181. }
  182. /**
  183. * Delete an Object from a Category.
  184. *
  185. * @param category
  186. * the Category
  187. * @param obj
  188. * the Object
  189. */
  190. public void deleteObject(String category, String obj) {
  191. Category cat = mpC.searchCat(category);
  192. removeObject(cat, mpC.searchCatObj(cat, obj));
  193. }
  194. /**
  195. * Init the CategoryListener.
  196. *
  197. * @param catLis
  198. * the CategoryListener
  199. */
  200. public void addCategoryListener(CategoryListener catLis) {
  201. model.getCategoryListeners().add(catLis);
  202. }
  203. /**
  204. * notifies all listeners about changes in the Categories.
  205. */
  206. public void notifyCatListeners() {
  207. for (CategoryListener l : model.getCategoryListeners()) {
  208. l.onChange(model.getCategories());
  209. }
  210. }
  211. }