GroupNodeCanvas.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. package ui.view.canvas;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.RenderingHints;
  8. import java.awt.event.MouseEvent;
  9. import java.io.IOException;
  10. import java.util.HashSet;
  11. import classes.AbstractCanvasObject;
  12. import classes.Edge;
  13. import classes.GroupNode;
  14. import classes.HolonObject;
  15. import classes.HolonSwitch;
  16. import classes.Node;
  17. import ui.controller.Control;
  18. import ui.model.Consumer;
  19. import ui.model.DecoratedCable;
  20. import ui.model.DecoratedGroupNode;
  21. import ui.model.DecoratedSwitch;
  22. import ui.model.ExitCable;
  23. import ui.model.Model;
  24. import ui.model.Passiv;
  25. import ui.model.Supplier;
  26. import ui.view.inspector.UnitGraph;
  27. import utility.ImageImport;
  28. import utility.Vector2Int;
  29. //TODO delete GroupNodeCanvas completely and only have canvas Class
  30. public class GroupNodeCanvas extends Canvas {
  31. private String parentPath;
  32. private Component parentComponent;
  33. public GroupNodeCanvas(Model mod, Control control, UnitGraph unitGraph, GroupNode groupNode, String parentPath,
  34. Component parentComponent) {
  35. super(mod, control, unitGraph);
  36. this.groupNode = groupNode;
  37. this.parentPath = parentPath;
  38. this.parentComponent = parentComponent;
  39. }
  40. public void setGroupNode(GroupNode upperNode) {
  41. this.groupNode = upperNode;
  42. }
  43. public GroupNode getGroupNode() {
  44. return groupNode;
  45. }
  46. public String getParentPath() {
  47. return parentPath;
  48. }
  49. public Component getParentComponent() {
  50. return parentComponent;
  51. }
  52. public void paintComponent(Graphics g) {
  53. // super.paintComponent(g);
  54. Graphics2D g2d = (Graphics2D) g;
  55. g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
  56. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  57. // -->Old code
  58. if (drawEdge) {
  59. g2d.setColor(Color.BLACK);
  60. g2d.setStroke(new BasicStroke(1));
  61. g2d.drawLine(tempCps.getPosition().getX(), tempCps.getPosition().getY(), x, y);
  62. }
  63. // <--
  64. // SelectedCable
  65. HashSet<Edge> selectedEdges = new HashSet<Edge>();
  66. for (AbstractCanvasObject aCps : model.getSelectedObjects()) {
  67. for (Edge edge : aCps.getConnections()) {
  68. selectedEdges.add(edge);
  69. }
  70. }
  71. if (model.getSelectedEdge() != null)
  72. selectedEdges.add(model.getSelectedEdge());
  73. DecoratedGroupNode actualGroupNode = controller.getSimManager().getActualVisualRepresentationalState()
  74. .getCreatedGroupNodes().get(groupNode);
  75. // VisualState Representation:
  76. for (ExitCable cable : actualGroupNode.getExitCableList()) {
  77. paintExitCable(g2d, cable);
  78. }
  79. for (DecoratedCable cable : actualGroupNode.getInternCableList()) {
  80. paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
  81. }
  82. for (DecoratedGroupNode dGroupNode : actualGroupNode.getGroupNodeList()) {
  83. paintGroupNode(g2d, dGroupNode);
  84. }
  85. for (Consumer con : actualGroupNode.getConsumerList()) {
  86. paintConsumer(g2d, con);
  87. }
  88. for (Supplier sup : actualGroupNode.getSupplierList()) {
  89. paintSupplier(g2d, sup);
  90. }
  91. for (Passiv pas : actualGroupNode.getPassivList()) {
  92. paintCanvasObject(g2d, pas);
  93. }
  94. for (DecoratedSwitch dSwitch : actualGroupNode.getSwitchList()) {
  95. paintSwitch(g2d, dSwitch);
  96. }
  97. for (Node node : actualGroupNode.getNodeList()) {
  98. drawCanvasObject(g2d, "/Images/node.png", node.getPosition());
  99. }
  100. // -->oldCode
  101. if (doMark) {
  102. g2d.setColor(Color.BLACK);
  103. g2d.setStroke(new BasicStroke(0));
  104. drawMarker(g2d);
  105. }
  106. // Test Selection
  107. // Objects:
  108. g2d.setColor(Color.BLUE);
  109. g2d.setStroke(new BasicStroke(1));
  110. Color transparentGrey = new Color(128, 174, 247, 40);
  111. for (AbstractCanvasObject aCps : model.getSelectedObjects()) {
  112. if (aCps instanceof Node) {
  113. Vector2Int pos = aCps.getPosition();
  114. g2d.setColor(transparentGrey);
  115. g2d.fillOval(pos.getX() - (int) (controller.getScaleDiv2()),
  116. pos.getY() - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  117. g2d.setColor(Color.LIGHT_GRAY);
  118. g2d.setStroke(new BasicStroke(2));
  119. g2d.drawOval(pos.getX() - (int) (controller.getScaleDiv2()),
  120. pos.getY() - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  121. } else {
  122. Vector2Int pos = aCps.getPosition();
  123. g2d.setColor(transparentGrey);
  124. g2d.fillRect(pos.getX() - (int) (controller.getScaleDiv2() * 1.5f),
  125. pos.getY() - (int) (controller.getScaleDiv2() * 1.5f), (int) (controller.getScale() * 1.5f),
  126. (int) (controller.getScale() * 1.5f));
  127. g2d.setColor(Color.LIGHT_GRAY);
  128. g2d.setStroke(new BasicStroke(2));
  129. g2d.drawRect(pos.getX() - (int) (controller.getScaleDiv2() * 1.5f),
  130. pos.getY() - (int) (controller.getScaleDiv2() * 1.5f), (int) (controller.getScale() * 1.5f),
  131. (int) (controller.getScale() * 1.5f));
  132. }
  133. }
  134. // maybeReplace:
  135. if (mayBeReplaced != null) {
  136. g2d.setColor(Color.RED);
  137. Vector2Int pos = mayBeReplaced.getPosition();
  138. g.drawImage(ImageImport.loadImage("/Images/replace.png"), pos.getX() + controller.getScaleDiv2(),
  139. pos.getY() - controller.getScale(), controller.getScaleDiv2(), controller.getScaleDiv2(), null);
  140. }
  141. // <-- OldCode
  142. }
  143. @Override
  144. public void mousePressed(MouseEvent e) {
  145. this.grabFocus();
  146. if (!disabled) {
  147. tempCps = null;
  148. dataSelected = null;
  149. edgeHighlight = null;
  150. controller.setSelecteEdge(null);
  151. controller.setSelectedObjectID(-1);
  152. // Object Selection
  153. if (e.getX() > 0) {
  154. for (AbstractCanvasObject cps : groupNode.getNodes()) {
  155. cx = cps.getPosition().getX() - model.getScaleDiv2();
  156. cy = cps.getPosition().getY() - model.getScaleDiv2();
  157. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  158. tempCps = cps;
  159. dragging = true;
  160. if (e.isControlDown() && tempCps != null) {
  161. System.out.println("Add Remove Selection GROUPNODECANVAS");
  162. if (model.getSelectedObjects().contains(tempCps)) {
  163. controller.removeObjectFromSelection(tempCps);
  164. } else {
  165. controller.addSelectedObject(tempCps);
  166. if (tempCps instanceof GroupNode)
  167. controller.getObjectsInDepth();
  168. }
  169. }
  170. // If drawing an Edge (CTRL down)
  171. if (tempCps.getClass() == HolonObject.class) {
  172. HolonObject tempObj = ((HolonObject) tempCps);
  173. dataSelected = tempObj.getElements();
  174. }
  175. if (e.isShiftDown()) {
  176. drawEdge = true;
  177. dragging = false;
  178. }
  179. break;
  180. }
  181. }
  182. } else {
  183. // look for objects connected to uppernode
  184. int count = 0;
  185. for (Edge ed : groupNode.getConnections()) {
  186. AbstractCanvasObject cps;
  187. if (ed.getA().equals(this.groupNode)) {
  188. cps = ed.getB();
  189. } else {
  190. cps = ed.getA();
  191. }
  192. if (x - controller.getScale() <= (model.getScaleDiv2())
  193. && y - controller.getScale() <= (scalediv20 + 5
  194. + (model.getScale() + scalediv20 + 10) * count)
  195. && x >= model.getScaleDiv2()
  196. && y >= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)) {
  197. tempCps = cps;
  198. // If drawing an Edge (CTRL down)
  199. if (tempCps.getClass() == HolonObject.class) {
  200. HolonObject tempObj = ((HolonObject) tempCps);
  201. dataSelected = tempObj.getElements();
  202. }
  203. if (e.isShiftDown()) {
  204. drawEdge = true;
  205. }
  206. }
  207. count++;
  208. }
  209. }
  210. // Edge Selection
  211. if (e.getButton() == MouseEvent.BUTTON1) {
  212. if (tempCps == null) {
  213. edgeHighlight = mousePositionOnEdge(x, y);
  214. controller.setSelecteEdge(edgeHighlight);
  215. controller.setSelectedObjectID(0);
  216. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  217. controller.clearSelection();
  218. }
  219. }
  220. if (edgeHighlight == null && tempCps == null) {
  221. sx = e.getX();
  222. sy = e.getY();
  223. doMark = true;
  224. }
  225. repaint();
  226. }
  227. }
  228. }
  229. @Override
  230. public void mouseReleased(MouseEvent e) {
  231. if (!disabled) {
  232. x = e.getX();
  233. y = e.getY();
  234. dragging = false;
  235. if (drawEdge) {
  236. drawEdge = false;
  237. drawDeleteEdge();
  238. }
  239. if (dragged) {
  240. try {
  241. /**
  242. * Save State before performing NodePlacement, replacement e.g.
  243. */
  244. controller.autoSave();
  245. } catch (IOException ex) {
  246. ex.printStackTrace();
  247. }
  248. /**
  249. * check if tempCps could replace an Object on the UpperNodeanvas
  250. */
  251. if (model.getSelectedObjects().size() == 1 && checkForReplacement(groupNode.getNodes(), tempCps,
  252. tempCps.getPosition().getX(), tempCps.getPosition().getY())) {
  253. /**
  254. * if UpperNode would be replaced, close its tabs
  255. */
  256. if (mayBeReplaced instanceof GroupNode)
  257. closeUpperNodeTab(mayBeReplaced.getId());
  258. /**
  259. * replace on canvas
  260. */
  261. controller.replaceObjUpperNode(mayBeReplaced, tempCps, groupNode);
  262. mayBeReplaced = null;
  263. }
  264. }
  265. if (!e.isControlDown() && !dragged && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  266. controller.clearSelection();
  267. controller.addSelectedObject(tempCps);
  268. if (tempCps instanceof GroupNode)
  269. controller.getObjectsInDepth();
  270. }
  271. dragged = false;
  272. // Rightclick List
  273. setRightClickMenu(e);
  274. markObjects();
  275. boolean doubleclick = doubleClick();
  276. if (doubleclick && tempCps != null && tempCps instanceof HolonSwitch) {
  277. ((HolonSwitch) tempCps).switchState();
  278. }
  279. if (doubleclick && tempCps != null && tempCps instanceof GroupNode) {
  280. controller.getGui().openNewUpperNodeTab((GroupNode)tempCps);
  281. }
  282. controller.calculateStateAndVisualForTimeStep(model.getCurrentIteration());
  283. repaint();
  284. }
  285. }
  286. @Override
  287. public void mouseDragged(MouseEvent e) {
  288. if (!disabled) {
  289. // If Edge is drawn
  290. x = e.getX();
  291. y = e.getY();
  292. if (!model.getSelectedObjects().contains(tempCps) && !doMark) {
  293. controller.clearSelection();
  294. if (tempCps != null) {
  295. controller.addSelectedObject(tempCps);
  296. }
  297. }
  298. if (dragging) {
  299. try {
  300. // tempCps in the upperNode? else its a connected Object from
  301. // outside
  302. if (groupNode.getNodes().contains(tempCps)) {
  303. dragged = true;
  304. float xDist, yDist; // Distance
  305. x = e.getX();
  306. y = e.getY();
  307. // Make sure its in bounds
  308. if (e.getX() < controller.getScaleDiv2() + + 5)
  309. x = controller.getScaleDiv2() + + 5;
  310. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  311. x = this.getWidth() - controller.getScaleDiv2();
  312. if (e.getY() < controller.getScaleDiv2())
  313. y = controller.getScaleDiv2();
  314. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  315. y = this.getHeight() - controller.getScaleDiv2();
  316. // Distance
  317. xDist = x - tempCps.getPosition().getX();
  318. yDist = y - tempCps.getPosition().getY();
  319. tempCps.setPosition(x, y); // Drag Position
  320. // TipText Position and name
  321. toolTip = true;
  322. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  323. toolTipPos.setX(tempCps.getPosition().getX() - model.getScaleDiv2());
  324. toolTipPos.setY(tempCps.getPosition().getY() - model.getScaleDiv2());
  325. // All Selected Objects
  326. for (AbstractCanvasObject cps : model.getSelectedObjects()) {
  327. if (cps != tempCps) {
  328. x = (int) (cps.getPosition().getX() + xDist);
  329. y = (int) (cps.getPosition().getY() + yDist);
  330. // Make sure its in bounds
  331. if (x < + 5 + controller.getScaleDiv2())
  332. x = controller.getScaleDiv2() + + 5;
  333. else if (x > this.getWidth() - controller.getScaleDiv2())
  334. x = this.getWidth() - controller.getScaleDiv2();
  335. if (y <= controller.getScaleDiv2())
  336. y = controller.getScaleDiv2();
  337. else if (y > this.getHeight() - controller.getScaleDiv2())
  338. y = this.getHeight() - controller.getScaleDiv2();
  339. cps.setPosition(x, y);
  340. }
  341. }
  342. }
  343. /**
  344. * check if something would be replaced
  345. */
  346. if (model.getSelectedObjects().size() == 1)
  347. checkForReplacement(groupNode.getNodes(), tempCps, x, y);
  348. repaint();
  349. } catch (Exception eex) {
  350. }
  351. }
  352. // Mark Objects
  353. if (doMark) {
  354. tempSelected.clear();
  355. for (AbstractCanvasObject cps : groupNode.getNodes()) {
  356. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  357. if (sx >= x) {
  358. x1 = x;
  359. x2 = sx;
  360. }
  361. if (sy >= y) {
  362. y1 = y;
  363. y2 = sy;
  364. }
  365. if (x1 <= cps.getPosition().getX() + model.getScaleDiv2()
  366. && y1 <= cps.getPosition().getY() + model.getScaleDiv2() && x2 >= cps.getPosition().getX()
  367. && y2 >= cps.getPosition().getY()) {
  368. tempSelected.add(cps);
  369. }
  370. }
  371. int count = 0;
  372. for (Edge ed : groupNode.getConnections()) {
  373. AbstractCanvasObject cps;
  374. if (ed.getA().equals(groupNode)) {
  375. cps = ed.getB();
  376. } else {
  377. cps = ed.getA();
  378. }
  379. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  380. if (sx >= x) {
  381. x1 = x;
  382. x2 = sx;
  383. }
  384. if (sy >= y) {
  385. y1 = y;
  386. y2 = sy;
  387. }
  388. if (x1 <= 0
  389. && y1 <= (int) (5 + (model.getScale() + scalediv20 + 10) * count) + model.getScaleDiv2()
  390. && x2 >= 0
  391. && y2 >= (int) (5 + (model.getScale() + scalediv20 + 10) * count)) {
  392. tempSelected.add(cps);
  393. }
  394. count++;
  395. }
  396. }
  397. repaint();
  398. }
  399. }
  400. @Override
  401. public void mouseMoved(MouseEvent e) {
  402. {
  403. x = e.getX();
  404. y = e.getY();
  405. // Everything for the tooltip :)
  406. boolean on = false;
  407. for (AbstractCanvasObject cps : groupNode.getNodes()) {
  408. cx = cps.getPosition().getX() - controller.getScaleDiv2();
  409. cy = cps.getPosition().getY() - controller.getScaleDiv2();
  410. on = setToolTipInfoAndPosition(on, cps);
  411. }
  412. int count = 0;
  413. for (Edge ed : groupNode.getConnections()) {
  414. AbstractCanvasObject cps;
  415. if (ed.getA().equals(this.groupNode)) {
  416. cps = ed.getB();
  417. } else {
  418. cps = ed.getA();
  419. }
  420. cx = 0 ;
  421. cy = (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count);
  422. if (x - 50 <= cx && y - 50 <= cy && x >= cx && y >= cy) {
  423. on = true;
  424. toolTipPos.setX(cx - 25);
  425. toolTipPos.setY(cy + 50);
  426. toolTipText = cps.getName() + ", " + cps.getId();
  427. }
  428. count++;
  429. }
  430. toolTip = on;
  431. repaint();
  432. }
  433. }
  434. }