CanvasController.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package ui.controller;
  2. import java.awt.Point;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6. import classes.Edge;
  7. import classes.Node;
  8. import classes.GroupNode;
  9. import classes.AbstractCanvasObject;
  10. import classes.HolonObject;
  11. import classes.HolonSwitch;
  12. import classes.Position;
  13. import interfaces.ObjectListener;
  14. import ui.model.Model;
  15. /**
  16. * Controller for the Canvas.
  17. *
  18. * @author Gruppe14
  19. */
  20. public class CanvasController {
  21. private Model model;
  22. private MultiPurposeController mpC;
  23. /**
  24. * Constructor.
  25. *
  26. * @param model
  27. * the Model
  28. * @param mp
  29. * the MultipurposeController
  30. */
  31. public CanvasController(Model model, MultiPurposeController mp) {
  32. this.model = model;
  33. this.mpC = mp;
  34. }
  35. /**
  36. * Add an CpsObject to the model and notify the ObjectListener for update.
  37. *
  38. * @param object
  39. * CpsObject to be added.
  40. * @param replace when true objects could be replaced
  41. */
  42. public void addObject(AbstractCanvasObject object, boolean replace) {
  43. model.getCvsObjIdx().put(object.getId(), model.getObjectsOnCanvas().size());
  44. model.getObjectsOnCanvas().add(object);
  45. /**
  46. * check if we should drag & drop replace
  47. */
  48. if(!(object instanceof Node) && replace){
  49. /** x of the dragged Object */
  50. int x = object.getPosition().x;
  51. /** y of the dragged Object */
  52. int y = object.getPosition().y;
  53. /** distance treshold for replacement */
  54. int treshhold = model.getScale()/2;
  55. /** number of Objects that might be replaced (should be 1) */
  56. int replaceCounter = 0;
  57. /** last object that could be replaced */
  58. AbstractCanvasObject toBeReplaced = null;
  59. /** for each cps on Canvas */
  60. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()){
  61. /** same object -> ignore */
  62. if(cps == object)continue;
  63. /** x of object that might get replaced */
  64. int c_x = cps.getPosition().x;
  65. /** y of object that might get replaced */
  66. int c_y = cps.getPosition().y;
  67. /** if near enough */
  68. if(Math.abs(x-c_x)<treshhold && Math.abs(y-c_y)<treshhold){
  69. replaceCounter++;
  70. toBeReplaced = cps;
  71. }
  72. }
  73. /** if replacement of exactly one object possible */
  74. if(replaceCounter == 1 && toBeReplaced != null){
  75. replaceObjectOnCanvas(toBeReplaced, object);
  76. }
  77. }
  78. notifyObjListeners();
  79. }
  80. /**
  81. * Add a new Object.
  82. *
  83. * @param object
  84. * the Object
  85. */
  86. public void addNewObject(AbstractCanvasObject object) {
  87. object.setSav("CVS");
  88. // object.setConnections(new ArrayList<CpsEdge>());
  89. addObject(object, true);
  90. }
  91. /**
  92. * adds the ObjectListener.
  93. *
  94. * @param objLis
  95. * ObjectListener
  96. */
  97. public void addObjectListener(ObjectListener objLis) {
  98. model.getObjectListeners().add(objLis);
  99. }
  100. /**
  101. * notifies all listeners about changes in the Canvas.
  102. */
  103. public void notifyObjListeners() {
  104. for (ObjectListener l : model.getObjectListeners()) {
  105. l.onChange(model.getObjectsOnCanvas());
  106. }
  107. }
  108. /**
  109. * Deletes an CpsObject on the Canvas and its connections.
  110. *
  111. * @param obj
  112. * AbstractCpsObject
  113. */
  114. public void deleteObjectOnCanvas(AbstractCanvasObject obj) {
  115. removeAllConnectionsFromObject(obj);
  116. mpC.decIdx(obj.getId(), model.getCvsObjIdx());
  117. model.getCvsObjIdx().remove(obj.getId());
  118. model.getObjectsOnCanvas().remove(obj);
  119. notifyObjListeners();
  120. }
  121. /**
  122. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  123. * @param toBeReplaced the object that will be replaced
  124. * @param by the object that will replace it
  125. */
  126. public void replaceObjectOnCanvas(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
  127. //Replace edges
  128. ListIterator<Edge> iter = model.getEdgesOnCanvas().listIterator();
  129. while(iter.hasNext() ) {
  130. Edge edge = iter.next();
  131. if(edge.getA() == toBeReplaced && edge.getB() != by) {
  132. edge.setA(by);
  133. }
  134. else if( edge.getB() == toBeReplaced && edge.getA() != by) {
  135. edge.setB(by);
  136. }
  137. }
  138. /** delete 'toBeReplaced' new empty connections, to prevent Nullpointer*/
  139. toBeReplaced.setConnections(new ArrayList<Edge>(1));
  140. /**
  141. * set Position of by to exactly toBeReplaced
  142. */
  143. by.setPosition(toBeReplaced.getPosition());
  144. deleteObjectOnCanvas(toBeReplaced);
  145. }
  146. /**
  147. * Add an edge to the Canvas.
  148. *
  149. * @param edge
  150. * the edge
  151. */
  152. public void addEdgeOnCanvas(Edge edge) {
  153. model.getEdgesOnCanvas().add(edge);
  154. }
  155. /**
  156. * Removes an Edge from the Canvas.
  157. *
  158. * @param edge
  159. * the edge to remove
  160. */
  161. public void removeEdgesOnCanvas(Edge edge) {
  162. edge.getA().getConnections().remove(edge);
  163. edge.getB().getConnections().remove(edge);
  164. model.getEdgesOnCanvas().remove(edge);
  165. }
  166. /**
  167. * Paste all Selected Objects.
  168. *
  169. * @param p
  170. * the mouse Position
  171. */
  172. public void pasteObjects(Point p) {
  173. model.getSelectedCpsObjects().clear();
  174. AbstractCanvasObject tCps = null;
  175. int x = Integer.MAX_VALUE, y = Integer.MAX_VALUE;
  176. // Location whre to copy the Elements
  177. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  178. if (cps.getPosition().x < x) {
  179. x = cps.getPosition().x;
  180. }
  181. if (cps.getPosition().y < y) {
  182. y = cps.getPosition().y;
  183. }
  184. }
  185. ArrayList<AbstractCanvasObject> tempList = new ArrayList<>();
  186. // Objects
  187. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  188. if (cps instanceof HolonObject) {
  189. tCps = new HolonObject((HolonObject) cps);
  190. } else if (cps instanceof HolonSwitch) {
  191. tCps = new HolonSwitch((HolonSwitch) cps);
  192. } else {
  193. tCps = new Node("Node");
  194. }
  195. tCps.setPosition(new Position(p.x + (cps.getPosition().x - x), p.y + (cps.getPosition().y - y)));
  196. tCps.setSav(cps.getSav());
  197. tempList.add(tCps);
  198. addObject(tCps, false);
  199. }
  200. // Edges
  201. boolean newEdge = true;
  202. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  203. for (Edge e : cps.getConnectedTo()) {
  204. // A and B of e in the copied Elements?
  205. if (model.getClipboradObjects().indexOf(e.getA()) != -1
  206. && model.getClipboradObjects().indexOf(e.getB()) != -1) {
  207. AbstractCanvasObject a = tempList.get(model.getClipboradObjects().indexOf(e.getA()));
  208. AbstractCanvasObject b = tempList.get(model.getClipboradObjects().indexOf(e.getB()));
  209. // was this Edge created or not?
  210. for (Edge et : tempList.get(model.getClipboradObjects().indexOf(cps)).getConnectedTo()) {
  211. //TODO Changed et to etA is this right?
  212. for (Edge etA : et.getA().getConnectedTo()) {
  213. if (etA.getA() == a && etA.getB() == b) {
  214. newEdge = false;
  215. }
  216. }
  217. for (Edge etB : et.getB().getConnectedTo()) {
  218. if (etB.getA() == a && etB.getB() == b) {
  219. newEdge = false;
  220. }
  221. }
  222. }
  223. if (newEdge) {
  224. Edge tempE = new Edge(tempList.get(model.getClipboradObjects().indexOf(e.getA())), // A
  225. tempList.get(model.getClipboradObjects().indexOf(e.getB())), /* B */
  226. e.getCapacity());
  227. addEdgeOnCanvas(tempE);
  228. }
  229. newEdge = true;
  230. }
  231. }
  232. }
  233. }
  234. /**
  235. * Cut all Selected Objects.
  236. */
  237. @SuppressWarnings("unchecked")
  238. public void cutObjects() {
  239. model.setClipboradObjects((ArrayList<AbstractCanvasObject>) model.getSelectedCpsObjects().clone());
  240. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  241. deleteObjectOnCanvas(cps);
  242. }
  243. model.getSelectedCpsObjects().clear();
  244. }
  245. /**
  246. * Some cleaning Algorithm which traverses the UpperNode through BFS Can be
  247. * extended with other cleaning stuff No need for coloring since there tree
  248. * is only directed in one direction
  249. *
  250. * @param node
  251. */
  252. public void bfsNodeCleaner(GroupNode node) {
  253. List<AbstractCanvasObject> objectsInGroupNode = node.getNodesAndGroupnodeNodes();
  254. ListIterator<Edge> iter = model.getEdgesOnCanvas().listIterator();
  255. while(iter.hasNext() ) {
  256. Edge edge = iter.next();
  257. if(objectsInGroupNode.contains(edge.getA()) || objectsInGroupNode.contains(edge.getB())) {
  258. iter.remove();
  259. }
  260. }
  261. }
  262. public void removeAllConnectionsFromObject(AbstractCanvasObject obj) {
  263. ListIterator<Edge> iter = model.getEdgesOnCanvas().listIterator();
  264. while(iter.hasNext() ) {
  265. Edge edge = iter.next();
  266. if(edge.getA() == obj || edge.getB() == obj) {
  267. iter.remove();
  268. }
  269. }
  270. }
  271. /**
  272. * Set the Background Image;
  273. *
  274. * @param imagePath
  275. * Image Path
  276. * @param mode
  277. * Image Mode
  278. * @param width
  279. * Image custom width
  280. * @param height
  281. * Image custom height
  282. */
  283. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  284. model.setCanvasImagePath(imagePath);
  285. model.setCanvasImageMode(mode);
  286. model.setCanvasImageWidth(width);
  287. model.setCanvasImageHeight(height);
  288. }
  289. }