CategoryController.java 4.9 KB

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