CategoryController.java 6.6 KB

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