CanvasController.java 9.0 KB

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