AbstractCanvas.java 17 KB

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