AbstractCanvas.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 java.awt.*;
  8. import java.awt.event.MouseEvent;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.TimerTask;
  13. /**
  14. * Collection of methods and values needed in both <code>MyCanvas</code> and
  15. * <code>UpperNodeCanvas</code>
  16. * <p>
  17. * Although Java works on references we chose to add explicit return values for
  18. * clearer code understanding in most cases
  19. *
  20. * @author: I. Dix
  21. */
  22. public abstract class AbstractCanvas extends JPanel {
  23. /**
  24. * Version
  25. */
  26. private static final long serialVersionUID = 1L;
  27. final JMenuItem itemDelete = new JMenuItem(Languages.getLanguage()[98]);
  28. final JMenuItem itemCut = new JMenuItem(Languages.getLanguage()[95]);
  29. final JMenuItem itemCopy = new JMenuItem(Languages.getLanguage()[96]);
  30. final JMenuItem itemPaste = new JMenuItem(Languages.getLanguage()[97]);
  31. final JMenuItem itemGroup = new JMenuItem(Languages.getLanguage()[99]);
  32. final JMenuItem itemUngroup = new JMenuItem(Languages.getLanguage()[100]);
  33. final JMenuItem itemTrack = new JMenuItem(Languages.getLanguage()[101]);
  34. final JMenuItem itemUntrack = new JMenuItem(Languages.getLanguage()[102]);
  35. final JMenuItem itemCreateTemplate = new JMenuItem(Languages.getLanguage()[Languages.right_click_create_template]);
  36. final int ANIMTIME = 500; // animation Time
  37. private final int animFPS = 60;
  38. final int animDelay = 1000 / animFPS; // animation Delay
  39. protected Model model;
  40. protected Control controller;
  41. protected int x = 0;
  42. protected int y = 0;
  43. // Selection
  44. AbstractCpsObject tempCps = null;
  45. UpdateController updCon;
  46. //Replacement
  47. /**
  48. * the CpsObject that might be replaced by drag&drop
  49. */
  50. protected AbstractCpsObject mayBeReplaced = null;
  51. // PopUpMenu
  52. JPopupMenu popmenu = new JPopupMenu();
  53. // Tooltip
  54. boolean toolTip; // Tooltip on or off
  55. Position toolTipPos = new Position(); // Tooltip Position
  56. String toolTipText = "";
  57. ArrayList<HolonElement> dataSelected = new ArrayList<>();
  58. ArrayList<AbstractCpsObject> tempSelected = new ArrayList<>();
  59. boolean[] showedInformation = new boolean[5];
  60. boolean dragging = false; // for dragging
  61. boolean dragged = false; // if an object/objects was/were dragged
  62. boolean drawEdge = false; // for drawing edges
  63. boolean doMark = false; // for double click
  64. CpsEdge edgeHighlight = null;
  65. Point mousePosition = new Point(); // Mouse Position when
  66. ArrayList<Position> savePos;
  67. // edge Object Start Point
  68. int cx, cy;
  69. int sx, sy; // Mark Coords
  70. Position unPos;
  71. // Animation
  72. Timer animT; // animation Timer
  73. int animDuration = ANIMTIME; // animation Duration
  74. int animSteps = animDuration / animDelay; // animation Steps;
  75. ArrayList<AbstractCpsObject> animCps = null;
  76. // Graphics
  77. Image img = null; // Contains the image to draw on the Canvas
  78. Graphics2D g2; // For Painting
  79. float scalediv20;
  80. // Mouse
  81. private boolean click = false;
  82. // ------------------------------------------ METHODS
  83. // ------------------------------------------
  84. String paintEdge(CpsEdge con, String maxCap) {
  85. if (con!=null && con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
  86. && con != edgeHighlight) {
  87. if (con.getConnected() == CpsEdge.CON_UPPER_NODE
  88. || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
  89. setEdgeState(con);
  90. } else {
  91. g2.setColor(Color.DARK_GRAY);
  92. g2.setStroke(new BasicStroke(2));
  93. }
  94. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  95. con.getB().getPosition().y);
  96. maxCap = setCapacityString(con, maxCap);
  97. if (showedInformation[0]) {
  98. if (con.getConnected() == CpsEdge.CON_UPPER_NODE
  99. || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
  100. g2.drawString(con.getFlow() + "/" + maxCap,
  101. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  102. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  103. } else {
  104. g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  105. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  106. }
  107. }
  108. }
  109. return maxCap;
  110. }
  111. void setEdgeState(CpsEdge con) {
  112. if (con.isWorking()) {
  113. g2.setColor(Color.GREEN);
  114. if (con.getCapacity() != CpsEdge.CAPACITY_INFINITE) {
  115. g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  116. }
  117. } else {
  118. g2.setColor(Color.RED);
  119. g2.setStroke(new BasicStroke(2));
  120. }
  121. }
  122. String setCapacityString(CpsEdge con, String maxCap) {
  123. if (con.getCapacity() == -1) {
  124. maxCap = Character.toString('\u221e');
  125. } else if (con.getCapacity() == -2) {
  126. maxCap = "???";
  127. } else {
  128. maxCap = String.valueOf(con.getCapacity());
  129. }
  130. return maxCap;
  131. }
  132. String drawEdgeLine(CpsEdge con, String maxCap) {
  133. if (con.getA().getId() == model.getSelectedObjectID() || model.getSelectedCpsObjects().contains(con.getA())
  134. || tempSelected.contains(con.getA()) || con.getB().getId() == model.getSelectedObjectID()
  135. || model.getSelectedCpsObjects().contains(con.getB())
  136. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  137. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  138. con.getB().getPosition().y);
  139. maxCap = setCapacityString(con, maxCap);
  140. if (showedInformation[0]) {
  141. if (con.getConnected() == CpsEdge.CON_UPPER_NODE
  142. || con.getConnected() == CpsEdge.CON_UPPER_NODE_AND_INSIDE) {
  143. g2.drawString(con.getFlow() + "/" + maxCap,
  144. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  145. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  146. } else {
  147. g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  148. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  149. }
  150. }
  151. }
  152. return maxCap;
  153. }
  154. /**
  155. * Paints the SupplyBar for the given cps object on the canvas
  156. *
  157. * @param g
  158. * Graphics used
  159. * @param cps
  160. * cpsObject which the supplyBar should be drawn for
  161. */
  162. protected void paintSupplyBar(Graphics g, AbstractCpsObject cps) {
  163. /**
  164. * draw and fill the supply Bar
  165. */
  166. if (model.getShowSupplyBars() && (cps instanceof HolonObject || cps instanceof HolonBattery))
  167. {
  168. // set Color & Percentage
  169. /**
  170. * percentage the SupplyBar is representing
  171. */
  172. float percentage = 0;
  173. /**
  174. * color of the SupplyBar
  175. */
  176. Color paintColor = Color.WHITE;
  177. if(cps instanceof HolonObject)
  178. {
  179. HolonObject hl = (HolonObject) cps;
  180. if (hl == null || !(hl.getState() == HolonObject.NOT_SUPPLIED
  181. || hl.getState() == HolonObject.PARTIALLY_SUPPLIED || hl.getState() == HolonObject.OVER_SUPPLIED)) {
  182. /**
  183. * don't show Bars for unsupplied oder fully supplied Objects
  184. */
  185. return;
  186. }
  187. /**
  188. * calculate Percentage & set Color
  189. */
  190. percentage = hl.getSuppliedPercentage();
  191. paintColor = hl.getColor();
  192. }
  193. else if (cps instanceof HolonBattery){
  194. HolonBattery hB = (HolonBattery) cps;
  195. if(hB == null || hB.getCapacity() == 0){
  196. return;
  197. }
  198. /**
  199. * get percentage filled
  200. */
  201. percentage = hB.getStateOfChargeAtTimeStep(model.getCurIteration()-1) / hB.getCapacity();
  202. /**
  203. * calculate the Color (Red->Yellow->Green)
  204. */
  205. Color color1 = Color.RED;
  206. Color color2 = Color.GREEN;
  207. //
  208. float colorPercentage;
  209. if(percentage < 0.5f){
  210. colorPercentage = percentage * 2;
  211. color1 = Color.RED;
  212. color2 = Color.YELLOW;
  213. }
  214. else {
  215. colorPercentage = (percentage - 0.5f) * 2;
  216. color1 = Color.YELLOW;
  217. color2 = Color.GREEN;
  218. }
  219. final int dRed = color2.getRed() - color1.getRed();
  220. final int dGreen = color2.getGreen() - color1.getGreen();
  221. final int dBlue = color2.getBlue() - color1.getBlue();
  222. int resultRed = color1.getRed() + (int)(colorPercentage * dRed);
  223. int resultGreen = color1.getGreen() + (int)(colorPercentage * dGreen);
  224. int resultBlue = color1.getBlue() + (int)( colorPercentage * dBlue);
  225. paintColor = new Color( resultRed,resultGreen,resultBlue);
  226. }
  227. /**
  228. * Starting position x of the bar
  229. */
  230. int barX = (int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20);
  231. /**
  232. * Starting position y of the bar
  233. */
  234. int barY = (int) (cps.getPosition().y - controller.getScaleDiv2() + controller.getScale() + 1);
  235. /**
  236. * if object should be replaced -> move bar below the ReplacementIndicator
  237. */
  238. if(mayBeReplaced==cps) barY += 6;
  239. /**
  240. * Width of the bar
  241. */
  242. int barWidth = (int) (controller.getScale() + ((scalediv20) * 2) - 1);
  243. /**
  244. * Height of the bar
  245. */
  246. int barHeight = (int) (controller.getScale() / 5);
  247. /**
  248. * draw Rectangle below the image
  249. */
  250. g2.setStroke(new BasicStroke(1));
  251. g2.drawRect(barX, barY, barWidth, barHeight);
  252. g2.setColor(paintColor);
  253. /** fill it accordingly if filled partially */
  254. if (percentage < 1)
  255. g2.fillRect(barX + 1, barY + 1, (int) ((barWidth - 1) * percentage), barHeight - 1);
  256. else /** over supplied / supplied bar should be fully filled */
  257. g2.fillRect(barX + 1, barY + 1, (int) (barWidth - 1), barHeight - 1);
  258. /** write percentage */
  259. if(percentage>1)
  260. g2.setColor(Color.WHITE);
  261. else
  262. g2.setColor(Color.BLACK);
  263. /** original font */
  264. Font oldFont = g2.getFont();
  265. g.setFont(new Font("TimesRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  266. String percentageString = (Math.round((percentage * 100))) + "%";
  267. int stringWidth = (int) g2.getFontMetrics().getStringBounds(percentageString, g2).getWidth();
  268. g2.drawString(percentageString, barX + barWidth / 2 + 1 - stringWidth / 2, barY + barHeight);
  269. /** recover Font and Color */
  270. g2.setFont(oldFont);
  271. g2.setColor(Color.BLACK);
  272. }
  273. }
  274. void setEdgePictureAndHighlighting(AbstractCpsObject cps) {
  275. // node image
  276. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  277. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  278. img = Util.loadImage(this, "/Images/node_selected.png");
  279. } else {
  280. if (cps instanceof HolonSwitch) {//TODO. What the hell are thes ecalls doing here?
  281. if (((HolonSwitch) cps).getState(model.getCurIteration())) {
  282. ((HolonSwitch) cps).setAutoState(true);
  283. } else {
  284. ((HolonSwitch) cps).setAutoState(false);
  285. }
  286. }
  287. // Highlighting
  288. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  289. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  290. g2.setColor(Color.BLUE);
  291. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
  292. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
  293. (int) (controller.getScale() + (scalediv20 * 2)),
  294. (int) (controller.getScale() + (scalediv20 * 2)));
  295. if (showedInformation[1] && cps instanceof HolonObject) {
  296. g2.setColor(Color.BLACK);
  297. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  298. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
  299. cps.getPosition().y - controller.getScaleDiv2() - 10);
  300. }else if (showedInformation[1] && cps instanceof HolonBattery)
  301. {
  302. g2.setColor(Color.BLACK);
  303. g2.drawString(((HolonBattery) cps).getCanvasBatteryString(model.getCurIteration()), cps.getPosition().x - controller.getScaleDiv2(),
  304. cps.getPosition().y - controller.getScaleDiv2() - 10);
  305. }
  306. } else if (cps instanceof HolonObject) {
  307. g2.setColor(((HolonObject) cps).getColor());
  308. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
  309. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
  310. (int) (controller.getScale() + (scalediv20 * 2)),
  311. (int) (controller.getScale() + (scalediv20 * 2)));
  312. if (showedInformation[1]) {
  313. g2.setColor(Color.BLACK);
  314. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  315. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
  316. cps.getPosition().y - controller.getScaleDiv2() - 10);
  317. }
  318. }else if (cps instanceof HolonBattery) {
  319. if (showedInformation[1]) {
  320. g2.setColor(Color.BLACK);
  321. g2.drawString(((HolonBattery) cps).getCanvasBatteryString(model.getCurIteration()), cps.getPosition().x - controller.getScaleDiv2(),
  322. cps.getPosition().y - controller.getScaleDiv2() - 10);
  323. }
  324. }
  325. // draw image
  326. File checkPath = new File(cps.getImage());
  327. if (checkPath.exists()) {
  328. img = new ImageIcon(cps.getImage()).getImage();
  329. } else {
  330. img = Util.loadImage(this, cps.getImage());
  331. }
  332. }
  333. }
  334. void drawMarker() {
  335. if (sx > x && sy > y) {
  336. g2.drawRect(x, y, sx - x, sy - y);
  337. } else if (sx < x && sy < y) {
  338. g2.drawRect(sx, sy, x - sx, y - sy);
  339. } else if (sx >= x) {
  340. g2.drawRect(x, sy, sx - x, y - sy);
  341. } else if (sy >= y) {
  342. g2.drawRect(sx, y, x - sx, sy - y);
  343. }
  344. }
  345. void showTooltip(Graphics g) {
  346. if (toolTip) {
  347. g2.setColor(new Color(255, 225, 150));
  348. g2.setStroke(new BasicStroke(1));
  349. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  350. // width
  351. // fixed x and y Position to the screen
  352. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2();
  353. int fixYPos = toolTipPos.y;
  354. if (fixXPos < 0) {
  355. fixXPos = 0;
  356. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  357. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  358. }
  359. if (fixYPos + 16 > this.getHeight()) {
  360. fixYPos -= (fixYPos + 16) - this.getHeight();
  361. }
  362. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  363. g2.setColor(Color.BLACK);
  364. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  365. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  366. }
  367. }
  368. void setConsoleTextAfterSelect(AbstractCpsObject cps) {
  369. if (model.getShowConsoleLog()) {
  370. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  371. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  372. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  373. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  374. }
  375. }
  376. void setRightClickMenu(MouseEvent e) {
  377. if (e.getButton() == MouseEvent.BUTTON3) {
  378. itemPaste.setEnabled(true);
  379. if (tempCps != null) {
  380. itemPaste.setEnabled(true);
  381. itemDelete.setEnabled(true);
  382. itemCut.setEnabled(true);
  383. itemCopy.setEnabled(true);
  384. // tracking
  385. if (tempCps != null) {
  386. itemGroup.setEnabled(true);
  387. itemTrack.setEnabled(true);
  388. itemUntrack.setEnabled(true);
  389. }
  390. // ungrouping
  391. if (tempCps instanceof CpsUpperNode)
  392. itemUngroup.setEnabled(true);
  393. else
  394. itemUngroup.setEnabled(false);
  395. if (model.getSelectedCpsObjects().size() == 0) {
  396. controller.addSelectedObject(tempCps);
  397. }
  398. if (tempCps instanceof HolonObject) {
  399. itemCreateTemplate.setEnabled(true);
  400. } else {
  401. itemCreateTemplate.setEnabled(false);
  402. }
  403. } else {
  404. itemCut.setEnabled(false);
  405. itemCopy.setEnabled(false);
  406. itemGroup.setEnabled(false);
  407. itemUngroup.setEnabled(false);
  408. itemTrack.setEnabled(false);
  409. itemUntrack.setEnabled(false);
  410. itemCreateTemplate.setEnabled(false);
  411. if (edgeHighlight != null) {
  412. itemDelete.setEnabled(true);
  413. itemPaste.setEnabled(false);
  414. } else {
  415. itemDelete.setEnabled(false);
  416. itemPaste.setEnabled(true);
  417. }
  418. }
  419. mousePosition = this.getMousePosition();
  420. popmenu.show(e.getComponent(), e.getX(), e.getY());
  421. }
  422. }
  423. void markObjects() {
  424. if (doMark) {
  425. doMark = false;
  426. for (AbstractCpsObject cps : tempSelected) {
  427. if (!model.getSelectedCpsObjects().contains(cps)) {
  428. controller.addSelectedObject(cps);
  429. }
  430. }
  431. controller.getObjectsInDepth();
  432. tempSelected.clear();
  433. }
  434. }
  435. int[] determineMousePositionOnEdge(CpsEdge p) {
  436. int lx, ly, hx, hy;
  437. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  438. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  439. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  440. } else {
  441. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  442. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  443. }
  444. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  445. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  446. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  447. } else {
  448. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  449. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  450. }
  451. return new int[] { lx, ly, hx, hy };
  452. }
  453. /**
  454. * Checks if a double click was made.
  455. *
  456. * @return true if doublecklick, false if not
  457. */
  458. boolean doubleClick() {
  459. if (click) {
  460. click = false;
  461. return true;
  462. } else {
  463. click = true;
  464. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  465. t.schedule(new TimerTask() {
  466. @Override
  467. public void run() {
  468. click = false;
  469. }
  470. }, 500);
  471. }
  472. return false;
  473. }
  474. boolean setToolTipInfoAndPosition(boolean on, AbstractCpsObject cps) {
  475. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  476. on = true;
  477. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  478. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  479. toolTipText = cps.getName() + ", " + cps.getId();
  480. }
  481. return on;
  482. }
  483. abstract void drawDeleteEdge();
  484. void triggerUpdateController() {
  485. updCon.paintProperties(tempCps);
  486. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  487. updCon.refreshTableProperties(model.getPropertyTable());
  488. }
  489. /**
  490. * Checks if {@code draggedCps} or a new cpsObject at Position (x,y) could replace exactly one object
  491. * in {@code objects}.
  492. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  493. * @param objects list of objects that could be replaced
  494. * @param draggedCps Object that might replace
  495. * @param x Position of the objects that might replace
  496. * @param y Position of the objects that might replace
  497. * @return true if exactly one Object could be replaced
  498. */
  499. protected boolean checkForReplacement(ArrayList<AbstractCpsObject> objects, AbstractCpsObject draggedCps, int x, int y){
  500. /** distance treshold for replacement */
  501. int treshhold = controller.getScale()/2;
  502. /** number of Objects that might be replaced (should be 1) */
  503. int replaceCounter = 0;
  504. /** last object that could be replaced */
  505. AbstractCpsObject toBeReplaced = null;
  506. /** Position of object that might be replaced */
  507. Position p;
  508. /** for each cps on Canvas */
  509. if(draggedCps == null || !(draggedCps instanceof CpsNode) && !(draggedCps instanceof CpsNode)){
  510. for (AbstractCpsObject cps : objects){
  511. /** same object -> ignore */
  512. if(cps == draggedCps)continue;
  513. /** set Position of object that might be replaced */
  514. p = cps.getPosition();
  515. /** if near enough */
  516. if(Math.abs(x-p.x)<treshhold && Math.abs(y-p.y)<treshhold){
  517. replaceCounter++;
  518. toBeReplaced = cps;
  519. /**
  520. * if too many Objects could be replaced:
  521. * stop searching, because it would not be clear which one should
  522. * be replaced
  523. */
  524. if(replaceCounter>1)break;
  525. }
  526. }
  527. }
  528. /**
  529. * return true if exactly one obect would be replaced
  530. */
  531. if( replaceCounter == 1 && toBeReplaced != null){
  532. mayBeReplaced = toBeReplaced;
  533. return true;
  534. }else{
  535. mayBeReplaced = null;
  536. return false;
  537. }
  538. }
  539. /**
  540. * Checks if an inserted new Object could replace exactly one object on the canvas.
  541. * Saves the object that would be replaced in {@link AbstractCanvas}.{@code MayBeReplaced}
  542. * @param x Position of the objects that might replace
  543. * @param y Position of the objects that might replace
  544. * @return true if exactly one Object could be replaced
  545. */
  546. public abstract boolean checkForReplacement(int x, int y);
  547. /**
  548. * highlights the object that mayBeReplaced
  549. * @param g2
  550. */
  551. protected void highlightMayBeReplaced(Graphics2D g2) {
  552. if(mayBeReplaced != null){
  553. g2.setColor(Color.RED);
  554. g2.fillRect(
  555. (int) (mayBeReplaced.getPosition().x
  556. - controller.getScaleDiv2() - (scalediv20 + 5)),
  557. (int) (mayBeReplaced.getPosition().y
  558. - controller.getScaleDiv2() - (scalediv20 + 5)),
  559. (int) (controller.getScale() + ((scalediv20 + 5) * 2)),
  560. (int) (controller.getScale() + ((scalediv20 + 5) * 2)));
  561. }
  562. }
  563. /**
  564. * Align alle Objects on the Canvas to a Grid with objects every 10 pixels
  565. */
  566. public abstract void tryToAlignObjects();
  567. /**
  568. * Aligns the Object the a grid
  569. * @param cps Object that should be aligned
  570. * @param distance distance between the AlignmentGrid Lines. (objects every 'distance' pixels
  571. */
  572. protected void align(AbstractCpsObject cps, int distance) {
  573. /** Position of the AbstractCpsObject which should be aligned */
  574. Position p = cps.getPosition();
  575. //calculate how many pixels the cps should be decreased to align
  576. /** x offset relative to a grid with lines every distance pixels */
  577. int x_off = cps.getPosition().x % distance;
  578. /** y offset relative to a grid with lines every distance pixels */
  579. int y_off = cps.getPosition().y % distance;
  580. //align to the other Line, if it is nearer
  581. if(x_off > distance/2)
  582. x_off -= distance;
  583. if(y_off > distance/2)
  584. y_off -= distance;
  585. /** set new Position */
  586. cps.setPosition(p.x-x_off, p.y-y_off);
  587. }
  588. }