CategoryController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import Interfaces.CategoryListener;
  4. import classes.Category;
  5. import classes.CpsObject;
  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.searchCategory("Energy"), "Power Plant", new ArrayList<HolonElement>(), "/Images/power-plant.png");
  27. addNewHolonObject(mpC.searchCategory("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
  28. addNewHolonTransformer(mpC.searchCategory("Component"), "Transformer", "/Images/transformer-1.png");
  29. addNewHolonSwitch(mpC.searchCategory("Component"), "Switch", "/Images/switch-off.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.searchCategory(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. */
  79. public void deleteCategory(String category) {
  80. removeCategory(mpC.searchCategory(category));
  81. }
  82. /**
  83. * Add Object into a Category
  84. *
  85. * @param category
  86. * Category
  87. * @param object
  88. * Object
  89. */
  90. public void addObject(Category category, CpsObject object) {
  91. int i = 0;
  92. while (mpC.searchCategoryObject(category, object.getObjName()) != null) {
  93. if (object.getObjName().contains("_"))
  94. object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
  95. String name = object.getObjName() + "_" + i;
  96. object.setObjName(name);
  97. object.setName(name);
  98. i++;
  99. }
  100. category.getObjIdx().put(object.getObjName(), category.getObjects().size());
  101. category.getObjects().add(object);
  102. notifyCatListeners();
  103. }
  104. /**
  105. * Add new Holon Object
  106. *
  107. * @param cat
  108. * Category
  109. * @param obj
  110. * New Object Name
  111. */
  112. public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
  113. HolonObject obj = new HolonObject(object);
  114. obj.setImage(image);
  115. obj.setElements(elements);
  116. obj.setSav(category.getName());
  117. addObject(category, obj);
  118. }
  119. /**
  120. * Add new Holon Transformer
  121. *
  122. * @param cat
  123. * Category
  124. * @param obj
  125. * New Object Name
  126. */
  127. public void addNewHolonTransformer(Category cat, String objName, String image) {
  128. HolonTransformer transformer = new HolonTransformer(objName);
  129. transformer.setImage(image);
  130. transformer.setSav(cat.getName());
  131. addObject(cat, transformer);
  132. }
  133. /**
  134. * Add new Holon Switch
  135. *
  136. * @param cat
  137. * Category
  138. * @param obj
  139. * New Object Name
  140. */
  141. public void addNewHolonSwitch(Category cat, String objName, String image) {
  142. HolonSwitch holonSwitch = new HolonSwitch(objName);
  143. holonSwitch.setImage(image);
  144. holonSwitch.setSav(cat.getName());
  145. addObject(cat, holonSwitch);
  146. }
  147. public void removeObject(Category category, CpsObject cps) {
  148. mpC.decIdx(cps.getObjName(), category.getObjIdx());
  149. category.getObjIdx().remove(cps.getObjName());
  150. category.getObjects().remove(cps);
  151. notifyCatListeners();
  152. }
  153. public void deleteObject(String category, String obj) {
  154. Category cat = mpC.searchCategory(category);
  155. removeObject(cat, mpC.searchCategoryObject(cat, obj));
  156. }
  157. /**
  158. *
  159. * @param catLis
  160. */
  161. public void addCategoryListener(CategoryListener catLis) {
  162. MODEL.getCategoryListeners().add(catLis);
  163. }
  164. /**
  165. * notifies all listeners about changes in the Categories
  166. */
  167. public void notifyCatListeners() {
  168. for (CategoryListener l : MODEL.getCategoryListeners()) {
  169. l.onChange(MODEL.getCategories());
  170. }
  171. }
  172. }