AbstractCanvas.java 12 KB

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