CanvasController.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. //model.getStateHolon().addHolonObject(((HolonObject) object));
  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. notifyObjListeners();
  126. }
  127. /**
  128. * Replaces {@code toBeReplaced} by {@code by} on the canvas
  129. * @param toBeReplaced the object that will be replaced
  130. * @param by the object that will replace it
  131. */
  132. public void replaceObjectOnCanvas(AbstractCanvasObject toBeReplaced, AbstractCanvasObject by) {
  133. //Replace edges
  134. ListIterator<Edge> iter = model.getEdgesOnCanvas().listIterator();
  135. while(iter.hasNext() ) {
  136. Edge edge = iter.next();
  137. if(edge.getA() == toBeReplaced && edge.getB() != by) {
  138. edge.setA(by);
  139. }
  140. else if( edge.getB() == toBeReplaced && edge.getA() != by) {
  141. edge.setB(by);
  142. }
  143. }
  144. /** delete 'toBeReplaced' new empty connections, to prevent Nullpointer*/
  145. toBeReplaced.setConnections(new ArrayList<Edge>(1));
  146. /**
  147. * set Position of by to exactly toBeReplaced
  148. */
  149. by.setPosition(toBeReplaced.getPosition());
  150. deleteObjectOnCanvas(toBeReplaced);
  151. }
  152. /**
  153. * Add an edge to the Canvas.
  154. *
  155. * @param edge
  156. * the edge
  157. */
  158. public void addEdgeOnCanvas(Edge edge) {
  159. model.getEdgesOnCanvas().add(edge);
  160. }
  161. /**
  162. * Removes an Edge from the Canvas.
  163. *
  164. * @param edge
  165. * the edge to remove
  166. */
  167. public void removeEdgesOnCanvas(Edge edge) {
  168. edge.getA().getConnections().remove(edge);
  169. edge.getB().getConnections().remove(edge);
  170. model.getEdgesOnCanvas().remove(edge);
  171. }
  172. /**
  173. * Paste all Selected Objects.
  174. *
  175. * @param p
  176. * the mouse Position
  177. */
  178. public void pasteObjects(Point p) {
  179. model.getSelectedCpsObjects().clear();
  180. AbstractCanvasObject tCps = null;
  181. int x = Integer.MAX_VALUE, y = Integer.MAX_VALUE;
  182. // Location whre to copy the Elements
  183. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  184. if (cps.getPosition().x < x) {
  185. x = cps.getPosition().x;
  186. }
  187. if (cps.getPosition().y < y) {
  188. y = cps.getPosition().y;
  189. }
  190. }
  191. ArrayList<AbstractCanvasObject> tempList = new ArrayList<>();
  192. // Objects
  193. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  194. if (cps instanceof HolonObject) {
  195. tCps = new HolonObject((HolonObject) cps);
  196. } else if (cps instanceof HolonSwitch) {
  197. tCps = new HolonSwitch((HolonSwitch) cps);
  198. } else {
  199. tCps = new Node("Node");
  200. }
  201. tCps.setPosition(new Position(p.x + (cps.getPosition().x - x), p.y + (cps.getPosition().y - y)));
  202. tCps.setSav(cps.getSav());
  203. tempList.add(tCps);
  204. addObject(tCps, false);
  205. }
  206. // Edges
  207. boolean newEdge = true;
  208. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  209. for (Edge e : cps.getConnectedTo()) {
  210. // A and B of e in the copied Elements?
  211. if (model.getClipboradObjects().indexOf(e.getA()) != -1
  212. && model.getClipboradObjects().indexOf(e.getB()) != -1) {
  213. AbstractCanvasObject a = tempList.get(model.getClipboradObjects().indexOf(e.getA()));
  214. AbstractCanvasObject b = tempList.get(model.getClipboradObjects().indexOf(e.getB()));
  215. // was this Edge created or not?
  216. for (Edge et : tempList.get(model.getClipboradObjects().indexOf(cps)).getConnectedTo()) {
  217. //TODO Changed et to etA is this right?
  218. for (Edge etA : et.getA().getConnectedTo()) {
  219. if (etA.getA() == a && etA.getB() == b) {
  220. newEdge = false;
  221. }
  222. }
  223. for (Edge etB : et.getB().getConnectedTo()) {
  224. if (etB.getA() == a && etB.getB() == b) {
  225. newEdge = false;
  226. }
  227. }
  228. }
  229. if (newEdge) {
  230. Edge tempE = new Edge(tempList.get(model.getClipboradObjects().indexOf(e.getA())), // A
  231. tempList.get(model.getClipboradObjects().indexOf(e.getB())), /* B */
  232. e.getCapacity());
  233. addEdgeOnCanvas(tempE);
  234. }
  235. newEdge = true;
  236. }
  237. }
  238. }
  239. }
  240. /**
  241. * Cut all Selected Objects.
  242. */
  243. @SuppressWarnings("unchecked")
  244. public void cutObjects() {
  245. model.setClipboradObjects((ArrayList<AbstractCanvasObject>) model.getSelectedCpsObjects().clone());
  246. for (AbstractCanvasObject cps : model.getClipboradObjects()) {
  247. deleteObjectOnCanvas(cps);
  248. }
  249. model.getSelectedCpsObjects().clear();
  250. }
  251. /**
  252. * Some cleaning Algorithm which traverses the UpperNode through BFS Can be
  253. * extended with other cleaning stuff No need for coloring since there tree
  254. * is only directed in one direction
  255. *
  256. * @param node
  257. */
  258. public void bfsNodeCleaner(GroupNode node) {
  259. List<AbstractCanvasObject> objectsInGroupNode = node.getNodesAndGroupnodeNodes();
  260. ListIterator<Edge> iter = model.getEdgesOnCanvas().listIterator();
  261. while(iter.hasNext() ) {
  262. Edge edge = iter.next();
  263. if(objectsInGroupNode.contains(edge.getA()) || objectsInGroupNode.contains(edge.getB())) {
  264. iter.remove();
  265. }
  266. }
  267. }
  268. public void removeAllConnectionsFromObject(AbstractCanvasObject obj) {
  269. ListIterator<Edge> iter = model.getEdgesOnCanvas().listIterator();
  270. while(iter.hasNext() ) {
  271. Edge edge = iter.next();
  272. if(edge.getA() == obj || edge.getB() == obj) {
  273. iter.remove();
  274. }
  275. }
  276. }
  277. /**
  278. * Set the Background Image;
  279. *
  280. * @param imagePath
  281. * Image Path
  282. * @param mode
  283. * Image Mode
  284. * @param width
  285. * Image custom width
  286. * @param height
  287. * Image custom height
  288. */
  289. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  290. model.setCanvasImagePath(imagePath);
  291. model.setCanvasImageMode(mode);
  292. model.setCanvasImageWidth(width);
  293. model.setCanvasImageHeight(height);
  294. }
  295. }