MultiPurposeController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * Search Edge between 2 Objects.
  98. *
  99. * @param a
  100. * ID of Object a
  101. * @param b
  102. * ID of Object b
  103. * @return The Edge
  104. */
  105. public Edge searchEdge(int a, int b) {
  106. AbstractCanvasObject objA = searchByID(a);
  107. AbstractCanvasObject objB = searchByID(b);
  108. for (Edge edge : model.getEdgesOnCanvas()) {
  109. // if (edge.getA().getObjName().equals(A.getObjName()) &&
  110. // (edge.getB().getObjName().equals(B.getObjName()))
  111. // || edge.getB().getObjName().equals(A.getObjName())
  112. // && (edge.getA().getObjName().equals(B.getObjName())))
  113. if ((edge.getA().equals(objA) && edge.getB().equals(objB))
  114. || (edge.getB().equals(objA)) && edge.getA().equals(objB))
  115. return edge;
  116. }
  117. return null;
  118. }
  119. /**
  120. * Decrement the Indices if a Key as been removed.
  121. *
  122. * @param key
  123. * the Key
  124. * @param <T>
  125. * key type
  126. * @param map
  127. * the Map
  128. */
  129. public <T> void decIdx(T key, HashMap<T, Integer> map) {
  130. if(!map.containsKey(key)) {
  131. return;
  132. }
  133. for (Entry<T, Integer> i : map.entrySet()) {
  134. if (i.getValue() > map.get(key))
  135. i.setValue(i.getValue() - 1);
  136. }
  137. }
  138. /**
  139. * Adjust Indices before porting them from one map to another
  140. *
  141. * @param key
  142. * the Key
  143. * @param <T>
  144. * key type
  145. * @param map
  146. * the Map
  147. */
  148. public <T> void adjustIdx(int x, HashMap<T, Integer> map) {
  149. for (Entry<T, Integer> i : map.entrySet()) {
  150. i.setValue(i.getValue() + x + 1);
  151. }
  152. }
  153. /**
  154. * Returns the highest Id
  155. *
  156. * @param map
  157. * @return
  158. */
  159. public <T> int getHighestIdx(HashMap<T, Integer> map) {
  160. int max = 0;
  161. for (T i : map.keySet()) {
  162. if (map.get(i) > max)
  163. max = map.get(i);
  164. }
  165. return max;
  166. }
  167. /**
  168. * Copies a HashMap into a new One.
  169. *
  170. * @param map
  171. * the HashMap
  172. * @param <T>
  173. * type
  174. * @return Copy of the HashMap
  175. */
  176. public static <T> HashMap<T, Integer> copyHashMap(HashMap<T, Integer> map) {
  177. HashMap<T, Integer> newMap = new HashMap<>();
  178. for (Entry<T, Integer> i : map.entrySet()) {
  179. newMap.put(i.getKey(), i.getValue());
  180. }
  181. return newMap;
  182. }
  183. }