ObjectController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package ui.controller;
  2. import java.util.ArrayList;
  3. import classes.Category;
  4. import classes.CpsUpperNode;
  5. import classes.AbstractCpsObject;
  6. import classes.HolonElement;
  7. import classes.HolonObject;
  8. import javafx.util.Pair;
  9. import ui.model.Model;
  10. /**
  11. * Controller for Objects.
  12. *
  13. * @author Gruppe14
  14. */
  15. public class ObjectController {
  16. private Model model;
  17. private MultiPurposeController mpC;
  18. /**
  19. * Constructor.
  20. *
  21. * @param model
  22. * Model
  23. * @param mp
  24. * MultiPurposeController
  25. */
  26. public ObjectController(Model model, MultiPurposeController mp) {
  27. this.model = model;
  28. this.mpC = mp;
  29. initHolonElements();
  30. }
  31. /**
  32. * init default Power supply of the Power Plant.
  33. */
  34. public void initHolonElements() {
  35. addNewElementIntoCategoryObject("Energy", "Power Plant", "Power", 1, 10000);
  36. addNewElementIntoCategoryObject("Building", "House", "TV", 2, -250);
  37. addNewElementIntoCategoryObject("Building", "House", "Fridge", 1, -500);
  38. addNewElementIntoCategoryObject("Building", "House", "Radio", 1, -100);
  39. addNewElementIntoCategoryObject("Building", "House", "PC", 3, -250);
  40. addNewElementIntoCategoryObject("Building", "House", "Light", 5, -50);
  41. addNewElementIntoCategoryObject("Building", "House", "Solar Panels", 1, 300);
  42. }
  43. /**
  44. * Adds Element into a Object.
  45. *
  46. * @param object
  47. * the Object
  48. * @param element
  49. * the Element
  50. */
  51. public void addElement(HolonObject object, HolonElement element) {
  52. object.getEleIdx().put(element.getEleName(), object.getElements().size());
  53. object.getElements().add(element);
  54. }
  55. /**
  56. * Adds Element into a Object on the Canvas.
  57. *
  58. * @param object
  59. * the Object
  60. * @param element
  61. * the Element
  62. */
  63. public void addElementIntoCanvasObject(HolonObject object, HolonElement element) {
  64. element.setSaving(null);
  65. addElement(object, element);
  66. }
  67. /**
  68. * Add a new Element into a Object on the Canvas.
  69. *
  70. * @param id
  71. * the Object ID
  72. * @param element
  73. * the Name of the Element
  74. * @param amount
  75. * the Amount
  76. * @param energy
  77. * the Energy
  78. */
  79. public void addNewElementIntoCanvasObject(int id, String element, int amount, float energy) {
  80. HolonElement ele = new HolonElement(element, amount, energy);
  81. if ((HolonObject) mpC.searchByID(id) == null) {
  82. addElementIntoCanvasObject((HolonObject) model.getSelectedCpsObjects().get(0), ele);
  83. } else {
  84. addElementIntoCanvasObject((HolonObject) mpC.searchByID(id), ele);
  85. }
  86. }
  87. /**
  88. * Add Element into a Object in Category.
  89. *
  90. * @param category
  91. * the Category
  92. *
  93. * @param object
  94. * the Object
  95. * @param element
  96. * the Element
  97. */
  98. public void addElementIntoCategoryObject(String category, String object, HolonElement element) {
  99. element.setSaving(new Pair<String, String>(category, object));
  100. addElement((HolonObject) mpC.searchCatObj(mpC.searchCat(category), object), element);
  101. }
  102. /**
  103. * Add a new Element into a Object in Category.
  104. *
  105. * @param category
  106. * the Category
  107. * @param object
  108. * the Object
  109. * @param element
  110. * the Element Name
  111. * @param energy
  112. * the Energy
  113. * @param amount
  114. * the amount
  115. */
  116. public void addNewElementIntoCategoryObject(String category, String object, String element, int amount,
  117. float energy) {
  118. HolonElement ele = new HolonElement(element, amount, energy);
  119. addElementIntoCategoryObject(category, object, ele);
  120. }
  121. /**
  122. * deletes a Element from a given Object.
  123. *
  124. * @param obj
  125. * the Oject
  126. * @param ele
  127. * the Element
  128. */
  129. public void deleteElement(HolonObject obj, HolonElement ele) {
  130. System.out.println(ele.getEleName() + " and " + obj.getEleIdx());
  131. mpC.decIdx(ele.getEleName(), obj.getEleIdx());
  132. obj.getEleIdx().remove(ele.getEleName());
  133. obj.getElements().remove(ele);
  134. }
  135. /**
  136. * deletes a selectedObject.
  137. *
  138. * @param obj
  139. * Cpsobject
  140. */
  141. public void deleteSelectedObject(AbstractCpsObject obj) {
  142. model.getSelectedCpsObjects().remove(obj);
  143. }
  144. /**
  145. * add an Object to selectedObject.
  146. *
  147. * @param obj
  148. * AbstractCpsobject
  149. */
  150. public void addSelectedObject(AbstractCpsObject obj) {
  151. model.getSelectedCpsObjects().add(obj);
  152. }
  153. /**
  154. * deletes a Element from a given Canvas Object.
  155. *
  156. * @param id
  157. * the ID
  158. * @param eleid
  159. * the Element ID
  160. */
  161. public void deleteElementInCanvas(int id, int eleid) {
  162. HolonObject object = (HolonObject) mpC.searchByID(id);
  163. if (object == null) {
  164. object = (HolonObject) model.getSelectedCpsObjects().get(0);
  165. }
  166. HolonElement element = mpC.searchEleById(object, eleid);
  167. // mpC.searchEle(object, ele);
  168. deleteElement(object, element);
  169. }
  170. /**
  171. * deletes a Element from a given Category Object.
  172. *
  173. * @param cat
  174. * the Category
  175. * @param obj
  176. * the Object
  177. * @param ele
  178. * the Element
  179. */
  180. public void deleteElementInCategory(String cat, String obj, String ele) {
  181. Category category = mpC.searchCat(cat);
  182. HolonObject object = (HolonObject) mpC.searchCatObj(category, obj);
  183. HolonElement element = mpC.searchEle(object, ele);
  184. deleteElement(object, element);
  185. }
  186. /**
  187. * Returns the ID of the selected Object 0 = no Object is selected.
  188. *
  189. * @param id
  190. * the ID of the selected Object
  191. */
  192. public void setSelectedObjectID(int id) {
  193. model.setSelectedObjectID(id);
  194. }
  195. /**
  196. * Get the number of HolonObjects in the given List
  197. *
  198. * @param list
  199. */
  200. public int getNumberHolonObjects(ArrayList<AbstractCpsObject> list) {
  201. int val = 0;
  202. for (AbstractCpsObject obj : list) {
  203. if (obj instanceof HolonObject) {
  204. val++;
  205. } else if (obj instanceof CpsUpperNode) {
  206. val += getNumberHolonObjects(((CpsUpperNode) obj).getNodes());
  207. }
  208. }
  209. return val;
  210. }
  211. /**
  212. * Get the number of HolonObjects with the given state in the given List
  213. *
  214. * @param list
  215. * @param state
  216. */
  217. public int getNumberStateObjects(ArrayList<AbstractCpsObject> list, int state) {
  218. int val = 0;
  219. for (AbstractCpsObject obj : list) {
  220. if (obj instanceof HolonObject) {
  221. if (((HolonObject) obj).getState() == state || (state == 2 && ((HolonObject) obj).getState() == 3)) {
  222. val++;
  223. }
  224. } else if (obj instanceof CpsUpperNode) {
  225. val += getNumberStateObjects(((CpsUpperNode) obj).getNodes(), state);
  226. }
  227. }
  228. return val;
  229. }
  230. /**
  231. * add the ID of a HolonBody.
  232. *
  233. * @param i
  234. * int
  235. */
  236. public void addSelectedHolonBody(int i) {
  237. model.setSelectedHolonBody(i);
  238. }
  239. }