CanvasController.java 9.6 KB

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