CategoryController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import Interfaces.CategoryListener;
  4. import Interfaces.ComparableObject;
  5. import classes.Category;
  6. import classes.CpsObject;
  7. import classes.HolonElement;
  8. import classes.HolonObject;
  9. import classes.HolonSwitch;
  10. import classes.HolonTransformer;
  11. import ui.model.Model;
  12. public class CategoryController {
  13. private Model MODEL;
  14. public CategoryController(Model model) {
  15. this.MODEL = model;
  16. initCategories();
  17. }
  18. /**
  19. * init default category and objects
  20. */
  21. public void initCategories() {
  22. Category energy = new Category("Energy");
  23. Category building = new Category("Building");
  24. Category component = new Category("Component");
  25. HolonObject powerp = new HolonObject("Power Plant");
  26. powerp.setImage("/Images/power-plant.png");
  27. powerp.setSav("Energy");
  28. HolonObject house = new HolonObject("House");
  29. house.setImage("/Images/home-2.png");
  30. house.setSav("Building");
  31. HolonTransformer transformer = new HolonTransformer("Transformer");
  32. transformer.setImage("/Images/transformer-1.png");
  33. transformer.setSav("Component");
  34. HolonSwitch sw = new HolonSwitch("Switch");
  35. sw.setImage("/Images/switch-on.png");
  36. sw.setSav("Component");
  37. addObject(energy, powerp);
  38. addObject(building, house);
  39. addObject(component, transformer);
  40. addObject(component, sw);
  41. addCategory(energy);
  42. addCategory(building);
  43. addCategory(component);
  44. }
  45. /**
  46. * Adds Category into Model if a Category with the same name already exists
  47. * Add Category_+1
  48. *
  49. * @param toAdd
  50. * neue Kategorie
  51. */
  52. public void addCategory(Category toAdd) {
  53. int number = 0;
  54. String name = toAdd.getName();
  55. while (containsInList(MODEL.getCategories(), toAdd)) {
  56. number++;
  57. toAdd.setName(name + "_" + number);
  58. }
  59. ;
  60. MODEL.getCategories().add(toAdd);
  61. notifyCatListeners();
  62. }
  63. /**
  64. * Adds New Category into Model
  65. *
  66. * @param name
  67. * Bezeichnung der neuen Kategorie
  68. */
  69. public void addNewCategory(String name) {
  70. addCategory(new Category(name));
  71. }
  72. /**
  73. * deletes category with given name
  74. *
  75. * @param catName
  76. * @return true if successfull, otherwise false
  77. */
  78. public boolean deleteCategory(String catName) {
  79. int position = getPositionInList(MODEL.getCategories(), searchCatNode(catName));
  80. if (position != -1) {
  81. deleteCategoryAt(position);
  82. return true;
  83. } else
  84. return false;
  85. }
  86. /**
  87. * deletes a Category at given position
  88. */
  89. public void deleteCategoryAt(int idx) {
  90. MODEL.getCategories().remove(idx);
  91. notifyCatListeners();
  92. }
  93. /**
  94. * Add Object into a Category
  95. *
  96. * @param cat
  97. * Category
  98. * @param obj
  99. * Object
  100. */
  101. public void addObject(Category cat, CpsObject obj) {
  102. cat.getObjects().add(obj);
  103. notifyCatListeners();
  104. }
  105. /**
  106. * Add new Holon Object
  107. *
  108. * @param cat
  109. * Category
  110. * @param obj
  111. * New Object Name
  112. */
  113. public void addNewHolonObject(Category cat, String objName, ArrayList<HolonElement> elements, String image) {
  114. HolonObject object = new HolonObject(objName);
  115. object.setImage(image);
  116. object.setElements(elements);
  117. object.setSav(cat.getName());
  118. addObject(cat, object);
  119. }
  120. /**
  121. * Add new Holon Transformer
  122. *
  123. * @param cat
  124. * Category
  125. * @param obj
  126. * New Object Name
  127. */
  128. public void addNewHolonTransformer(Category cat, String objName, String image) {
  129. HolonTransformer transformer = new HolonTransformer(objName);
  130. transformer.setImage(image);
  131. transformer.setSav(cat.getName());
  132. addObject(cat, transformer);
  133. }
  134. /**
  135. * Add new Holon Switch
  136. *
  137. * @param cat
  138. * Category
  139. * @param obj
  140. * New Object Name
  141. */
  142. public void addNewHolonSwitch(Category cat, String objName, String image) {
  143. HolonSwitch holonSwitch = new HolonSwitch(objName);
  144. holonSwitch.setImage(image);
  145. holonSwitch.setSav(cat.getName());
  146. addObject(cat, holonSwitch);
  147. }
  148. /**
  149. * deletes given Object in given Category
  150. *
  151. * @param toDelete
  152. * @param deleteIn
  153. */
  154. public void deleteObjectInCat(String toDelete, String deleteIn) {
  155. Category cat = searchCatNode(deleteIn);
  156. print(cat.getObjects());
  157. for (int i = 0; i < cat.getObjects().size(); i++) {
  158. if (cat.getObjects().get(i).getCompareName().equals(toDelete)) {
  159. cat.getObjects().remove(i);
  160. cat.getObjects();
  161. notifyCatListeners();
  162. }
  163. }
  164. }
  165. public void print(ArrayList<CpsObject> iterate) {
  166. for (CpsObject cps : iterate) {
  167. System.out.println(cps.getCompareName());
  168. }
  169. }
  170. /**
  171. *
  172. * @param catLis
  173. */
  174. public void addCategoryListener(CategoryListener catLis) {
  175. MODEL.getCategoryListeners().add(catLis);
  176. }
  177. /**
  178. * notifies all listeners about changes in the Categories
  179. */
  180. public void notifyCatListeners() {
  181. for (CategoryListener l : MODEL.getCategoryListeners()) {
  182. l.onChange(MODEL.getCategories());
  183. }
  184. }
  185. /**
  186. * search for category
  187. *
  188. * @param name
  189. * @return
  190. */
  191. public Category searchCatNode(String name) {
  192. Category query = null;
  193. for (Category cat : MODEL.getCategories()) {
  194. if (cat.getName().equals(name)) {
  195. query = cat;
  196. break;
  197. }
  198. }
  199. return query;
  200. }
  201. /**
  202. * gets the position of an object in the array
  203. *
  204. * @param arrayList
  205. * @param toSearch
  206. * @return -1 if object is not in the array, otherwise the index
  207. */
  208. public int getPositionInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
  209. for (int i = 0; i < arrayList.size(); i++) {
  210. if (arrayList.get(i).getCompareName() == toSearch.getCompareName()) {
  211. return i;
  212. }
  213. }
  214. return -1;
  215. }
  216. public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
  217. for (ComparableObject obj : arrayList) {
  218. if (obj.getCompareName().equals(toSearch.getCompareName())) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. }