CategoryController.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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
  44. * if a Category with the same name already exists
  45. * Add Category_+1
  46. *
  47. * @param toAdd
  48. * neue Kategorie
  49. */
  50. public void addCategory(Category toAdd) {
  51. int number = 0;
  52. String name = toAdd.getName();
  53. while(containsInList( M.getCategories(),toAdd)){
  54. number++;
  55. toAdd.setName(name + "_" + number);
  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. * @param catName
  72. * @return true if successfull, otherwise false
  73. */
  74. public boolean deleteCategory(String catName){
  75. int position = getPositionInList(M.getCategories(), searchCatNode(catName));
  76. if(position != -1){
  77. deleteCategoryAt(position);
  78. return true;
  79. }
  80. else{
  81. return false;
  82. }
  83. }
  84. /**
  85. * deletes a Category at given position
  86. */
  87. public void deleteCategoryAt(int idx) {
  88. M.getCategories().remove(idx);
  89. notifyCatListeners();
  90. }
  91. /**
  92. * Add Object into a Category
  93. *
  94. * @param cat
  95. * Category
  96. * @param obj
  97. * Object
  98. */
  99. public void addObject(Category cat, CpsObject obj) {
  100. cat.getObjects().add(obj);
  101. notifyCatListeners();
  102. }
  103. /**
  104. * Add new Holon Object
  105. *
  106. * @param cat
  107. * Category
  108. * @param obj
  109. * New Object Name
  110. */
  111. public void addNewHolonObject(Category cat, String obj) {
  112. addObject(cat, new HolonObject(obj));
  113. }
  114. /**
  115. * Add new Holon Transformer
  116. *
  117. * @param cat
  118. * Category
  119. * @param obj
  120. * New Object Name
  121. */
  122. public void addNewHolonTransformer(Category cat, String obj) {
  123. addObject(cat, new HolonTransformer(obj));
  124. }
  125. /**
  126. * Add new Holon Switch
  127. *
  128. * @param cat
  129. * Category
  130. * @param obj
  131. * New Object Name
  132. */
  133. public void addNewHolonSwitch(Category cat, String obj) {
  134. addObject(cat, new HolonSwitch(obj));
  135. }
  136. /**
  137. * deletes given Object in given Category
  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. * @param name
  175. * @return
  176. */
  177. public Category searchCatNode(String name) {
  178. Category query = null;
  179. for (Category cat : M.getCategories()) {
  180. if (cat.getName().equals(name)) {
  181. query = cat;
  182. break;
  183. }
  184. }
  185. return query;
  186. }
  187. /**
  188. * gets the position of an object in the array
  189. * @param arrayList
  190. * @param toSearch
  191. * @return -1 if object is not in the array, otherwise the index
  192. */
  193. public int getPositionInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch){
  194. for(int i = 0;i < arrayList.size(); i++){
  195. if(arrayList.get(i).getCompareName() == toSearch.getCompareName()){
  196. return i;
  197. }
  198. }
  199. return -1;
  200. }
  201. public boolean containsInList(ArrayList<? extends ComparableObject> arrayList, ComparableObject toSearch){
  202. for(ComparableObject obj : arrayList){
  203. if(obj.getCompareName().equals(toSearch.getCompareName())){
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. }