CategoryController.java 5.3 KB

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