AbstractCanvas.java 20 KB

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