CategoryController.java 5.3 KB

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