MultiPurposeController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package ui.controller;
  2. import java.util.HashMap;
  3. import java.util.Map.Entry;
  4. import classes.Category;
  5. import classes.Edge;
  6. import classes.GroupNode;
  7. import classes.AbstractCanvasObject;
  8. import classes.HolonElement;
  9. import classes.HolonObject;
  10. import ui.model.Model;
  11. /**
  12. * Controller for Multiple Purposes.
  13. *
  14. * @author Gruppe14
  15. */
  16. public class MultiPurposeController {
  17. private Model model;
  18. /**
  19. * Constructor.
  20. *
  21. * @param model
  22. * Model
  23. */
  24. public MultiPurposeController(Model model) {
  25. this.model = model;
  26. }
  27. /**
  28. * search for category.
  29. *
  30. * @param category
  31. * name of the Category
  32. * @return the Category
  33. */
  34. public Category searchCat(String category) {
  35. Integer idx;
  36. if ((idx = model.getCgIdx().get(category)) == null || model.getCgIdx().size() < 1)
  37. return null;
  38. else
  39. return model.getCategories().get(idx);
  40. }
  41. /**
  42. * Search for Object in a Category.
  43. *
  44. * @param category
  45. * name of the Category
  46. * @param object
  47. * Name of the Object
  48. * @return The Object
  49. */
  50. public AbstractCanvasObject searchCatObj(Category category, String object) {
  51. Integer idx;
  52. if ((idx = category.getObjIdx().get(object)) == null || category.getObjIdx().size() < 1)
  53. return null;
  54. else
  55. return category.getObjects().get(idx);
  56. }
  57. /**
  58. * Search for Object by ID.
  59. *
  60. * @param id
  61. * the ID of the Object
  62. * @return the CpsObject
  63. */
  64. public AbstractCanvasObject searchByID(int id) {
  65. Integer idx;
  66. if ((idx = model.getCvsObjIdx().get(id)) == null || model.getCvsObjIdx().size() < 1)
  67. return null;
  68. else
  69. return model.getObjectsOnCanvas().get(idx);
  70. }
  71. /**
  72. *
  73. * @param upperNode
  74. * @param id
  75. * @return
  76. */
  77. public AbstractCanvasObject searchByIDUpperNode(int id, GroupNode upperNode) {
  78. Integer idx;
  79. if ((idx = upperNode.getNodesIdx().get(id)) == null || upperNode.getNodesIdx().size() < 1)
  80. return null;
  81. else
  82. return upperNode.getNodes().get(idx);
  83. }
  84. /**
  85. * Search the Element by ID.
  86. *
  87. * @param object
  88. * the Holon Object
  89. * @param idEle
  90. * the Element ID
  91. * @return The Holon Element
  92. */
  93. public HolonElement searchEleById(HolonObject object, int idEle) {
  94. return object.searchElementById(idEle);
  95. }
  96. /**
  97. *
  98. * @param id
  99. * @return
  100. */
  101. public AbstractCanvasObject searchTracked(int id) {
  102. for (AbstractCanvasObject cps : model.getTrackingObj()) {
  103. if (cps.getId() == id)
  104. return cps;
  105. }
  106. return null;
  107. }
  108. /**
  109. * Search Edge between 2 Objects.
  110. *
  111. * @param a
  112. * ID of Object a
  113. * @param b
  114. * ID of Object b
  115. * @return The Edge
  116. */
  117. public Edge searchEdge(int a, int b) {
  118. AbstractCanvasObject objA = searchByID(a);
  119. AbstractCanvasObject objB = searchByID(b);
  120. for (Edge edge : model.getEdgesOnCanvas()) {
  121. // if (edge.getA().getObjName().equals(A.getObjName()) &&
  122. // (edge.getB().getObjName().equals(B.getObjName()))
  123. // || edge.getB().getObjName().equals(A.getObjName())
  124. // && (edge.getA().getObjName().equals(B.getObjName())))
  125. if ((edge.getA().equals(objA) && edge.getB().equals(objB))
  126. || (edge.getB().equals(objA)) && edge.getA().equals(objB))
  127. return edge;
  128. }
  129. return null;
  130. }
  131. /**
  132. * Decrement the Indices if a Key as been removed.
  133. *
  134. * @param key
  135. * the Key
  136. * @param <T>
  137. * key type
  138. * @param map
  139. * the Map
  140. */
  141. public <T> void decIdx(T key, HashMap<T, Integer> map) {
  142. if(!map.containsKey(key)) {
  143. return;
  144. }
  145. for (Entry<T, Integer> i : map.entrySet()) {
  146. if (i.getValue() > map.get(key))
  147. i.setValue(i.getValue() - 1);
  148. }
  149. }
  150. /**
  151. * Adjust Indices before porting them from one map to another
  152. *
  153. * @param key
  154. * the Key
  155. * @param <T>
  156. * key type
  157. * @param map
  158. * the Map
  159. */
  160. public <T> void adjustIdx(int x, HashMap<T, Integer> map) {
  161. for (Entry<T, Integer> i : map.entrySet()) {
  162. i.setValue(i.getValue() + x + 1);
  163. }
  164. }
  165. /**
  166. * Returns the highest Id
  167. *
  168. * @param map
  169. * @return
  170. */
  171. public <T> int getHighestIdx(HashMap<T, Integer> map) {
  172. int max = 0;
  173. for (T i : map.keySet()) {
  174. if (map.get(i) > max)
  175. max = map.get(i);
  176. }
  177. return max;
  178. }
  179. /**
  180. * Copies a HashMap into a new One.
  181. *
  182. * @param map
  183. * the HashMap
  184. * @param <T>
  185. * type
  186. * @return Copy of the HashMap
  187. */
  188. public static <T, Integer> HashMap<T, Integer> copyHashMap(HashMap<T, Integer> map) {
  189. HashMap<T, Integer> newMap = new HashMap<>();
  190. for (Entry<T, Integer> i : map.entrySet()) {
  191. newMap.put(i.getKey(), i.getValue());
  192. }
  193. return newMap;
  194. }
  195. /**
  196. * Set the Algorithm.
  197. *
  198. * @param obj
  199. * the Algorithm
  200. */
  201. public void setAlgorithm(Object obj) {
  202. model.setAlgorithm(obj);
  203. }
  204. }