AbstractCanvas.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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).getEnergyAtTimeStep(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).getEnergyAtTimeStep(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 setRightClickMenu(MouseEvent e) {
  386. if (e.getButton() == MouseEvent.BUTTON3) {
  387. itemPaste.setEnabled(true);
  388. if (tempCps != null) {
  389. itemPaste.setEnabled(true);
  390. itemDelete.setEnabled(true);
  391. itemCut.setEnabled(true);
  392. itemCopy.setEnabled(true);
  393. itemAlign.setEnabled(true);
  394. // tracking
  395. if (tempCps != null) {
  396. itemGroup.setEnabled(true);
  397. itemTrack.setEnabled(true);
  398. itemUntrack.setEnabled(true);
  399. }
  400. // ungrouping
  401. if (tempCps instanceof CpsUpperNode)
  402. itemUngroup.setEnabled(true);
  403. else
  404. itemUngroup.setEnabled(false);
  405. if (model.getSelectedCpsObjects().size() == 0) {
  406. controller.addSelectedObject(tempCps);
  407. }
  408. if (tempCps instanceof HolonObject) {
  409. itemCreateTemplate.setEnabled(true);
  410. } else {
  411. itemCreateTemplate.setEnabled(false);
  412. }
  413. } else {
  414. itemAlign.setEnabled(false);
  415. itemCut.setEnabled(false);
  416. itemCopy.setEnabled(false);
  417. itemGroup.setEnabled(false);
  418. itemUngroup.setEnabled(false);
  419. itemTrack.setEnabled(false);
  420. itemUntrack.setEnabled(false);
  421. itemCreateTemplate.setEnabled(false);
  422. if (edgeHighlight != null) {
  423. itemDelete.setEnabled(true);
  424. itemPaste.setEnabled(false);
  425. } else {
  426. itemDelete.setEnabled(false);
  427. itemPaste.setEnabled(true);
  428. }
  429. }
  430. mousePosition = this.getMousePosition();
  431. popmenu.show(e.getComponent(), e.getX(), e.getY());
  432. }
  433. }
  434. void markObjects() {
  435. if (doMark) {
  436. doMark = false;
  437. for (AbstractCpsObject cps : tempSelected) {
  438. if (!model.getSelectedCpsObjects().contains(cps)) {
  439. controller.addSelectedObject(cps);
  440. }
  441. }
  442. controller.getObjectsInDepth();
  443. tempSelected.clear();
  444. }
  445. }
  446. int[] determineMousePositionOnEdge(CpsEdge p) {
  447. int lx, ly, hx, hy;
  448. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  449. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  450. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  451. } else {
  452. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  453. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  454. }
  455. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  456. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  457. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  458. } else {
  459. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  460. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  461. }
  462. return new int[] { lx, ly, hx, hy };
  463. }
  464. /**
  465. * Checks if a double click was made.
  466. *
  467. * @return true if doublecklick, false if not
  468. */
  469. boolean doubleClick() {
  470. if (click) {
  471. click = false;
  472. return true;
  473. } else {
  474. click = true;
  475. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  476. t.schedule(new TimerTask() {
  477. @Override
  478. public void run() {
  479. click = false;
  480. }
  481. }, 500);
  482. }
  483. return false;
  484. }
  485. boolean setToolTipInfoAndPosition(boolean on, AbstractCpsObject cps) {
  486. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  487. on = true;
  488. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  489. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  490. toolTipText = cps.getName() + ", " + cps.getId();
  491. }
  492. return on;
  493. }
  494. abstract void drawDeleteEdge();
  495. void triggerUpdateController() {
  496. updCon.paintProperties(tempCps);
  497. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  498. updCon.refreshTableProperties(model.getPropertyTable());
  499. }
  500. /**
  501. * Checks if {@code draggedCps} or a new cpsObject at Position (x,y) could replace exactly one object
  502. * in {@code objects}.
  503. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  504. * @param objects list of objects that could be replaced
  505. * @param draggedCps Object that might replace
  506. * @param x Position of the objects that might replace
  507. * @param y Position of the objects that might replace
  508. * @return true if exactly one Object could be replaced
  509. */
  510. protected boolean checkForReplacement(ArrayList<AbstractCpsObject> objects, AbstractCpsObject draggedCps, int x, int y){
  511. /** distance treshold for replacement */
  512. int treshhold = controller.getScale()/2;
  513. /** number of Objects that might be replaced (should be 1) */
  514. int replaceCounter = 0;
  515. /** last object that could be replaced */
  516. AbstractCpsObject toBeReplaced = null;
  517. /** Position of object that might be replaced */
  518. Position p;
  519. /** for each cps on Canvas */
  520. if(draggedCps == null || !(draggedCps instanceof CpsNode) && !(draggedCps instanceof CpsNode)){
  521. for (AbstractCpsObject cps : objects){
  522. /** same object -> ignore */
  523. if(cps == draggedCps)continue;
  524. /** set Position of object that might be replaced */
  525. p = cps.getPosition();
  526. /** if near enough */
  527. if(Math.abs(x-p.x)<treshhold && Math.abs(y-p.y)<treshhold){
  528. replaceCounter++;
  529. toBeReplaced = cps;
  530. /**
  531. * if too many Objects could be replaced:
  532. * stop searching, because it would not be clear which one should
  533. * be replaced
  534. */
  535. if(replaceCounter>1)break;
  536. }
  537. }
  538. }
  539. /**
  540. * return true if exactly one obect would be replaced
  541. */
  542. if( replaceCounter == 1 && toBeReplaced != null){
  543. mayBeReplaced = toBeReplaced;
  544. return true;
  545. }else{
  546. mayBeReplaced = null;
  547. return false;
  548. }
  549. }
  550. /**
  551. * Checks if an inserted new Object could replace exactly one object on the canvas.
  552. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  553. * @param x Position of the objects that might replace
  554. * @param y Position of the objects that might replace
  555. * @return true if exactly one Object could be replaced
  556. */
  557. public abstract boolean checkForReplacement(int x, int y);
  558. /**
  559. * highlights the object that mayBeReplaced
  560. * @param g2
  561. */
  562. protected void highlightMayBeReplaced(Graphics2D g2) {
  563. if(mayBeReplaced != null){
  564. g2.setColor(Color.RED);
  565. g2.fillRect(
  566. (int) (mayBeReplaced.getPosition().x
  567. - controller.getScaleDiv2() - (scalediv20 + 3)),
  568. (int) (mayBeReplaced.getPosition().y
  569. - controller.getScaleDiv2() - (scalediv20 + 3)),
  570. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  571. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  572. }
  573. }
  574. /**
  575. * Align alle Objects on the Canvas to a Grid with objects every 10 pixels
  576. */
  577. public abstract void tryToAlignObjects();
  578. /**
  579. * Aligns the Object the a grid
  580. * @param cps Object that should be aligned
  581. * @param distance distance between the AlignmentGrid Lines. (objects every 'distance' pixels
  582. */
  583. protected void align(AbstractCpsObject cps, int distance) {
  584. /** Position of the AbstractCpsObject which should be aligned */
  585. Position p = cps.getPosition();
  586. //calculate how many pixels the cps should be decreased to align
  587. /** x offset relative to a grid with lines every distance pixels */
  588. int x_off = cps.getPosition().x % distance;
  589. /** y offset relative to a grid with lines every distance pixels */
  590. int y_off = cps.getPosition().y % distance;
  591. //align to the other Line, if it is nearer
  592. if(x_off > distance/2)
  593. x_off -= distance;
  594. if(y_off > distance/2)
  595. y_off -= distance;
  596. /** set new Position */
  597. cps.setPosition(p.x-x_off, p.y-y_off);
  598. }
  599. /**
  600. * Stops Editing in HolonElementTable and PropertyTable
  601. */
  602. protected void stopEditing() {
  603. /**
  604. * Stop Editing, if mouse exits the Table
  605. */
  606. JTable holElem = model.getTableHolonElement();
  607. CellEditor cellEditor = holElem.getCellEditor();
  608. if (cellEditor != null) {
  609. if (cellEditor.getCellEditorValue() != null) {
  610. /** TODO: Maybe try to save current Data */
  611. cellEditor.stopCellEditing();
  612. } else {
  613. cellEditor.cancelCellEditing();
  614. }
  615. }
  616. JTable propertys = model.getTableProperties();
  617. cellEditor = propertys.getCellEditor();
  618. if (cellEditor != null) {
  619. if (cellEditor.getCellEditorValue() != null) {
  620. /** TODO: Maybe try to save current Data */
  621. cellEditor.stopCellEditing();
  622. } else {
  623. cellEditor.cancelCellEditing();
  624. }
  625. }
  626. }
  627. /**
  628. * Closes a tab of the UpperNode with ID upperNodeID
  629. * @param upperNodeId
  630. */
  631. public abstract void closeUpperNodeTab(int upperNodeId);
  632. }