CategoryController.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.setStored("Energy");
  28. HolonObject house = new HolonObject("House");
  29. house.setImage("/Images/home.png");
  30. house.setStored("Building");
  31. HolonTransformer transformer = new HolonTransformer("Transformer");
  32. transformer.setImage("/Images/transformer.png");
  33. transformer.setStored("Component");
  34. HolonSwitch sw = new HolonSwitch("Switch");
  35. sw.setImage("/Images/switch-on.png");
  36. sw.setStored("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. addObject(cat, object);
  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. 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. addObject(cat, holonSwitch);
  144. }
  145. /**
  146. * deletes given Object in given Category
  147. *
  148. * @param toDelete
  149. * @param deleteIn
  150. */
  151. public void deleteObjectInCat(String toDelete, String deleteIn) {
  152. Category cat = searchCatNode(deleteIn);
  153. print(cat.getObjects());
  154. for (int i = 0; i < cat.getObjects().size(); i++) {
  155. if (cat.getObjects().get(i).getCompareName().equals(toDelete)) {
  156. cat.getObjects().remove(i);
  157. cat.getObjects();
  158. notifyCatListeners();
  159. }
  160. }
  161. }
  162. public void print(ArrayList<CpsObject> iterate) {
  163. for (CpsObject cps : iterate) {
  164. System.out.println(cps.getCompareName());
  165. }
  166. }
  167. /**
  168. *
  169. * @param catLis
  170. */
  171. public void addCategoryListener(CategoryListener catLis) {
  172. MODEL.getCategoryListeners().add(catLis);
  173. }
  174. /**
  175. * notifies all listeners about changes in the Categories
  176. */
  177. public void notifyCatListeners() {
  178. for (CategoryListener l : MODEL.getCategoryListeners()) {
  179. l.onChange(MODEL.getCategories());
  180. }
  181. }
  182. /**
  183. * search for category
  184. *
  185. * @param name
  186. * @return
  187. */
  188. public Category searchCatNode(String name) {
  189. Category query = null;
  190. for (Category cat : MODEL.getCategories()) {
  191. if (cat.getName().equals(name)) {
  192. query = cat;
  193. break;
  194. }
  195. }
  196. return query;
  197. }
  198. /**
  199. * gets the position of an object in the array
  200. *
  201. * @param arrayList
  202. * @param toSearch
  203. * @return -1 if object is not in the array, otherwise the index
  204. */
  205. public int getPositionInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
  206. for (int i = 0; i < arrayList.size(); i++) {
  207. if (arrayList.get(i).getCompareName() == toSearch.getCompareName()) {
  208. return i;
  209. }
  210. }
  211. return -1;
  212. }
  213. public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
  214. for (ComparableObject obj : arrayList) {
  215. if (obj.getCompareName().equals(toSearch.getCompareName())) {
  216. return true;
  217. }
  218. }
  219. return false;
  220. }
  221. }