GroupNodeCanvas.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. // Object Selection
  152. if (e.getX() > 0) {
  153. for (AbstractCanvasObject cps : groupNode.getNodes()) {
  154. cx = cps.getPosition().getX() - model.getScaleDiv2();
  155. cy = cps.getPosition().getY() - model.getScaleDiv2();
  156. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  157. tempCps = cps;
  158. dragging = true;
  159. if (e.isControlDown() && tempCps != null) {
  160. System.out.println("Add Remove Selection GROUPNODECANVAS");
  161. if (model.getSelectedObjects().contains(tempCps)) {
  162. controller.removeObjectFromSelection(tempCps);
  163. } else {
  164. controller.addSelectedObject(tempCps);
  165. if (tempCps instanceof GroupNode)
  166. controller.getObjectsInDepth();
  167. }
  168. }
  169. // If drawing an Edge (CTRL down)
  170. if (tempCps.getClass() == HolonObject.class) {
  171. HolonObject tempObj = ((HolonObject) tempCps);
  172. dataSelected = tempObj.getElements();
  173. }
  174. if (e.isShiftDown()) {
  175. drawEdge = true;
  176. dragging = false;
  177. }
  178. break;
  179. }
  180. }
  181. } else {
  182. // look for objects connected to uppernode
  183. int count = 0;
  184. for (Edge ed : groupNode.getConnections()) {
  185. AbstractCanvasObject cps;
  186. if (ed.getA().equals(this.groupNode)) {
  187. cps = ed.getB();
  188. } else {
  189. cps = ed.getA();
  190. }
  191. if (x - controller.getScale() <= (model.getScaleDiv2())
  192. && y - controller.getScale() <= (scalediv20 + 5
  193. + (model.getScale() + scalediv20 + 10) * count)
  194. && x >= model.getScaleDiv2()
  195. && y >= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)) {
  196. tempCps = cps;
  197. // If drawing an Edge (CTRL down)
  198. if (tempCps.getClass() == HolonObject.class) {
  199. HolonObject tempObj = ((HolonObject) tempCps);
  200. dataSelected = tempObj.getElements();
  201. }
  202. if (e.isShiftDown()) {
  203. drawEdge = true;
  204. }
  205. }
  206. count++;
  207. }
  208. }
  209. // Edge Selection
  210. if (e.getButton() == MouseEvent.BUTTON1) {
  211. if (tempCps == null) {
  212. edgeHighlight = mousePositionOnEdge(x, y);
  213. controller.setSelecteEdge(edgeHighlight);
  214. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  215. controller.clearSelection();
  216. }
  217. }
  218. if (edgeHighlight == null && tempCps == null) {
  219. sx = e.getX();
  220. sy = e.getY();
  221. doMark = true;
  222. }
  223. repaint();
  224. }
  225. }
  226. }
  227. @Override
  228. public void mouseReleased(MouseEvent e) {
  229. if (!disabled) {
  230. x = e.getX();
  231. y = e.getY();
  232. dragging = false;
  233. if (drawEdge) {
  234. drawEdge = false;
  235. drawDeleteEdge();
  236. }
  237. if (dragged) {
  238. try {
  239. /**
  240. * Save State before performing NodePlacement, replacement e.g.
  241. */
  242. controller.autoSave();
  243. } catch (IOException ex) {
  244. ex.printStackTrace();
  245. }
  246. /**
  247. * check if tempCps could replace an Object on the UpperNodeanvas
  248. */
  249. if (model.getSelectedObjects().size() == 1 && checkForReplacement(groupNode.getNodes(), tempCps,
  250. tempCps.getPosition().getX(), tempCps.getPosition().getY())) {
  251. /**
  252. * if UpperNode would be replaced, close its tabs
  253. */
  254. if (mayBeReplaced instanceof GroupNode)
  255. closeUpperNodeTab(mayBeReplaced.getId());
  256. /**
  257. * replace on canvas
  258. */
  259. controller.replaceObjUpperNode(mayBeReplaced, tempCps, groupNode);
  260. mayBeReplaced = null;
  261. }
  262. }
  263. if (!e.isControlDown() && !dragged && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  264. controller.clearSelection();
  265. controller.addSelectedObject(tempCps);
  266. if (tempCps instanceof GroupNode)
  267. controller.getObjectsInDepth();
  268. }
  269. dragged = false;
  270. // Rightclick List
  271. setRightClickMenu(e);
  272. markObjects();
  273. boolean doubleclick = doubleClick();
  274. if (doubleclick && tempCps != null && tempCps instanceof HolonSwitch) {
  275. ((HolonSwitch) tempCps).switchState();
  276. }
  277. if (doubleclick && tempCps != null && tempCps instanceof GroupNode) {
  278. controller.getGui().openNewUpperNodeTab((GroupNode)tempCps);
  279. }
  280. controller.calculateStateAndVisualForTimeStep(model.getCurrentIteration());
  281. repaint();
  282. }
  283. }
  284. @Override
  285. public void mouseDragged(MouseEvent e) {
  286. if (!disabled) {
  287. // If Edge is drawn
  288. x = e.getX();
  289. y = e.getY();
  290. if (!model.getSelectedObjects().contains(tempCps) && !doMark) {
  291. controller.clearSelection();
  292. if (tempCps != null) {
  293. controller.addSelectedObject(tempCps);
  294. }
  295. }
  296. if (dragging) {
  297. try {
  298. // tempCps in the upperNode? else its a connected Object from
  299. // outside
  300. if (groupNode.getNodes().contains(tempCps)) {
  301. dragged = true;
  302. float xDist, yDist; // Distance
  303. x = e.getX();
  304. y = e.getY();
  305. // Make sure its in bounds
  306. if (e.getX() < controller.getScaleDiv2() + + 5)
  307. x = controller.getScaleDiv2() + + 5;
  308. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  309. x = this.getWidth() - controller.getScaleDiv2();
  310. if (e.getY() < controller.getScaleDiv2())
  311. y = controller.getScaleDiv2();
  312. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  313. y = this.getHeight() - controller.getScaleDiv2();
  314. // Distance
  315. xDist = x - tempCps.getPosition().getX();
  316. yDist = y - tempCps.getPosition().getY();
  317. tempCps.setPosition(x, y); // Drag Position
  318. // TipText Position and name
  319. toolTip = true;
  320. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  321. toolTipPos.setX(tempCps.getPosition().getX() - model.getScaleDiv2());
  322. toolTipPos.setY(tempCps.getPosition().getY() - model.getScaleDiv2());
  323. // All Selected Objects
  324. for (AbstractCanvasObject cps : model.getSelectedObjects()) {
  325. if (cps != tempCps) {
  326. x = (int) (cps.getPosition().getX() + xDist);
  327. y = (int) (cps.getPosition().getY() + yDist);
  328. // Make sure its in bounds
  329. if (x < + 5 + controller.getScaleDiv2())
  330. x = controller.getScaleDiv2() + + 5;
  331. else if (x > this.getWidth() - controller.getScaleDiv2())
  332. x = this.getWidth() - controller.getScaleDiv2();
  333. if (y <= controller.getScaleDiv2())
  334. y = controller.getScaleDiv2();
  335. else if (y > this.getHeight() - controller.getScaleDiv2())
  336. y = this.getHeight() - controller.getScaleDiv2();
  337. cps.setPosition(x, y);
  338. }
  339. }
  340. }
  341. /**
  342. * check if something would be replaced
  343. */
  344. if (model.getSelectedObjects().size() == 1)
  345. checkForReplacement(groupNode.getNodes(), tempCps, x, y);
  346. repaint();
  347. } catch (Exception eex) {
  348. }
  349. }
  350. // Mark Objects
  351. if (doMark) {
  352. tempSelected.clear();
  353. for (AbstractCanvasObject cps : groupNode.getNodes()) {
  354. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  355. if (sx >= x) {
  356. x1 = x;
  357. x2 = sx;
  358. }
  359. if (sy >= y) {
  360. y1 = y;
  361. y2 = sy;
  362. }
  363. if (x1 <= cps.getPosition().getX() + model.getScaleDiv2()
  364. && y1 <= cps.getPosition().getY() + model.getScaleDiv2() && x2 >= cps.getPosition().getX()
  365. && y2 >= cps.getPosition().getY()) {
  366. tempSelected.add(cps);
  367. }
  368. }
  369. int count = 0;
  370. for (Edge ed : groupNode.getConnections()) {
  371. AbstractCanvasObject cps;
  372. if (ed.getA().equals(groupNode)) {
  373. cps = ed.getB();
  374. } else {
  375. cps = ed.getA();
  376. }
  377. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  378. if (sx >= x) {
  379. x1 = x;
  380. x2 = sx;
  381. }
  382. if (sy >= y) {
  383. y1 = y;
  384. y2 = sy;
  385. }
  386. if (x1 <= 0
  387. && y1 <= (int) (5 + (model.getScale() + scalediv20 + 10) * count) + model.getScaleDiv2()
  388. && x2 >= 0
  389. && y2 >= (int) (5 + (model.getScale() + scalediv20 + 10) * count)) {
  390. tempSelected.add(cps);
  391. }
  392. count++;
  393. }
  394. }
  395. repaint();
  396. }
  397. }
  398. @Override
  399. public void mouseMoved(MouseEvent e) {
  400. {
  401. x = e.getX();
  402. y = e.getY();
  403. // Everything for the tooltip :)
  404. boolean on = false;
  405. for (AbstractCanvasObject cps : groupNode.getNodes()) {
  406. cx = cps.getPosition().getX() - controller.getScaleDiv2();
  407. cy = cps.getPosition().getY() - controller.getScaleDiv2();
  408. on = setToolTipInfoAndPosition(on, cps);
  409. }
  410. int count = 0;
  411. for (Edge ed : groupNode.getConnections()) {
  412. AbstractCanvasObject cps;
  413. if (ed.getA().equals(this.groupNode)) {
  414. cps = ed.getB();
  415. } else {
  416. cps = ed.getA();
  417. }
  418. cx = 0 ;
  419. cy = (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count);
  420. if (x - 50 <= cx && y - 50 <= cy && x >= cx && y >= cy) {
  421. on = true;
  422. toolTipPos.setX(cx - 25);
  423. toolTipPos.setY(cy + 50);
  424. toolTipText = cps.getName() + ", " + cps.getId();
  425. }
  426. count++;
  427. }
  428. toolTip = on;
  429. repaint();
  430. }
  431. }
  432. }