CategoryController.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import classes.Category;
  4. import classes.HolonBattery;
  5. import classes.AbstractCpsObject;
  6. import classes.HolonElement;
  7. import classes.HolonObject;
  8. import classes.HolonSwitch;
  9. import classes.HolonTransformer;
  10. import classes.Pair;
  11. import interfaces.CategoryListener;
  12. import ui.model.Model;
  13. /**
  14. * Controller for the Categories.
  15. *
  16. * @author Gruppe14
  17. */
  18. public class CategoryController {
  19. private Model model;
  20. private MultiPurposeController mpC;
  21. /**
  22. * Constructor.
  23. *
  24. * @param model
  25. * the Model
  26. * @param mp
  27. * the MultiPurposeController
  28. */
  29. public CategoryController(Model model, MultiPurposeController mp) {
  30. this.model = model;
  31. this.mpC = mp;
  32. initCategories();
  33. }
  34. /**
  35. * init default category and objects.
  36. */
  37. public void initCategories() {
  38. addNewCategory("Energy");
  39. addNewCategory("Building");
  40. addNewCategory("Component");
  41. addNewHolonObject(mpC.searchCat("Energy"), "Power Plant", new ArrayList<HolonElement>(),
  42. "/Images/power-plant.png");
  43. addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
  44. addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
  45. addNewHolonBattery(mpC.searchCat("Component"), "Battery", "/Images/battery.png");
  46. }
  47. /**
  48. * Adds Category into Model if a Category with the same name already exists
  49. *
  50. * @param category
  51. * the new Category
  52. */
  53. public void addCategory(Category category) {
  54. // int number = 0;
  55. // String name = toAdd.getName();
  56. // while (sC.containsInList(MODEL.getCategories(), toAdd)) {
  57. // number++;
  58. // toAdd.setName(name + "_" + number);
  59. // }
  60. int i = 0;
  61. while (mpC.searchCat(category.getName()) != null) {
  62. if (category.getName().contains("_"))
  63. category.setName(category.getName().substring(0, category.getName().indexOf('_')));
  64. category.setName(category.getName() + "_" + i);
  65. i++;
  66. }
  67. model.getCgIdx().put(category.getName(), model.getCategories().size());
  68. model.getCategories().add(category);
  69. notifyCatListeners();
  70. }
  71. /**
  72. * Adds New Category into Model.
  73. *
  74. * @param name
  75. * Bezeichnung der neuen Kategorie
  76. */
  77. public void addNewCategory(String name) {
  78. addCategory(new Category(name));
  79. }
  80. /**
  81. * remove a Category from Model.
  82. *
  83. * @param c
  84. * Category
  85. */
  86. public void removeCategory(Category c) {
  87. mpC.decIdx(c.getName(), model.getCgIdx());
  88. model.getCgIdx().remove(c.getName());
  89. model.getCategories().remove(c);
  90. notifyCatListeners();
  91. }
  92. /**
  93. * get All Categories
  94. *
  95. * @return the ArrayList of all Categories
  96. */
  97. public ArrayList<Category> getCategories()
  98. {
  99. return model.getCategories();
  100. }
  101. /**
  102. * delete a given Category.
  103. *
  104. * @param category
  105. * the Category
  106. */
  107. public void deleteCategory(String category) {
  108. removeCategory(mpC.searchCat(category));
  109. }
  110. /**
  111. * Add Object into a Category.
  112. *
  113. * @param category
  114. * Category
  115. * @param object
  116. * Object
  117. */
  118. public void addObject(Category category, AbstractCpsObject object) {
  119. int i = 0;
  120. boolean updateElementSaves = false;
  121. String name = "";
  122. while (mpC.searchCatObj(category, object.getObjName()) != null) {
  123. updateElementSaves = true;
  124. if (object.getObjName().contains("_"))
  125. object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
  126. name = object.getObjName() + "_" + i;
  127. object.setObjName(name);
  128. object.setName(name);
  129. i++;
  130. }
  131. if(updateElementSaves && object instanceof HolonObject &&((HolonObject)object).getElements()!=null){
  132. for(HolonElement e: ((HolonObject)object).getElements()){
  133. e.setSaving(new Pair<String,String>(e.getSaving().getKey(), name));
  134. }
  135. }
  136. category.getObjIdx().put(object.getObjName(), category.getObjects().size());
  137. category.getObjects().add(object);
  138. notifyCatListeners();
  139. }
  140. /**
  141. * Add new Holon Object to a Category.
  142. *
  143. * @param category
  144. * Category
  145. * @param object
  146. * New Object Name
  147. * @param elements
  148. * Array of Elements
  149. * @param image
  150. * the image Path
  151. */
  152. public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
  153. HolonObject obj = new HolonObject(object);
  154. obj.setImage(image);
  155. obj.setElements(elements);
  156. obj.setSav(category.getName());
  157. addObject(category, obj);
  158. }
  159. /**
  160. * Add new Holon Transformer.
  161. *
  162. * @param cat
  163. * Category
  164. * @param objName
  165. * New Object Name
  166. * @param image
  167. * the image Path
  168. */
  169. public void addNewHolonTransformer(Category cat, String objName, String image) {
  170. HolonTransformer transformer = new HolonTransformer(objName);
  171. transformer.setImage(image);
  172. transformer.setSav(cat.getName());
  173. addObject(cat, transformer);
  174. }
  175. /**
  176. * Add new Holon Switch.
  177. *
  178. * @param cat
  179. * Category
  180. * @param objName
  181. * New Object Name
  182. * @param image
  183. * the Image Path
  184. */
  185. public void addNewHolonSwitch(Category cat, String objName, String image) {
  186. HolonSwitch holonSwitch = new HolonSwitch(objName);
  187. holonSwitch.setImage(image);
  188. holonSwitch.setSav(cat.getName());
  189. addObject(cat, holonSwitch);
  190. }
  191. /**
  192. * Add new Holon Battery
  193. * @param cat
  194. * Categorx
  195. * @param objName
  196. * New Object Name
  197. * @param image
  198. * The Image Path
  199. */
  200. public void addNewHolonBattery(Category cat, String objName, String image) {
  201. HolonBattery holonBat = new HolonBattery(objName);
  202. holonBat.setImage(image);
  203. holonBat.setSav(cat.getName());
  204. addObject(cat, holonBat);
  205. }
  206. public void addNewHolonBattery(Category cat, HolonBattery holonBat, String image) {
  207. holonBat.setImage(image);
  208. holonBat.setSav(cat.getName());
  209. addObject(cat, holonBat);
  210. }
  211. /**
  212. * Removes an Object from a Category.
  213. * @param category Category
  214. * @param cps the Object
  215. */
  216. public void removeObject(Category category, AbstractCpsObject cps) {
  217. mpC.decIdx(cps.getObjName(), category.getObjIdx());
  218. category.getObjIdx().remove(cps.getObjName());
  219. category.getObjects().remove(cps);
  220. notifyCatListeners();
  221. }
  222. /**
  223. * Delete an Object from a Category.
  224. *
  225. * @param category
  226. * the Category
  227. * @param obj
  228. * the Object
  229. */
  230. public void deleteObject(String category, String obj) {
  231. Category cat = mpC.searchCat(category);
  232. removeObject(cat, mpC.searchCatObj(cat, obj));
  233. }
  234. /**
  235. * Init the CategoryListener.
  236. *
  237. * @param catLis
  238. * the CategoryListener
  239. */
  240. public void addCategoryListener(CategoryListener catLis) {
  241. model.getCategoryListeners().add(catLis);
  242. }
  243. /**
  244. * notifies all listeners about changes in the Categories.
  245. */
  246. public void notifyCatListeners() {
  247. for (CategoryListener l : model.getCategoryListeners()) {
  248. l.onChange(model.getCategories());
  249. }
  250. }
  251. }