CanvasController.java 9.4 KB

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