CategoryController.java 5.8 KB

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