AbstractCanvas.java 13 KB

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