CategoryController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import classes.Category;
  4. import classes.HolonBattery;
  5. import classes.AbstractCanvasObject;
  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 i = 0;
  55. while (mpC.searchCat(category.getName()) != null) {
  56. if (category.getName().contains("_"))
  57. category.setName(category.getName().substring(0, category.getName().indexOf('_')));
  58. category.setName(category.getName() + "_" + i);
  59. i++;
  60. }
  61. model.getCgIdx().put(category.getName(), model.getCategories().size());
  62. model.getCategories().add(category);
  63. notifyCatListeners();
  64. }
  65. /**
  66. * Adds New Category into Model.
  67. *
  68. * @param name
  69. * Bezeichnung der neuen Kategorie
  70. */
  71. public void addNewCategory(String name) {
  72. addCategory(new Category(name));
  73. }
  74. /**
  75. * remove a Category from Model.
  76. *
  77. * @param c
  78. * Category
  79. */
  80. public void removeCategory(Category c) {
  81. mpC.decIdx(c.getName(), model.getCgIdx());
  82. model.getCgIdx().remove(c.getName());
  83. model.getCategories().remove(c);
  84. notifyCatListeners();
  85. }
  86. /**
  87. * get All Categories
  88. *
  89. * @return the ArrayList of all Categories
  90. */
  91. public ArrayList<Category> getCategories()
  92. {
  93. return model.getCategories();
  94. }
  95. /**
  96. * delete a given Category.
  97. *
  98. * @param category
  99. * the Category
  100. */
  101. public void deleteCategory(String category) {
  102. removeCategory(mpC.searchCat(category));
  103. }
  104. /**
  105. * Add Object into a Category.
  106. *
  107. * @param category
  108. * Category
  109. * @param object
  110. * Object
  111. */
  112. public void addObject(Category category, AbstractCanvasObject object) {
  113. int i = 0;
  114. boolean updateElementSaves = false;
  115. String name = "";
  116. while (mpC.searchCatObj(category, object.getObjName()) != null) {
  117. updateElementSaves = true;
  118. if (object.getObjName().contains("_"))
  119. object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
  120. name = object.getObjName() + "_" + i;
  121. object.setObjName(name);
  122. object.setName(name);
  123. i++;
  124. }
  125. if(updateElementSaves && object instanceof HolonObject &&((HolonObject)object).getElements()!=null){
  126. for(HolonElement e: ((HolonObject)object).getElements()){
  127. e.setSaving(new Pair<String,String>(e.getSaving().getKey(), name));
  128. }
  129. }
  130. category.getObjIdx().put(object.getObjName(), category.getObjects().size());
  131. category.getObjects().add(object);
  132. notifyCatListeners();
  133. }
  134. /**
  135. * Add new Holon Object to a Category.
  136. *
  137. * @param category
  138. * Category
  139. * @param object
  140. * New Object Name
  141. * @param elements
  142. * Array of Elements
  143. * @param image
  144. * the image Path
  145. */
  146. public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
  147. HolonObject obj = new HolonObject(object);
  148. obj.setImage(image);
  149. obj.setElements(elements);
  150. obj.setSav(category.getName());
  151. addObject(category, obj);
  152. }
  153. /**
  154. * Add new Holon Switch.
  155. *
  156. * @param cat
  157. * Category
  158. * @param objName
  159. * New Object Name
  160. * @param image
  161. * the Image Path
  162. */
  163. public void addNewHolonSwitch(Category cat, String objName, String image) {
  164. HolonSwitch holonSwitch = new HolonSwitch(objName);
  165. holonSwitch.setImage(image);
  166. holonSwitch.setSav(cat.getName());
  167. addObject(cat, holonSwitch);
  168. }
  169. /**
  170. * Add new Holon Battery
  171. * @param cat
  172. * Categorx
  173. * @param objName
  174. * New Object Name
  175. * @param image
  176. * The Image Path
  177. */
  178. public void addNewHolonBattery(Category cat, String objName, String image) {
  179. HolonBattery holonBat = new HolonBattery(objName);
  180. holonBat.setImage(image);
  181. holonBat.setSav(cat.getName());
  182. addObject(cat, holonBat);
  183. }
  184. public void addNewHolonBattery(Category cat, HolonBattery holonBat, String image) {
  185. holonBat.setImage(image);
  186. holonBat.setSav(cat.getName());
  187. addObject(cat, holonBat);
  188. }
  189. /**
  190. * Removes an Object from a Category.
  191. * @param category Category
  192. * @param cps the Object
  193. */
  194. public void removeObject(Category category, AbstractCanvasObject cps) {
  195. mpC.decIdx(cps.getObjName(), category.getObjIdx());
  196. category.getObjIdx().remove(cps.getObjName());
  197. category.getObjects().remove(cps);
  198. notifyCatListeners();
  199. }
  200. /**
  201. * Delete an Object from a Category.
  202. *
  203. * @param category
  204. * the Category
  205. * @param obj
  206. * the Object
  207. */
  208. public void deleteObject(String category, String obj) {
  209. Category cat = mpC.searchCat(category);
  210. removeObject(cat, mpC.searchCatObj(cat, obj));
  211. }
  212. /**
  213. * Init the CategoryListener.
  214. *
  215. * @param catLis
  216. * the CategoryListener
  217. */
  218. public void addCategoryListener(CategoryListener catLis) {
  219. model.getCategoryListeners().add(catLis);
  220. }
  221. /**
  222. * notifies all listeners about changes in the Categories.
  223. */
  224. public void notifyCatListeners() {
  225. ArrayList<Category> categories = model.getCategories();
  226. for (CategoryListener l : model.getCategoryListeners()) {
  227. l.onChange(categories);
  228. }
  229. }
  230. }