GroupNodeCanvas.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package holeg.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.Optional;
  11. import java.util.logging.Logger;
  12. import holeg.model.AbstractCanvasObject;
  13. import holeg.model.Edge;
  14. import holeg.model.GroupNode;
  15. import holeg.model.HolonObject;
  16. import holeg.model.HolonSwitch;
  17. import holeg.model.Node;
  18. import holeg.preferences.ColorPreference;
  19. import holeg.ui.controller.Control;
  20. import holeg.ui.model.Consumer;
  21. import holeg.ui.model.DecoratedGroupNode;
  22. import holeg.ui.model.DecoratedSwitch;
  23. import holeg.ui.model.ExitCable;
  24. import holeg.ui.model.GuiSettings;
  25. import holeg.ui.model.Model;
  26. import holeg.ui.model.Passiv;
  27. import holeg.ui.model.Supplier;
  28. import holeg.ui.model.VisualRepresentationalState;
  29. import holeg.ui.view.inspector.UnitGraph;
  30. import holeg.utility.ImageImport;
  31. import holeg.utility.Vector2Int;
  32. //TODO(Tom2021-12-1) delete GroupNodeCanvas completely and only have canvas Class
  33. public class GroupNodeCanvas extends Canvas {
  34. private static final Logger log = Logger.getLogger(GroupNodeCanvas.class.getName());
  35. private String parentPath;
  36. private Component parentComponent;
  37. public GroupNodeCanvas(Model mod, Control control, UnitGraph unitGraph, GroupNode groupNode, String parentPath,
  38. Component parentComponent) {
  39. super(mod, control, unitGraph);
  40. this.groupNode = groupNode;
  41. this.parentPath = parentPath;
  42. this.parentComponent = parentComponent;
  43. }
  44. public void setGroupNode(GroupNode upperNode) {
  45. this.groupNode = upperNode;
  46. }
  47. public GroupNode getGroupNode() {
  48. return groupNode;
  49. }
  50. public String getParentPath() {
  51. return parentPath;
  52. }
  53. public Component getParentComponent() {
  54. return parentComponent;
  55. }
  56. public void paintComponent(Graphics g) {
  57. // super.paintComponent(g);
  58. Graphics2D g2d = (Graphics2D) g;
  59. g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
  60. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  61. // -->Old code
  62. if (drawEdge) {
  63. g2d.setColor(Color.BLACK);
  64. g2d.setStroke(new BasicStroke(1));
  65. g2d.drawLine(tempCps.getPosition().getX(), tempCps.getPosition().getY(), x, y);
  66. }
  67. // <--
  68. // SelectedCable
  69. Optional<VisualRepresentationalState> visState = control.getSimManager()
  70. .getActualVisualRepresentationalState();
  71. if (visState.isEmpty()) {
  72. return;
  73. }
  74. DecoratedGroupNode actualGroupNode = visState.get().getCreatedGroupNodes().get(groupNode);
  75. // VisualState Representation:
  76. for (ExitCable cable : actualGroupNode.getExitCableList()) {
  77. paintExitCable(g2d, cable);
  78. }
  79. for (Edge cable : actualGroupNode.getInternCableList()) {
  80. paintCable(g2d, cable, GuiSettings.getSelectedEdges().contains(cable));
  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.setStroke(new BasicStroke(1));
  109. Color transparentGrey = ColorPreference.Panel.ObjectSelection;
  110. for (AbstractCanvasObject aCps : GuiSettings.getSelectedObjects()) {
  111. if (aCps instanceof Node) {
  112. Vector2Int pos = aCps.getPosition();
  113. g2d.setColor(transparentGrey);
  114. g2d.fillOval(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2()),
  115. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  116. g2d.setColor(ColorPreference.Panel.ObjectSelectionBorder);
  117. g2d.setStroke(new BasicStroke(2));
  118. g2d.drawOval(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2()),
  119. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2()), GuiSettings.getPictureScale(), GuiSettings.getPictureScale());
  120. } else {
  121. Vector2Int pos = aCps.getPosition();
  122. g2d.setColor(transparentGrey);
  123. g2d.fillRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  124. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  125. (int) (GuiSettings.getPictureScale() * 1.5f));
  126. g2d.setColor(ColorPreference.Panel.ObjectSelectionBorder);
  127. g2d.setStroke(new BasicStroke(2));
  128. g2d.drawRect(pos.getX() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f),
  129. pos.getY() - (int) (GuiSettings.getPictureScaleDiv2() * 1.5f), (int) (GuiSettings.getPictureScale() * 1.5f),
  130. (int) (GuiSettings.getPictureScale() * 1.5f));
  131. }
  132. }
  133. // maybeReplace:
  134. if (mayBeReplaced != null) {
  135. Vector2Int pos = mayBeReplaced.getPosition();
  136. g.drawImage(ImageImport.loadImage("/Images/replace.png"), pos.getX() + GuiSettings.getPictureScaleDiv2(),
  137. pos.getY() - GuiSettings.getPictureScale(), GuiSettings.getPictureScaleDiv2(), GuiSettings.getPictureScaleDiv2(), null);
  138. }
  139. // <-- OldCode
  140. }
  141. @Override
  142. public void mousePressed(MouseEvent e) {
  143. this.grabFocus();
  144. if (!disabled) {
  145. tempCps = null;
  146. dataSelected = null;
  147. edgeHighlight = null;
  148. GuiSettings.getSelectedEdges().clear();
  149. // Object Selection
  150. if (e.getX() > 0) {
  151. for (AbstractCanvasObject cps : groupNode.getObjectsInThisLayer().toList()) {
  152. cx = cps.getPosition().getX() - GuiSettings.getPictureScaleDiv2();
  153. cy = cps.getPosition().getY() - GuiSettings.getPictureScaleDiv2();
  154. if (x - GuiSettings.getPictureScale() <= cx && y - GuiSettings.getPictureScale() <= cy && x >= cx && y >= cy) {
  155. tempCps = cps;
  156. dragging = true;
  157. if (e.isControlDown() && tempCps != null) {
  158. log.info("Add Remove Selection GROUPNODECANVAS");
  159. if (GuiSettings.getSelectedObjects().contains(tempCps)) {
  160. control.removeObjectFromSelection(tempCps);
  161. } else {
  162. control.addSelectedObject(tempCps);
  163. if (tempCps instanceof GroupNode)
  164. control.getObjectsInDepth();
  165. }
  166. }
  167. // If drawing an Edge (CTRL down)
  168. if (tempCps.getClass() == HolonObject.class) {
  169. HolonObject tempObj = ((HolonObject) tempCps);
  170. dataSelected = tempObj.getElements();
  171. }
  172. if (e.isShiftDown()) {
  173. drawEdge = true;
  174. dragging = false;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. // Edge Selection
  181. if (e.getButton() == MouseEvent.BUTTON1) {
  182. if (tempCps == null) {
  183. edgeHighlight = mousePositionOnEdge(x, y);
  184. GuiSettings.getSelectedEdges().add(edgeHighlight);
  185. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  186. control.clearSelection();
  187. }
  188. }
  189. if (edgeHighlight == null && tempCps == null) {
  190. sx = e.getX();
  191. sy = e.getY();
  192. doMark = true;
  193. }
  194. repaint();
  195. }
  196. }
  197. }
  198. @Override
  199. public void mouseReleased(MouseEvent e) {
  200. if (!disabled) {
  201. x = e.getX();
  202. y = e.getY();
  203. dragging = false;
  204. if (drawEdge) {
  205. drawEdge = false;
  206. drawDeleteEdge();
  207. }
  208. if (dragged) {
  209. try {
  210. /**
  211. * Save State before performing NodePlacement, replacement e.g.
  212. */
  213. control.autoSave();
  214. } catch (IOException ex) {
  215. ex.printStackTrace();
  216. }
  217. /**
  218. * check if tempCps could replace an Object on the UpperNodeanvas
  219. */
  220. if (GuiSettings.getSelectedObjects().size() == 1 && checkForReplacement(groupNode.getObjectsInThisLayer().toList(), tempCps,
  221. tempCps.getPosition().getX(), tempCps.getPosition().getY())) {
  222. /**
  223. * if UpperNode would be replaced, close its tabs
  224. */
  225. if (mayBeReplaced instanceof GroupNode)
  226. closeUpperNodeTab(mayBeReplaced.getId());
  227. /**
  228. * replace on canvas
  229. */
  230. control.replaceObjUpperNode(mayBeReplaced, tempCps, groupNode);
  231. mayBeReplaced = null;
  232. }
  233. }
  234. if (!e.isControlDown() && !dragged && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  235. control.clearSelection();
  236. control.addSelectedObject(tempCps);
  237. if (tempCps instanceof GroupNode)
  238. control.getObjectsInDepth();
  239. }
  240. dragged = false;
  241. // Rightclick List
  242. setRightClickMenu(e);
  243. markObjects();
  244. boolean doubleclick = doubleClick();
  245. if (doubleclick && tempCps != null && tempCps instanceof HolonSwitch sw) {
  246. sw.switchState();
  247. }
  248. if (doubleclick && tempCps != null && tempCps instanceof GroupNode groupnode) {
  249. control.getGui().openNewUpperNodeTab(groupnode);
  250. }
  251. control.calculateStateAndVisualForTimeStep(model.getCurrentIteration());
  252. repaint();
  253. }
  254. }
  255. @Override
  256. public void mouseDragged(MouseEvent e) {
  257. if (!disabled) {
  258. // If Edge is drawn
  259. x = e.getX();
  260. y = e.getY();
  261. if (!GuiSettings.getSelectedObjects().contains(tempCps) && !doMark) {
  262. control.clearSelection();
  263. if (tempCps != null) {
  264. control.addSelectedObject(tempCps);
  265. }
  266. }
  267. if (dragging) {
  268. try {
  269. // tempCps in the upperNode? else its a connected Object from
  270. // outside
  271. if (groupNode.getObjectsInThisLayer().anyMatch(obj -> obj.equals(tempCps))) {
  272. dragged = true;
  273. float xDist, yDist; // Distance
  274. x = e.getX();
  275. y = e.getY();
  276. // Make sure its in bounds
  277. if (e.getX() < GuiSettings.getPictureScaleDiv2() + +5)
  278. x = GuiSettings.getPictureScaleDiv2() + +5;
  279. else if (e.getX() > this.getWidth() - GuiSettings.getPictureScaleDiv2())
  280. x = this.getWidth() - GuiSettings.getPictureScaleDiv2();
  281. if (e.getY() < GuiSettings.getPictureScaleDiv2())
  282. y = GuiSettings.getPictureScaleDiv2();
  283. else if (e.getY() > this.getHeight() - GuiSettings.getPictureScaleDiv2())
  284. y = this.getHeight() - GuiSettings.getPictureScaleDiv2();
  285. // Distance
  286. xDist = x - tempCps.getPosition().getX();
  287. yDist = y - tempCps.getPosition().getY();
  288. tempCps.setPosition(x, y); // Drag Position
  289. // TipText Position and name
  290. toolTip = true;
  291. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  292. toolTipPos.setX(tempCps.getPosition().getX() - GuiSettings.getPictureScaleDiv2());
  293. toolTipPos.setY(tempCps.getPosition().getY() - GuiSettings.getPictureScaleDiv2());
  294. // All Selected Objects
  295. for (AbstractCanvasObject cps : GuiSettings.getSelectedObjects()) {
  296. if (cps != tempCps) {
  297. x = (int) (cps.getPosition().getX() + xDist);
  298. y = (int) (cps.getPosition().getY() + yDist);
  299. // Make sure its in bounds
  300. if (x < +5 + GuiSettings.getPictureScaleDiv2())
  301. x = GuiSettings.getPictureScaleDiv2() + +5;
  302. else if (x > this.getWidth() - GuiSettings.getPictureScaleDiv2())
  303. x = this.getWidth() - GuiSettings.getPictureScaleDiv2();
  304. if (y <= GuiSettings.getPictureScaleDiv2())
  305. y = GuiSettings.getPictureScaleDiv2();
  306. else if (y > this.getHeight() - GuiSettings.getPictureScaleDiv2())
  307. y = this.getHeight() - GuiSettings.getPictureScaleDiv2();
  308. cps.setPosition(x, y);
  309. }
  310. }
  311. }
  312. /**
  313. * check if something would be replaced
  314. */
  315. if (GuiSettings.getSelectedObjects().size() == 1)
  316. checkForReplacement(groupNode.getObjectsInThisLayer().toList(), tempCps, x, y);
  317. repaint();
  318. } catch (Exception eex) {
  319. }
  320. }
  321. // Mark Objects
  322. if (doMark) {
  323. tempSelected.clear();
  324. groupNode.getObjectsInThisLayer().forEach(cps ->
  325. {
  326. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  327. if (sx >= x) {
  328. x1 = x;
  329. x2 = sx;
  330. }
  331. if (sy >= y) {
  332. y1 = y;
  333. y2 = sy;
  334. }
  335. if (x1 <= cps.getPosition().getX() + GuiSettings.getPictureScaleDiv2()
  336. && y1 <= cps.getPosition().getY() + GuiSettings.getPictureScaleDiv2() && x2 >= cps.getPosition().getX()
  337. && y2 >= cps.getPosition().getY()) {
  338. tempSelected.add(cps);
  339. }
  340. });
  341. }
  342. repaint();
  343. }
  344. }
  345. @Override
  346. public void mouseMoved(MouseEvent e) {
  347. {
  348. x = e.getX();
  349. y = e.getY();
  350. // Everything for the tooltip :)
  351. boolean on = false;
  352. Optional<AbstractCanvasObject> objectUnderCursor = groupNode.getObjectsInThisLayer().filter(cps -> {
  353. cx = cps.getPosition().getX() - GuiSettings.getPictureScaleDiv2();
  354. cy = cps.getPosition().getY() - GuiSettings.getPictureScaleDiv2();
  355. boolean mouseOverObject = x - GuiSettings.getPictureScale() <= cx && y - GuiSettings.getPictureScale() <= cy && x >= cx
  356. && y >= cy;
  357. return mouseOverObject;
  358. }).findAny();
  359. on |= objectUnderCursor.isPresent();
  360. objectUnderCursor.ifPresent(cps -> {
  361. toolTipPos.setX(cps.getPosition().getX() - GuiSettings.getPictureScaleDiv2());
  362. toolTipPos.setY(cps.getPosition().getY() + GuiSettings.getPictureScaleDiv2());
  363. toolTipText = cps.getName() + ", " + cps.getId();
  364. });
  365. toolTip = on;
  366. repaint();
  367. }
  368. }
  369. }