CategoryController.java 7.2 KB

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