AbstractCanvas.java 13 KB

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