CategoryController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import classes.Category;
  4. import classes.AbstractCanvasObject;
  5. import classes.HolonElement;
  6. import classes.HolonObject;
  7. import classes.HolonSwitch;
  8. import classes.Pair;
  9. import ui.model.Model;
  10. /**
  11. * Controller for the Categories.
  12. *
  13. * @author Gruppe14
  14. */
  15. public class CategoryController {
  16. private Model model;
  17. private MultiPurposeController mpC;
  18. /**
  19. * Constructor.
  20. *
  21. * @param model
  22. * the Model
  23. * @param mp
  24. * the MultiPurposeController
  25. */
  26. public CategoryController(Model model, MultiPurposeController mp) {
  27. this.model = model;
  28. this.mpC = mp;
  29. initCategories();
  30. }
  31. /**
  32. * init default category and objects.
  33. */
  34. public void initCategories() {
  35. addNewCategory("Energy");
  36. addNewCategory("Building");
  37. addNewCategory("Component");
  38. addNewHolonObject(mpC.searchCat("Energy"), "Power Plant", new ArrayList<HolonElement>(),
  39. "/Images/power-plant.png");
  40. addNewHolonObject(mpC.searchCat("Building"), "House", new ArrayList<HolonElement>(), "/Images/home-2.png");
  41. addNewHolonSwitch(mpC.searchCat("Component"), "Switch", "/Images/switch-on.png");
  42. }
  43. /**
  44. * Adds Category into Model if a Category with the same name already exists
  45. *
  46. * @param category
  47. * the new Category
  48. */
  49. public void addCategory(Category category) {
  50. int i = 0;
  51. while (mpC.searchCat(category.getName()) != null) {
  52. if (category.getName().contains("_"))
  53. category.setName(category.getName().substring(0, category.getName().indexOf('_')));
  54. category.setName(category.getName() + "_" + i);
  55. i++;
  56. }
  57. model.getCgIdx().put(category.getName(), model.getCategories().size());
  58. model.getCategories().add(category);
  59. }
  60. /**
  61. * Adds New Category into Model.
  62. *
  63. * @param name
  64. * Bezeichnung der neuen Kategorie
  65. */
  66. public void addNewCategory(String name) {
  67. addCategory(new Category(name));
  68. }
  69. /**
  70. * remove a Category from Model.
  71. *
  72. * @param c
  73. * Category
  74. */
  75. public void removeCategory(Category c) {
  76. mpC.decIdx(c.getName(), model.getCgIdx());
  77. model.getCgIdx().remove(c.getName());
  78. model.getCategories().remove(c);
  79. }
  80. /**
  81. * get All Categories
  82. *
  83. * @return the ArrayList of all Categories
  84. */
  85. public ArrayList<Category> getCategories()
  86. {
  87. return model.getCategories();
  88. }
  89. /**
  90. * delete a given Category.
  91. *
  92. * @param category
  93. * the Category
  94. */
  95. public void deleteCategory(String category) {
  96. removeCategory(mpC.searchCat(category));
  97. }
  98. /**
  99. * Add Object into a Category.
  100. *
  101. * @param category
  102. * Category
  103. * @param object
  104. * Object
  105. */
  106. public void addObject(Category category, AbstractCanvasObject object) {
  107. int i = 0;
  108. boolean updateElementSaves = false;
  109. String name = "";
  110. while (mpC.searchCatObj(category, object.getObjName()) != null) {
  111. updateElementSaves = true;
  112. if (object.getObjName().contains("_"))
  113. object.setObjName(object.getObjName().substring(0, object.getObjName().indexOf('_')));
  114. name = object.getObjName() + "_" + i;
  115. object.setObjName(name);
  116. object.setName(name);
  117. i++;
  118. }
  119. if(updateElementSaves && object instanceof HolonObject &&((HolonObject)object).getElements()!=null){
  120. for(HolonElement e: ((HolonObject)object).getElements()){
  121. e.setSaving(new Pair<String,String>(e.getSaving().getKey(), name));
  122. }
  123. }
  124. category.getObjIdx().put(object.getObjName(), category.getObjects().size());
  125. category.getObjects().add(object);
  126. }
  127. /**
  128. * Add new Holon Object to a Category.
  129. *
  130. * @param category
  131. * Category
  132. * @param object
  133. * New Object Name
  134. * @param elements
  135. * Array of Elements
  136. * @param image
  137. * the image Path
  138. */
  139. public void addNewHolonObject(Category category, String object, ArrayList<HolonElement> elements, String image) {
  140. HolonObject obj = new HolonObject(object);
  141. obj.setImage(image);
  142. obj.setElements(elements);
  143. obj.setSav(category.getName());
  144. addObject(category, obj);
  145. }
  146. /**
  147. * Add new Holon Switch.
  148. *
  149. * @param cat
  150. * Category
  151. * @param objName
  152. * New Object Name
  153. * @param image
  154. * the Image Path
  155. */
  156. public void addNewHolonSwitch(Category cat, String objName, String image) {
  157. HolonSwitch holonSwitch = new HolonSwitch(objName);
  158. holonSwitch.setImage(image);
  159. holonSwitch.setSav(cat.getName());
  160. addObject(cat, holonSwitch);
  161. }
  162. /**
  163. * Removes an Object from a Category.
  164. * @param category Category
  165. * @param cps the Object
  166. */
  167. public void removeObject(Category category, AbstractCanvasObject cps) {
  168. mpC.decIdx(cps.getObjName(), category.getObjIdx());
  169. category.getObjIdx().remove(cps.getObjName());
  170. category.getObjects().remove(cps);
  171. }
  172. /**
  173. * Delete an Object from a Category.
  174. *
  175. * @param category
  176. * the Category
  177. * @param obj
  178. * the Object
  179. */
  180. public void deleteObject(String category, String obj) {
  181. Category cat = mpC.searchCat(category);
  182. removeObject(cat, mpC.searchCatObj(cat, obj));
  183. }
  184. }