AbstractCanvas.java 15 KB

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