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