AbstractCanvas.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. String paintEdge(CpsEdge con, String maxCap) {
  87. if (con!=null && con.getA() != null && con.getB() != null && con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
  88. && con != edgeHighlight) {
  89. if (con.getConnected() == CpsEdge.CON_UPPER_NODE
  90. || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
  91. setEdgeState(con);
  92. } else {
  93. g2.setColor(Color.DARK_GRAY);
  94. g2.setStroke(new BasicStroke(2));
  95. }
  96. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  97. con.getB().getPosition().y);
  98. maxCap = setCapacityString(con, maxCap);
  99. if (showedInformation[0]) {
  100. if (con.getConnected() == CpsEdge.CON_UPPER_NODE
  101. || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
  102. g2.drawString(con.getFlow() + "/" + maxCap,
  103. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  104. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  105. } else {
  106. g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  107. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  108. }
  109. }
  110. }
  111. return maxCap;
  112. }
  113. void setEdgeState(CpsEdge con) {
  114. if (con.isWorking()) {
  115. g2.setColor(Color.GREEN);
  116. if (con.getCapacity() != CpsEdge.CAPACITY_INFINITE) {
  117. g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  118. }
  119. } else {
  120. g2.setColor(Color.RED);
  121. g2.setStroke(new BasicStroke(2));
  122. }
  123. }
  124. String setCapacityString(CpsEdge con, String maxCap) {
  125. if (con.getCapacity() == -1) {
  126. maxCap = Character.toString('\u221e');
  127. } else if (con.getCapacity() == -2) {
  128. maxCap = "???";
  129. } else {
  130. maxCap = String.valueOf(con.getCapacity());
  131. }
  132. return maxCap;
  133. }
  134. String drawEdgeLine(CpsEdge con, String maxCap) {
  135. if (con.getA().getId() == model.getSelectedObjectID() || model.getSelectedCpsObjects().contains(con.getA())
  136. || tempSelected.contains(con.getA()) || con.getB().getId() == model.getSelectedObjectID()
  137. || model.getSelectedCpsObjects().contains(con.getB())
  138. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  139. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  140. con.getB().getPosition().y);
  141. maxCap = setCapacityString(con, maxCap);
  142. if (showedInformation[0]) {
  143. if (con.getConnected() == CpsEdge.CON_UPPER_NODE
  144. || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
  145. g2.drawString(con.getFlow() + "/" + maxCap,
  146. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  147. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  148. } else {
  149. g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  150. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  151. }
  152. }
  153. }
  154. return maxCap;
  155. }
  156. /**
  157. * Paints the SupplyBar for the given cps object on the canvas
  158. *
  159. * @param g
  160. * Graphics used
  161. * @param cps
  162. * cpsObject which the supplyBar should be drawn for
  163. */
  164. protected void paintSupplyBar(Graphics g, AbstractCpsObject cps) {
  165. g2.setColor(Color.black);
  166. /**
  167. * draw and fill the supply Bar
  168. */
  169. if (model.getShowSupplyBars() && (cps instanceof HolonObject || cps instanceof HolonBattery))
  170. {
  171. // set Color & Percentage
  172. /**
  173. * percentage the SupplyBar is representing
  174. */
  175. float percentage = 0;
  176. /**
  177. * color of the SupplyBar
  178. */
  179. Color paintColor = Color.WHITE;
  180. if(cps instanceof HolonObject)
  181. {
  182. HolonObject hl = (HolonObject) cps;
  183. if (hl == null || !(hl.getState() == HolonObject.NOT_SUPPLIED
  184. || hl.getState() == HolonObject.PARTIALLY_SUPPLIED || hl.getState() == HolonObject.OVER_SUPPLIED)) {
  185. /**
  186. * don't show Bars for unsupplied oder fully supplied Objects
  187. */
  188. return;
  189. }
  190. /**
  191. * calculate Percentage & set Color
  192. */
  193. percentage = hl.getSuppliedPercentage();
  194. paintColor = hl.getColor();
  195. }
  196. else if (cps instanceof HolonBattery){
  197. HolonBattery hB = (HolonBattery) cps;
  198. if(hB == null || hB.getCapacity() == 0){
  199. return;
  200. }
  201. /**
  202. * get percentage filled
  203. */
  204. percentage = hB.getStateOfChargeAtTimeStep(model.getCurIteration()-1) / hB.getCapacity();
  205. /**
  206. * calculate the Color (Red->Yellow->Green)
  207. */
  208. Color color1 = Color.RED;
  209. Color color2 = Color.GREEN;
  210. //
  211. float colorPercentage;
  212. if(percentage < 0.5f){
  213. colorPercentage = percentage * 2;
  214. color1 = Color.RED;
  215. color2 = Color.YELLOW;
  216. }
  217. else {
  218. colorPercentage = (percentage - 0.5f) * 2;
  219. color1 = Color.YELLOW;
  220. color2 = Color.GREEN;
  221. }
  222. final int dRed = color2.getRed() - color1.getRed();
  223. final int dGreen = color2.getGreen() - color1.getGreen();
  224. final int dBlue = color2.getBlue() - color1.getBlue();
  225. int resultRed = color1.getRed() + (int)(colorPercentage * dRed);
  226. int resultGreen = color1.getGreen() + (int)(colorPercentage * dGreen);
  227. int resultBlue = color1.getBlue() + (int)( colorPercentage * dBlue);
  228. paintColor = new Color( resultRed,resultGreen,resultBlue);
  229. }
  230. /**
  231. * Starting position x of the bar
  232. */
  233. int barX = (int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20);
  234. /**
  235. * Starting position y of the bar
  236. */
  237. int barY = (int) (cps.getPosition().y - controller.getScaleDiv2() + controller.getScale() + 1);
  238. /**
  239. * if object should be replaced -> move bar below the ReplacementIndicator
  240. */
  241. if(mayBeReplaced==cps) barY += 3;
  242. /**
  243. * Width of the bar
  244. */
  245. int barWidth = (int) (controller.getScale() + ((scalediv20) * 2) - 1);
  246. /**
  247. * Height of the bar
  248. */
  249. int barHeight = (int) (controller.getScale() / 5);
  250. /**
  251. * draw Rectangle below the image
  252. */
  253. g2.setStroke(new BasicStroke(1));
  254. g2.drawRect(barX, barY, barWidth, barHeight);
  255. g2.setColor(paintColor);
  256. /** fill it accordingly if filled partially */
  257. if (percentage < 1)
  258. g2.fillRect(barX + 1, barY + 1, (int) ((barWidth - 1) * percentage), barHeight - 1);
  259. else /** over supplied / supplied bar should be fully filled */
  260. g2.fillRect(barX + 1, barY + 1, (int) (barWidth - 1), barHeight - 1);
  261. /** write percentage */
  262. if(percentage>1)
  263. g2.setColor(Color.WHITE);
  264. else
  265. g2.setColor(Color.BLACK);
  266. /** original font */
  267. Font oldFont = g2.getFont();
  268. g.setFont(new Font("TimesRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  269. String percentageString = (Math.round((percentage * 100))) + "%";
  270. int stringWidth = (int) g2.getFontMetrics().getStringBounds(percentageString, g2).getWidth();
  271. g2.drawString(percentageString, barX + barWidth / 2 + 1 - stringWidth / 2, barY + barHeight);
  272. /** recover Font and Color */
  273. g2.setFont(oldFont);
  274. g2.setColor(Color.BLACK);
  275. }
  276. }
  277. void setEdgePictureAndHighlighting(AbstractCpsObject cps) {
  278. // node image
  279. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  280. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  281. img = Util.loadImage("/Images/node_selected.png");
  282. } else {
  283. if (cps instanceof HolonSwitch) {//TODO: What the hell are thes ecalls doing here?
  284. if (((HolonSwitch) cps).getState(model.getCurIteration())) {
  285. ((HolonSwitch) cps).setAutoState(true);
  286. } else {
  287. ((HolonSwitch) cps).setAutoState(false);
  288. }
  289. }
  290. // Highlighting
  291. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  292. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  293. /**
  294. * draw selected Blue Frame
  295. */
  296. g2.setColor(Color.BLUE);
  297. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
  298. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
  299. (int) (controller.getScale() + (scalediv20 * 2)),
  300. (int) (controller.getScale() + (scalediv20 * 2)));
  301. /**
  302. *
  303. */
  304. if(cps instanceof HolonObject){
  305. g2.setColor(((HolonObject)cps).getColor());
  306. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20+3),
  307. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20+3),
  308. (int) (controller.getScale() + (scalediv20 * 2)-6),
  309. (int) (controller.getScale() + (scalediv20 * 2)-6));
  310. }
  311. if (showedInformation[1] && cps instanceof HolonObject) {
  312. g2.setColor(Color.BLACK);
  313. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  314. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
  315. cps.getPosition().y - controller.getScaleDiv2() - 10);
  316. }else if (showedInformation[1] && cps instanceof HolonBattery)
  317. {
  318. g2.setColor(Color.BLACK);
  319. g2.drawString(((HolonBattery) cps).getCanvasBatteryString(model.getCurIteration()), cps.getPosition().x - controller.getScaleDiv2(),
  320. cps.getPosition().y - controller.getScaleDiv2() - 10);
  321. }
  322. } else if (cps instanceof HolonObject) {
  323. g2.setColor(((HolonObject) cps).getColor());
  324. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
  325. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
  326. (int) (controller.getScale() + (scalediv20 * 2)),
  327. (int) (controller.getScale() + (scalediv20 * 2)));
  328. if (showedInformation[1]) {
  329. g2.setColor(Color.BLACK);
  330. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  331. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
  332. cps.getPosition().y - controller.getScaleDiv2() - 10);
  333. }
  334. }else if (cps instanceof HolonBattery) {
  335. if (showedInformation[1]) {
  336. g2.setColor(Color.BLACK);
  337. g2.drawString(((HolonBattery) cps).getCanvasBatteryString(model.getCurIteration()), cps.getPosition().x - controller.getScaleDiv2(),
  338. cps.getPosition().y - controller.getScaleDiv2() - 10);
  339. }
  340. }
  341. // set image
  342. if(cps instanceof HolonBattery)
  343. {
  344. img = Util.loadImage(((HolonBattery) cps).getImageBattery());
  345. }else
  346. {
  347. img = Util.loadImage(cps.getImage());
  348. }
  349. }
  350. }
  351. void drawMarker() {
  352. if (sx > x && sy > y) {
  353. g2.drawRect(x, y, sx - x, sy - y);
  354. } else if (sx < x && sy < y) {
  355. g2.drawRect(sx, sy, x - sx, y - sy);
  356. } else if (sx >= x) {
  357. g2.drawRect(x, sy, sx - x, y - sy);
  358. } else if (sy >= y) {
  359. g2.drawRect(sx, y, x - sx, sy - y);
  360. }
  361. }
  362. void showTooltip(Graphics g) {
  363. if (toolTip) {
  364. g2.setColor(new Color(255, 225, 150));
  365. g2.setStroke(new BasicStroke(1));
  366. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  367. // width
  368. // fixed x and y Position to the screen
  369. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2();
  370. int fixYPos = toolTipPos.y;
  371. if (fixXPos < 0) {
  372. fixXPos = 0;
  373. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  374. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  375. }
  376. if (fixYPos + 16 > this.getHeight()) {
  377. fixYPos -= (fixYPos + 16) - this.getHeight();
  378. }
  379. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  380. g2.setColor(Color.BLACK);
  381. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  382. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  383. }
  384. }
  385. void setConsoleTextAfterSelect(AbstractCpsObject cps) {
  386. if (model.getShowConsoleLog()) {
  387. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  388. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  389. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  390. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  391. }
  392. }
  393. void setRightClickMenu(MouseEvent e) {
  394. if (e.getButton() == MouseEvent.BUTTON3) {
  395. itemPaste.setEnabled(true);
  396. if (tempCps != null) {
  397. itemPaste.setEnabled(true);
  398. itemDelete.setEnabled(true);
  399. itemCut.setEnabled(true);
  400. itemCopy.setEnabled(true);
  401. itemAlign.setEnabled(true);
  402. // tracking
  403. if (tempCps != null) {
  404. itemGroup.setEnabled(true);
  405. itemTrack.setEnabled(true);
  406. itemUntrack.setEnabled(true);
  407. }
  408. // ungrouping
  409. if (tempCps instanceof CpsUpperNode)
  410. itemUngroup.setEnabled(true);
  411. else
  412. itemUngroup.setEnabled(false);
  413. if (model.getSelectedCpsObjects().size() == 0) {
  414. controller.addSelectedObject(tempCps);
  415. }
  416. if (tempCps instanceof HolonObject) {
  417. itemCreateTemplate.setEnabled(true);
  418. } else {
  419. itemCreateTemplate.setEnabled(false);
  420. }
  421. } else {
  422. itemAlign.setEnabled(false);
  423. itemCut.setEnabled(false);
  424. itemCopy.setEnabled(false);
  425. itemGroup.setEnabled(false);
  426. itemUngroup.setEnabled(false);
  427. itemTrack.setEnabled(false);
  428. itemUntrack.setEnabled(false);
  429. itemCreateTemplate.setEnabled(false);
  430. if (edgeHighlight != null) {
  431. itemDelete.setEnabled(true);
  432. itemPaste.setEnabled(false);
  433. } else {
  434. itemDelete.setEnabled(false);
  435. itemPaste.setEnabled(true);
  436. }
  437. }
  438. mousePosition = this.getMousePosition();
  439. popmenu.show(e.getComponent(), e.getX(), e.getY());
  440. }
  441. }
  442. void markObjects() {
  443. if (doMark) {
  444. doMark = false;
  445. for (AbstractCpsObject cps : tempSelected) {
  446. if (!model.getSelectedCpsObjects().contains(cps)) {
  447. controller.addSelectedObject(cps);
  448. }
  449. }
  450. controller.getObjectsInDepth();
  451. tempSelected.clear();
  452. }
  453. }
  454. int[] determineMousePositionOnEdge(CpsEdge p) {
  455. int lx, ly, hx, hy;
  456. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  457. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  458. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  459. } else {
  460. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  461. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  462. }
  463. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  464. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  465. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  466. } else {
  467. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  468. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  469. }
  470. return new int[] { lx, ly, hx, hy };
  471. }
  472. /**
  473. * Checks if a double click was made.
  474. *
  475. * @return true if doublecklick, false if not
  476. */
  477. boolean doubleClick() {
  478. if (click) {
  479. click = false;
  480. return true;
  481. } else {
  482. click = true;
  483. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  484. t.schedule(new TimerTask() {
  485. @Override
  486. public void run() {
  487. click = false;
  488. }
  489. }, 500);
  490. }
  491. return false;
  492. }
  493. boolean setToolTipInfoAndPosition(boolean on, AbstractCpsObject cps) {
  494. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  495. on = true;
  496. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  497. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  498. toolTipText = cps.getName() + ", " + cps.getId();
  499. }
  500. return on;
  501. }
  502. abstract void drawDeleteEdge();
  503. void triggerUpdateController() {
  504. updCon.paintProperties(tempCps);
  505. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  506. updCon.refreshTableProperties(model.getPropertyTable());
  507. }
  508. /**
  509. * Checks if {@code draggedCps} or a new cpsObject at Position (x,y) could replace exactly one object
  510. * in {@code objects}.
  511. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  512. * @param objects list of objects that could be replaced
  513. * @param draggedCps Object that might replace
  514. * @param x Position of the objects that might replace
  515. * @param y Position of the objects that might replace
  516. * @return true if exactly one Object could be replaced
  517. */
  518. protected boolean checkForReplacement(ArrayList<AbstractCpsObject> objects, AbstractCpsObject draggedCps, int x, int y){
  519. /** distance treshold for replacement */
  520. int treshhold = controller.getScale()/2;
  521. /** number of Objects that might be replaced (should be 1) */
  522. int replaceCounter = 0;
  523. /** last object that could be replaced */
  524. AbstractCpsObject toBeReplaced = null;
  525. /** Position of object that might be replaced */
  526. Position p;
  527. /** for each cps on Canvas */
  528. if(draggedCps == null || !(draggedCps instanceof CpsNode) && !(draggedCps instanceof CpsNode)){
  529. for (AbstractCpsObject cps : objects){
  530. /** same object -> ignore */
  531. if(cps == draggedCps)continue;
  532. /** set Position of object that might be replaced */
  533. p = cps.getPosition();
  534. /** if near enough */
  535. if(Math.abs(x-p.x)<treshhold && Math.abs(y-p.y)<treshhold){
  536. replaceCounter++;
  537. toBeReplaced = cps;
  538. /**
  539. * if too many Objects could be replaced:
  540. * stop searching, because it would not be clear which one should
  541. * be replaced
  542. */
  543. if(replaceCounter>1)break;
  544. }
  545. }
  546. }
  547. /**
  548. * return true if exactly one obect would be replaced
  549. */
  550. if( replaceCounter == 1 && toBeReplaced != null){
  551. mayBeReplaced = toBeReplaced;
  552. return true;
  553. }else{
  554. mayBeReplaced = null;
  555. return false;
  556. }
  557. }
  558. /**
  559. * Checks if an inserted new Object could replace exactly one object on the canvas.
  560. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  561. * @param x Position of the objects that might replace
  562. * @param y Position of the objects that might replace
  563. * @return true if exactly one Object could be replaced
  564. */
  565. public abstract boolean checkForReplacement(int x, int y);
  566. /**
  567. * highlights the object that mayBeReplaced
  568. * @param g2
  569. */
  570. protected void highlightMayBeReplaced(Graphics2D g2) {
  571. if(mayBeReplaced != null){
  572. g2.setColor(Color.RED);
  573. g2.fillRect(
  574. (int) (mayBeReplaced.getPosition().x
  575. - controller.getScaleDiv2() - (scalediv20 + 3)),
  576. (int) (mayBeReplaced.getPosition().y
  577. - controller.getScaleDiv2() - (scalediv20 + 3)),
  578. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  579. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  580. }
  581. }
  582. /**
  583. * Align alle Objects on the Canvas to a Grid with objects every 10 pixels
  584. */
  585. public abstract void tryToAlignObjects();
  586. /**
  587. * Aligns the Object the a grid
  588. * @param cps Object that should be aligned
  589. * @param distance distance between the AlignmentGrid Lines. (objects every 'distance' pixels
  590. */
  591. protected void align(AbstractCpsObject cps, int distance) {
  592. /** Position of the AbstractCpsObject which should be aligned */
  593. Position p = cps.getPosition();
  594. //calculate how many pixels the cps should be decreased to align
  595. /** x offset relative to a grid with lines every distance pixels */
  596. int x_off = cps.getPosition().x % distance;
  597. /** y offset relative to a grid with lines every distance pixels */
  598. int y_off = cps.getPosition().y % distance;
  599. //align to the other Line, if it is nearer
  600. if(x_off > distance/2)
  601. x_off -= distance;
  602. if(y_off > distance/2)
  603. y_off -= distance;
  604. /** set new Position */
  605. cps.setPosition(p.x-x_off, p.y-y_off);
  606. }
  607. /**
  608. * Stops Editing in HolonElementTable and PropertyTable
  609. */
  610. protected void stopEditing() {
  611. /**
  612. * Stop Editing, if mouse exits the Table
  613. */
  614. JTable holElem = model.getTableHolonElement();
  615. CellEditor cellEditor = holElem.getCellEditor();
  616. if (cellEditor != null) {
  617. if (cellEditor.getCellEditorValue() != null) {
  618. /** TODO: Maybe try to save current Data */
  619. cellEditor.stopCellEditing();
  620. } else {
  621. cellEditor.cancelCellEditing();
  622. }
  623. }
  624. JTable propertys = model.getTableProperties();
  625. cellEditor = propertys.getCellEditor();
  626. if (cellEditor != null) {
  627. if (cellEditor.getCellEditorValue() != null) {
  628. /** TODO: Maybe try to save current Data */
  629. cellEditor.stopCellEditing();
  630. } else {
  631. cellEditor.cancelCellEditing();
  632. }
  633. }
  634. }
  635. /**
  636. * Closes a tab of the UpperNode with ID upperNodeID
  637. * @param upperNodeId
  638. */
  639. public abstract void closeUpperNodeTab(int upperNodeId);
  640. }