CategoryController.java 5.3 KB

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