AbstractCanvas.java 17 KB

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