AbstractCanvas.java 12 KB

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