CategoryController.java 5.2 KB

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