CategoryController.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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>(), "/Images/power-plant.png");
  27. addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
  28. addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
  29. }
  30. /**
  31. * Adds Category into Model if a Category with the same name already exists
  32. * Add Category_+1
  33. *
  34. * @param toAdd
  35. * neue Kategorie
  36. */
  37. public void addCategory(Category category) {
  38. // int number = 0;
  39. // String name = toAdd.getName();
  40. // while (sC.containsInList(MODEL.getCategories(), toAdd)) {
  41. // number++;
  42. // toAdd.setName(name + "_" + number);
  43. // }
  44. int i = 0;
  45. while (mpC.searchCat(category.getName()) != null) {
  46. if (category.getName().contains("_"))
  47. category.setName(category.getName().substring(0, category.getName().indexOf('_')));
  48. category.setName(category.getName() + "_" + i);
  49. i++;
  50. }
  51. MODEL.getCgIdx().put(category.getName(), MODEL.getCategories().size());
  52. MODEL.getCategories().add(category);
  53. notifyCatListeners();
  54. }
  55. /**
  56. * Adds New Category into Model
  57. *
  58. * @param name
  59. * Bezeichnung der neuen Kategorie
  60. */
  61. public void addNewCategory(String name) {
  62. addCategory(new Category(name));
  63. }
  64. /**
  65. * remove a Category from Model
  66. */
  67. public void removeCategory(Category c) {
  68. mpC.decIdx(c.getName(), MODEL.getCgIdx());
  69. MODEL.getCgIdx().remove(c.getName());
  70. MODEL.getCategories().remove(c);
  71. notifyCatListeners();
  72. }
  73. /**
  74. * delete a given Category
  75. *
  76. * @param category
  77. */
  78. public void deleteCategory(String category) {
  79. removeCategory(mpC.searchCat(category));
  80. }
  81. /**
  82. * Add Object into a Category
  83. *
  84. * @param category
  85. * Category
  86. * @param object
  87. * Object
  88. */
  89. public void addObject(Category category, AbstractCpsObject object) {
  90. int i = 0;
  91. while (mpC.searchCatObj(category, object.getObjName()) != null) {
  92. if (object.getObjName().contains("_"))
  93. object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
  94. String name = object.getObjName() + "_" + i;
  95. object.setObjName(name);
  96. object.setName(name);
  97. i++;
  98. }
  99. category.getObjIdx().put(object.getObjName(), category.getObjects().size());
  100. category.getObjects().add(object);
  101. notifyCatListeners();
  102. }
  103. /**
  104. * Add new Holon Object
  105. *
  106. * @param cat
  107. * Category
  108. * @param obj
  109. * New Object Name
  110. */
  111. public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
  112. HolonObject obj = new HolonObject(object);
  113. obj.setImage(image);
  114. obj.setElements(elements);
  115. obj.setSav(category.getName());
  116. addObject(category, obj);
  117. }
  118. /**
  119. * Add new Holon Transformer
  120. *
  121. * @param cat
  122. * Category
  123. * @param obj
  124. * New Object Name
  125. */
  126. public void addNewHolonTransformer(Category cat, String objName, String image) {
  127. HolonTransformer transformer = new HolonTransformer(objName);
  128. transformer.setImage(image);
  129. transformer.setSav(cat.getName());
  130. addObject(cat, transformer);
  131. }
  132. /**
  133. * Add new Holon Switch
  134. *
  135. * @param cat
  136. * Category
  137. * @param obj
  138. * New Object Name
  139. */
  140. public void addNewHolonSwitch(Category cat, String objName, String image) {
  141. HolonSwitch holonSwitch = new HolonSwitch(objName);
  142. holonSwitch.setImage(image);
  143. holonSwitch.setSav(cat.getName());
  144. addObject(cat, holonSwitch);
  145. }
  146. public void removeObject(Category category, AbstractCpsObject cps) {
  147. mpC.decIdx(cps.getObjName(), category.getObjIdx());
  148. category.getObjIdx().remove(cps.getObjName());
  149. category.getObjects().remove(cps);
  150. notifyCatListeners();
  151. }
  152. public void deleteObject(String category, String obj) {
  153. Category cat = mpC.searchCat(category);
  154. removeObject(cat, mpC.searchCatObj(cat, obj));
  155. }
  156. /**
  157. *
  158. * @param catLis
  159. */
  160. public void addCategoryListener(CategoryListener catLis) {
  161. MODEL.getCategoryListeners().add(catLis);
  162. }
  163. /**
  164. * notifies all listeners about changes in the Categories
  165. */
  166. public void notifyCatListeners() {
  167. for (CategoryListener l : MODEL.getCategoryListeners()) {
  168. l.onChange(MODEL.getCategories());
  169. }
  170. }
  171. }