AbstractCanvas.java 23 KB

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