CategoryController.java 6.8 KB

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