AbstractCanvas.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package holeg.ui.view.canvas;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.Point;
  8. import java.awt.event.MouseEvent;
  9. import java.util.ArrayList;
  10. import java.util.HashSet;
  11. import java.util.List;
  12. import java.util.Set;
  13. import java.util.TimerTask;
  14. import java.util.stream.Stream;
  15. import javax.swing.JMenuItem;
  16. import javax.swing.JPanel;
  17. import javax.swing.JPopupMenu;
  18. import javax.swing.Timer;
  19. import holeg.model.AbstractCanvasObject;
  20. import holeg.model.Edge;
  21. import holeg.model.GroupNode;
  22. import holeg.model.HolonObject;
  23. import holeg.model.Node;
  24. import holeg.ui.controller.Control;
  25. import holeg.ui.model.GuiSettings;
  26. import holeg.ui.model.Model;
  27. import holeg.utility.math.vector.Vec2i;
  28. /**
  29. * Collection of methods and values needed in both <code>MyCanvas</code> and
  30. * <code>UpperNodeCanvas</code>
  31. * <p>
  32. * Although Java works on references we chose to add explicit return values for
  33. * clearer code understanding in most cases
  34. *
  35. * @author: I. Dix
  36. */
  37. public abstract class AbstractCanvas extends JPanel {
  38. /**
  39. * Version
  40. */
  41. private static final long serialVersionUID = 1L;
  42. final JMenuItem itemCut = new JMenuItem("Cut");
  43. final JMenuItem itemCopy = new JMenuItem("Copy");
  44. public final JMenuItem itemPaste = new JMenuItem("Paste");
  45. final JMenuItem itemDelete = new JMenuItem("Delete");
  46. final JMenuItem itemGroup = new JMenuItem("Group");
  47. final JMenuItem itemUngroup = new JMenuItem("Ungroup");
  48. final JMenuItem itemAlign = new JMenuItem("Align selected");
  49. final JMenuItem itemCreateTemplate = new JMenuItem("Create Template");
  50. final int ANIMTIME = 500; // animation Time
  51. private final int animFPS = 60;
  52. final int animDelay = 1000 / animFPS; // animation Delay
  53. protected Model model;
  54. protected Control control;
  55. protected int x = 0;
  56. protected int y = 0;
  57. // Selection
  58. //TODO(Tom2021-12-20): remove tmpCps or make is optional
  59. public AbstractCanvasObject tempCps = null;
  60. // Replacement
  61. /**
  62. * the CpsObject that might be replaced by drag&drop
  63. */
  64. public AbstractCanvasObject mayBeReplaced = null;
  65. // PopUpMenu
  66. JPopupMenu popmenu = new JPopupMenu();
  67. // Tooltip
  68. boolean toolTip; // Tooltip on or off
  69. Vec2i toolTipPos = new Vec2i(); // Tooltip Position
  70. String toolTipText = "";
  71. protected Set<AbstractCanvasObject> tempSelected = new HashSet<>();
  72. boolean showConnectionInformation;
  73. boolean dragging = false; // for dragging
  74. boolean dragged = false; // if an object/objects was/were dragged
  75. boolean drawEdge = false; // for drawing edges
  76. boolean doMark = false; // for double click
  77. public Edge edgeHighlight = null;
  78. Point mousePosition = new Point(); // Mouse Position when
  79. ArrayList<Vec2i> savePos;
  80. // edge Object Start Point
  81. int cx, cy;
  82. int sx, sy; // Mark Coords
  83. Vec2i unPos;
  84. // Animation
  85. Timer animT; // animation Timer
  86. int animDuration = ANIMTIME; // animation Duration
  87. int animSteps = animDuration / animDelay; // animation Steps;
  88. List<AbstractCanvasObject> animCps = null;
  89. // Graphics
  90. Image img = null; // Contains the image to draw on the Canvas
  91. Graphics2D g2; // For Painting
  92. float scalediv20 = GuiSettings.getPictureScale() / 20;
  93. // Mouse
  94. private boolean click = false;
  95. // ------------------------------------------ METHODS
  96. // ------------------------------------------
  97. class ACpsHandle {
  98. public AbstractCanvasObject object;
  99. ACpsHandle(AbstractCanvasObject object) {
  100. this.object = object;
  101. }
  102. public String toString() {
  103. return object.toString();
  104. }
  105. }
  106. void drawMarker() {
  107. if (sx > x && sy > y) {
  108. g2.drawRect(x, y, sx - x, sy - y);
  109. } else if (sx < x && sy < y) {
  110. g2.drawRect(sx, sy, x - sx, y - sy);
  111. } else if (sx >= x) {
  112. g2.drawRect(x, sy, sx - x, y - sy);
  113. } else if (sy >= y) {
  114. g2.drawRect(sx, y, x - sx, sy - y);
  115. }
  116. }
  117. /**
  118. * @deprecated
  119. * @param g
  120. */
  121. void showTooltip(Graphics g) {
  122. if (toolTip) {
  123. g2.setColor(new Color(255, 225, 150));
  124. g2.setStroke(new BasicStroke(1));
  125. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  126. // width
  127. // fixed x and y Position to the screen
  128. int fixXPos = toolTipPos.getX() - (textWidth >> 1) + GuiSettings.getPictureScaleDiv2();
  129. int fixYPos = toolTipPos.getY();
  130. if (fixXPos < 0) {
  131. fixXPos = 0;
  132. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  133. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  134. }
  135. if (fixYPos + 16 > this.getHeight()) {
  136. fixYPos -= (fixYPos + 16) - this.getHeight();
  137. }
  138. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  139. g2.setColor(Color.BLACK);
  140. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  141. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  142. }
  143. }
  144. void setRightClickMenu(MouseEvent e) {
  145. if (e.getButton() == MouseEvent.BUTTON3) {
  146. itemPaste.setEnabled(true);
  147. if (tempCps != null) {
  148. itemPaste.setEnabled(true);
  149. itemDelete.setEnabled(true);
  150. itemCut.setEnabled(true);
  151. itemCopy.setEnabled(true);
  152. itemAlign.setEnabled(true);
  153. // tracking
  154. if (tempCps != null) {
  155. itemGroup.setEnabled(true);
  156. }
  157. // ungrouping
  158. if (tempCps instanceof GroupNode)
  159. itemUngroup.setEnabled(true);
  160. else
  161. itemUngroup.setEnabled(false);
  162. if (GuiSettings.getSelectedObjects().isEmpty()) {
  163. control.addSelectedObject(tempCps);
  164. }
  165. if (tempCps instanceof HolonObject) {
  166. itemCreateTemplate.setEnabled(true);
  167. } else {
  168. itemCreateTemplate.setEnabled(false);
  169. }
  170. } else {
  171. itemAlign.setEnabled(false);
  172. itemCut.setEnabled(false);
  173. itemCopy.setEnabled(false);
  174. itemGroup.setEnabled(false);
  175. itemUngroup.setEnabled(false);
  176. itemCreateTemplate.setEnabled(false);
  177. if (edgeHighlight != null) {
  178. itemDelete.setEnabled(true);
  179. itemPaste.setEnabled(false);
  180. } else {
  181. itemDelete.setEnabled(false);
  182. itemPaste.setEnabled(true);
  183. }
  184. }
  185. mousePosition = this.getMousePosition();
  186. popmenu.show(e.getComponent(), e.getX(), e.getY());
  187. }
  188. }
  189. void markObjects() {
  190. if (doMark) {
  191. doMark = false;
  192. if (!tempSelected.isEmpty()) {
  193. control.toggleSelectedObjects(tempSelected);
  194. control.getObjectsInDepth();
  195. tempSelected.clear();
  196. }
  197. }
  198. }
  199. int[] determineMousePositionOnEdge(Edge p) {
  200. int lx, ly, hx, hy;
  201. if (p.getA().getPosition().getX() > p.getB().getPosition().getX()) {
  202. hx = p.getA().getPosition().getX() + GuiSettings.getPictureScaleDiv2() + 7;
  203. lx = p.getB().getPosition().getX() + GuiSettings.getPictureScaleDiv2() - 7;
  204. } else {
  205. lx = p.getA().getPosition().getX() + GuiSettings.getPictureScaleDiv2() - 7;
  206. hx = p.getB().getPosition().getX() + GuiSettings.getPictureScaleDiv2() + 7;
  207. }
  208. if (p.getA().getPosition().getY() > p.getB().getPosition().getY()) {
  209. hy = p.getA().getPosition().getY() + GuiSettings.getPictureScaleDiv2() + 7;
  210. ly = p.getB().getPosition().getY() + GuiSettings.getPictureScaleDiv2() - 7;
  211. } else {
  212. ly = p.getA().getPosition().getY() + GuiSettings.getPictureScaleDiv2() - 7;
  213. hy = p.getB().getPosition().getY() + GuiSettings.getPictureScaleDiv2() + 7;
  214. }
  215. return new int[] { lx, ly, hx, hy };
  216. }
  217. /**
  218. * Checks if a double click was made.
  219. *
  220. * @return true if doublecklick, false if not
  221. */
  222. boolean doubleClick() {
  223. if (click) {
  224. click = false;
  225. return true;
  226. } else {
  227. click = true;
  228. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  229. t.schedule(new TimerTask() {
  230. @Override
  231. public void run() {
  232. click = false;
  233. }
  234. }, 500);
  235. }
  236. return false;
  237. }
  238. abstract void drawDeleteEdge();
  239. /**
  240. * Checks if {@code draggedCps} or a new cpsObject at Position (x,y) could
  241. * replace exactly one object in {@code objects}. Saves the object that would be
  242. * replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  243. *
  244. * @param objects list of objects that could be replaced
  245. * @param draggedCps Object that might replace
  246. * @param x Position of the objects that might replace
  247. * @param y Position of the objects that might replace
  248. * @return true if exactly one Object could be replaced
  249. */
  250. protected boolean checkForReplacement(Stream<AbstractCanvasObject> objects, AbstractCanvasObject draggedCps,
  251. int x, int y) {
  252. /** distance treshold for replacement */
  253. int treshhold = GuiSettings.getPictureScaleDiv2();
  254. /** number of Objects that might be replaced (should be 1) */
  255. int replaceCounter = 0;
  256. /** last object that could be replaced */
  257. AbstractCanvasObject toBeReplaced = null;
  258. /** Position of object that might be replaced */
  259. Vec2i p;
  260. /** for each cps on Canvas */
  261. if (draggedCps == null || !(draggedCps instanceof Node) && !(draggedCps instanceof Node)) {
  262. for (AbstractCanvasObject cps : objects.toList()) {
  263. /** same object -> ignore */
  264. if (cps == draggedCps)
  265. continue;
  266. /** set Position of object that might be replaced */
  267. p = cps.getPosition();
  268. /** if near enough */
  269. if (Math.abs(x - p.getX()) < treshhold && Math.abs(y - p.getY()) < treshhold) {
  270. replaceCounter++;
  271. toBeReplaced = cps;
  272. /**
  273. * if too many Objects could be replaced: stop searching, because it would not
  274. * be clear which one should be replaced
  275. */
  276. if (replaceCounter > 1)
  277. break;
  278. }
  279. }
  280. }
  281. /**
  282. * return true if exactly one obect would be replaced
  283. */
  284. if (replaceCounter == 1 && toBeReplaced != null) {
  285. mayBeReplaced = toBeReplaced;
  286. return true;
  287. } else {
  288. mayBeReplaced = null;
  289. return false;
  290. }
  291. }
  292. /**
  293. * Checks if an inserted new Object could replace exactly one object on the
  294. * canvas. Saves the object that would be replaced in
  295. * {@link AbstractCanvas}.{@code MayBeReplaced}
  296. *
  297. * @param x Position of the objects that might replace
  298. * @param y Position of the objects that might replace
  299. * @return true if exactly one Object could be replaced
  300. */
  301. public abstract boolean checkForReplacement(int x, int y);
  302. /**
  303. * highlights the object that mayBeReplaced
  304. *
  305. * @param g2
  306. */
  307. protected void highlightMayBeReplaced(Graphics2D g2) {
  308. if (mayBeReplaced != null) {
  309. g2.setColor(Color.RED);
  310. g2.fillRect((int) (mayBeReplaced.getPosition().getX() - GuiSettings.getPictureScaleDiv2() - (scalediv20 + 3)),
  311. (int) (mayBeReplaced.getPosition().getY() - GuiSettings.getPictureScaleDiv2() - (scalediv20 + 3)),
  312. (int) (GuiSettings.getPictureScale() + ((scalediv20 + 3) * 2)),
  313. (int) (GuiSettings.getPictureScale() + ((scalediv20 + 3) * 2)));
  314. }
  315. }
  316. /**
  317. * Align alle Objects on the Canvas to a Grid with objects every 10 pixels
  318. */
  319. public abstract void tryToAlignObjects();
  320. /**
  321. * Aligns the Object the a grid
  322. *
  323. * @param cps Object that should be aligned
  324. * @param distance distance between the AlignmentGrid Lines. (objects every
  325. * 'distance' pixels
  326. */
  327. protected void align(AbstractCanvasObject cps, int distance) {
  328. /** Position of the AbstractCpsObject which should be aligned */
  329. Vec2i p = cps.getPosition();
  330. // calculate how many pixels the cps should be decreased to align
  331. /** x offset relative to a grid with lines every distance pixels */
  332. int x_off = cps.getPosition().getX() % distance;
  333. /** y offset relative to a grid with lines every distance pixels */
  334. int y_off = cps.getPosition().getY() % distance;
  335. // align to the other Line, if it is nearer
  336. if (x_off > distance / 2)
  337. x_off -= distance;
  338. if (y_off > distance / 2)
  339. y_off -= distance;
  340. /** set new Position */
  341. cps.setPosition(p.getX() - x_off, p.getY() - y_off);
  342. }
  343. /**
  344. * Closes a tab of the UpperNode with ID upperNodeID
  345. *
  346. * @param upperNodeId
  347. */
  348. public abstract void closeUpperNodeTab(int upperNodeId);
  349. }