CategoryController.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. powerp.setImage("/Images/Dummy_PowerPlant.png");
  31. HolonObject house = new HolonObject("House");
  32. house.setImage("/Images/Dummy_House.png");
  33. HolonTransformer transformer = new HolonTransformer("Transformer");
  34. transformer.setImage("/Images/Dummy_Transformator.png");
  35. HolonSwitch sw = new HolonSwitch("Switch");
  36. sw.setImage("/Images/Dummy_Switch.png");
  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(M.getCategories(), toAdd)) {
  56. number++;
  57. toAdd.setName(name + "_" + number);
  58. }
  59. ;
  60. M.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(M.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. M.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 obj, String objType) {
  114. addObject(cat, new HolonObject(objType, obj));
  115. }
  116. /**
  117. * Add new Holon Transformer
  118. *
  119. * @param cat
  120. * Category
  121. * @param obj
  122. * New Object Name
  123. */
  124. public void addNewHolonTransformer(Category cat, String obj, String objType) {
  125. addObject(cat, new HolonTransformer(objType, obj));
  126. }
  127. /**
  128. * Add new Holon Switch
  129. *
  130. * @param cat
  131. * Category
  132. * @param obj
  133. * New Object Name
  134. */
  135. public void addNewHolonSwitch(Category cat, String obj, String objType) {
  136. addObject(cat, new HolonSwitch(objType, obj));
  137. }
  138. /**
  139. * deletes given Object in given Category
  140. *
  141. * @param toDelete
  142. * @param deleteIn
  143. */
  144. public void deleteObjectInCat(String toDelete, String deleteIn) {
  145. Category cat = searchCatNode(deleteIn);
  146. print(cat.getObjects());
  147. for (int i = 0; i < cat.getObjects().size(); i++) {
  148. if (cat.getObjects().get(i).getCompareName().equals(toDelete)) {
  149. cat.getObjects().remove(i);
  150. cat.getObjects();
  151. notifyCatListeners();
  152. }
  153. }
  154. }
  155. public void print(ArrayList<CpsObject> iterate) {
  156. for (CpsObject cps : iterate) {
  157. System.out.println(cps.getCompareName());
  158. }
  159. }
  160. /**
  161. *
  162. * @param catLis
  163. */
  164. public void addCatListener(CategoryListener catLis) {
  165. M.getCategoryListeners().add(catLis);
  166. }
  167. /**
  168. * notifies all listeners about changes in the Categories
  169. */
  170. public void notifyCatListeners() {
  171. for (CategoryListener l : M.getCategoryListeners()) {
  172. l.onChange(M.getCategories());
  173. }
  174. }
  175. /**
  176. * search for category
  177. *
  178. * @param name
  179. * @return
  180. */
  181. public Category searchCatNode(String name) {
  182. Category query = null;
  183. for (Category cat : M.getCategories()) {
  184. if (cat.getName().equals(name)) {
  185. query = cat;
  186. break;
  187. }
  188. }
  189. return query;
  190. }
  191. /**
  192. * gets the position of an object in the array
  193. *
  194. * @param arrayList
  195. * @param toSearch
  196. * @return -1 if object is not in the array, otherwise the index
  197. */
  198. public int getPositionInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
  199. for (int i = 0; i < arrayList.size(); i++) {
  200. if (arrayList.get(i).getCompareName() == toSearch.getCompareName()) {
  201. return i;
  202. }
  203. }
  204. return -1;
  205. }
  206. public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch) {
  207. for (ComparableObject obj : arrayList) {
  208. if (obj.getCompareName().equals(toSearch.getCompareName())) {
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. }