AbstractCanvas.java 22 KB

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