MultiPurposeController.java 4.3 KB

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