AbstractCanvas.java 13 KB

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