AbstractCanvas.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. package ui.view.main;
  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.TimerTask;
  11. import javax.swing.CellEditor;
  12. import javax.swing.JMenuItem;
  13. import javax.swing.JPanel;
  14. import javax.swing.JPopupMenu;
  15. import javax.swing.JTable;
  16. import javax.swing.Timer;
  17. import classes.AbstractCanvasObject;
  18. import classes.Edge;
  19. import classes.GroupNode;
  20. import classes.HolonElement;
  21. import classes.HolonObject;
  22. import classes.Node;
  23. import classes.Position;
  24. import model.Model;
  25. import ui.controller.Control;
  26. import ui.controller.UpdateController;
  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. UpdateController updCon;
  59. //Replacement
  60. /**
  61. * the CpsObject that might be replaced by drag&drop
  62. */
  63. public AbstractCanvasObject mayBeReplaced = null;
  64. // PopUpMenu
  65. JPopupMenu popmenu = new JPopupMenu();
  66. // Tooltip
  67. boolean toolTip; // Tooltip on or off
  68. Position toolTipPos = new Position(); // Tooltip Position
  69. String toolTipText = "";
  70. ArrayList<HolonElement> dataSelected = new ArrayList<>();
  71. public ArrayList<AbstractCanvasObject> tempSelected = new ArrayList<>();
  72. boolean[] showedInformation = new boolean[5];
  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<Position> savePos;
  81. // edge Object Start Point
  82. int cx, cy;
  83. int sx, sy; // Mark Coords
  84. Position unPos;
  85. // Animation
  86. Timer animT; // animation Timer
  87. int animDuration = ANIMTIME; // animation Duration
  88. int animSteps = animDuration / animDelay; // animation Steps;
  89. ArrayList<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.x - (textWidth >> 1) + model.getScaleDiv2();
  130. int fixYPos = toolTipPos.y;
  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 (model.getSelectedCpsObjects().size() == 0) {
  164. controller.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. for (AbstractCanvasObject cps : tempSelected) {
  194. if (!model.getSelectedCpsObjects().contains(cps)) {
  195. controller.addSelectedObject(cps);
  196. }
  197. }
  198. controller.getObjectsInDepth();
  199. tempSelected.clear();
  200. }
  201. }
  202. int[] determineMousePositionOnEdge(Edge p) {
  203. int lx, ly, hx, hy;
  204. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  205. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  206. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  207. } else {
  208. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  209. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  210. }
  211. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  212. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  213. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  214. } else {
  215. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  216. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  217. }
  218. return new int[] { lx, ly, hx, hy };
  219. }
  220. /**
  221. * Checks if a double click was made.
  222. *
  223. * @return true if doublecklick, false if not
  224. */
  225. boolean doubleClick() {
  226. if (click) {
  227. click = false;
  228. return true;
  229. } else {
  230. click = true;
  231. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  232. t.schedule(new TimerTask() {
  233. @Override
  234. public void run() {
  235. click = false;
  236. }
  237. }, 500);
  238. }
  239. return false;
  240. }
  241. boolean setToolTipInfoAndPosition(boolean on, AbstractCanvasObject cps) {
  242. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  243. on = true;
  244. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  245. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  246. toolTipText = cps.getName() + ", " + cps.getId();
  247. }
  248. return on;
  249. }
  250. abstract void drawDeleteEdge();
  251. void triggerUpdateController() {
  252. updCon.paintProperties(tempCps);
  253. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  254. updCon.refreshTableProperties(model.getPropertyTable());
  255. }
  256. /**
  257. * Checks if {@code draggedCps} or a new cpsObject at Position (x,y) could replace exactly one object
  258. * in {@code objects}.
  259. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  260. * @param objects list of objects that could be replaced
  261. * @param draggedCps Object that might replace
  262. * @param x Position of the objects that might replace
  263. * @param y Position of the objects that might replace
  264. * @return true if exactly one Object could be replaced
  265. */
  266. protected boolean checkForReplacement(ArrayList<AbstractCanvasObject> objects, AbstractCanvasObject draggedCps, int x, int y){
  267. /** distance treshold for replacement */
  268. int treshhold = controller.getScale()/2;
  269. /** number of Objects that might be replaced (should be 1) */
  270. int replaceCounter = 0;
  271. /** last object that could be replaced */
  272. AbstractCanvasObject toBeReplaced = null;
  273. /** Position of object that might be replaced */
  274. Position p;
  275. /** for each cps on Canvas */
  276. if(draggedCps == null || !(draggedCps instanceof Node) && !(draggedCps instanceof Node)){
  277. for (AbstractCanvasObject cps : objects){
  278. /** same object -> ignore */
  279. if(cps == draggedCps)continue;
  280. /** set Position of object that might be replaced */
  281. p = cps.getPosition();
  282. /** if near enough */
  283. if(Math.abs(x-p.x)<treshhold && Math.abs(y-p.y)<treshhold){
  284. replaceCounter++;
  285. toBeReplaced = cps;
  286. /**
  287. * if too many Objects could be replaced:
  288. * stop searching, because it would not be clear which one should
  289. * be replaced
  290. */
  291. if(replaceCounter>1)break;
  292. }
  293. }
  294. }
  295. /**
  296. * return true if exactly one obect would be replaced
  297. */
  298. if( replaceCounter == 1 && toBeReplaced != null){
  299. mayBeReplaced = toBeReplaced;
  300. return true;
  301. }else{
  302. mayBeReplaced = null;
  303. return false;
  304. }
  305. }
  306. /**
  307. * Checks if an inserted new Object could replace exactly one object on the canvas.
  308. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  309. * @param x Position of the objects that might replace
  310. * @param y Position of the objects that might replace
  311. * @return true if exactly one Object could be replaced
  312. */
  313. public abstract boolean checkForReplacement(int x, int y);
  314. /**
  315. * highlights the object that mayBeReplaced
  316. * @param g2
  317. */
  318. protected void highlightMayBeReplaced(Graphics2D g2) {
  319. if(mayBeReplaced != null){
  320. g2.setColor(Color.RED);
  321. g2.fillRect(
  322. (int) (mayBeReplaced.getPosition().x
  323. - controller.getScaleDiv2() - (scalediv20 + 3)),
  324. (int) (mayBeReplaced.getPosition().y
  325. - controller.getScaleDiv2() - (scalediv20 + 3)),
  326. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  327. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  328. }
  329. }
  330. /**
  331. * Align alle Objects on the Canvas to a Grid with objects every 10 pixels
  332. */
  333. public abstract void tryToAlignObjects();
  334. /**
  335. * Aligns the Object the a grid
  336. * @param cps Object that should be aligned
  337. * @param distance distance between the AlignmentGrid Lines. (objects every 'distance' pixels
  338. */
  339. protected void align(AbstractCanvasObject cps, int distance) {
  340. /** Position of the AbstractCpsObject which should be aligned */
  341. Position p = cps.getPosition();
  342. //calculate how many pixels the cps should be decreased to align
  343. /** x offset relative to a grid with lines every distance pixels */
  344. int x_off = cps.getPosition().x % distance;
  345. /** y offset relative to a grid with lines every distance pixels */
  346. int y_off = cps.getPosition().y % distance;
  347. //align to the other Line, if it is nearer
  348. if(x_off > distance/2)
  349. x_off -= distance;
  350. if(y_off > distance/2)
  351. y_off -= distance;
  352. /** set new Position */
  353. cps.setPosition(p.x-x_off, p.y-y_off);
  354. }
  355. /**
  356. * Stops Editing in HolonElementTable and PropertyTable
  357. */
  358. protected void stopEditing() {
  359. /**
  360. * Stop Editing, if mouse exits the Table
  361. */
  362. JTable holElem = model.getTableHolonElement();
  363. CellEditor cellEditor = holElem.getCellEditor();
  364. if (cellEditor != null) {
  365. if (cellEditor.getCellEditorValue() != null) {
  366. /** TODO: Maybe try to save current Data */
  367. cellEditor.stopCellEditing();
  368. } else {
  369. cellEditor.cancelCellEditing();
  370. }
  371. }
  372. JTable propertys = model.getTableProperties();
  373. cellEditor = propertys.getCellEditor();
  374. if (cellEditor != null) {
  375. if (cellEditor.getCellEditorValue() != null) {
  376. /** TODO: Maybe try to save current Data */
  377. cellEditor.stopCellEditing();
  378. } else {
  379. cellEditor.cancelCellEditing();
  380. }
  381. }
  382. }
  383. /**
  384. * Closes a tab of the UpperNode with ID upperNodeID
  385. * @param upperNodeId
  386. */
  387. public abstract void closeUpperNodeTab(int upperNodeId);
  388. }