AbstractCanvas.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. void setEdgePictureAndHighlighting(AbstractCpsObject cps) {
  143. // node image
  144. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  145. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  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 = Util.loadImage(this, cps.getImage());
  188. }
  189. }
  190. }
  191. void drawMarker() {
  192. if (sx > x && sy > y) {
  193. g2.drawRect(x, y, sx - x, sy - y);
  194. } else if (sx < x && sy < y) {
  195. g2.drawRect(sx, sy, x - sx, y - sy);
  196. } else if (sx >= x) {
  197. g2.drawRect(x, sy, sx - x, y - sy);
  198. } else if (sy >= y) {
  199. g2.drawRect(sx, y, x - sx, sy - y);
  200. }
  201. }
  202. void showTooltip(Graphics g) {
  203. if (toolTip) {
  204. g2.setColor(new Color(255, 225, 150));
  205. g2.setStroke(new BasicStroke(1));
  206. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  207. // width
  208. // fixed x and y Position to the screen
  209. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2();
  210. int fixYPos = toolTipPos.y;
  211. if (fixXPos < 0) {
  212. fixXPos = 0;
  213. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  214. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  215. }
  216. if (fixYPos + 16 > this.getHeight()) {
  217. fixYPos -= (fixYPos + 16) - this.getHeight();
  218. }
  219. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  220. g2.setColor(Color.BLACK);
  221. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  222. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  223. }
  224. }
  225. void setConsoleTextAfterSelect(AbstractCpsObject cps) {
  226. if (model.getShowConsoleLog()) {
  227. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  228. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  229. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  230. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  231. }
  232. }
  233. void setRightClickMenu(MouseEvent e) {
  234. if (e.getButton() == MouseEvent.BUTTON3) {
  235. itemPaste.setEnabled(true);
  236. if (tempCps != null) {
  237. itemPaste.setEnabled(true);
  238. itemDelete.setEnabled(true);
  239. itemCut.setEnabled(true);
  240. itemCopy.setEnabled(true);
  241. if (tempCps != null) {
  242. itemGroup.setEnabled(true);
  243. itemTrack.setEnabled(true);
  244. itemUntrack.setEnabled(true);
  245. }
  246. if (tempCps instanceof CpsUpperNode)
  247. itemUngroup.setEnabled(true);
  248. else
  249. itemUngroup.setEnabled(false);
  250. if (model.getSelectedCpsObjects().size() == 0) {
  251. controller.addSelectedObject(tempCps);
  252. }
  253. }else{
  254. itemCut.setEnabled(false);
  255. itemCopy.setEnabled(false);
  256. itemGroup.setEnabled(false);
  257. itemUngroup.setEnabled(false);
  258. itemTrack.setEnabled(false);
  259. itemUntrack.setEnabled(false);
  260. if(edgeHighlight != null)
  261. {
  262. itemDelete.setEnabled(true);
  263. itemPaste.setEnabled(false);
  264. }
  265. else
  266. {
  267. itemDelete.setEnabled(false);
  268. itemPaste.setEnabled(true);
  269. }
  270. }
  271. mousePosition = this.getMousePosition();
  272. popmenu.show(e.getComponent(), e.getX(), e.getY());
  273. }
  274. }
  275. void markObjects() {
  276. if (doMark) {
  277. doMark = false;
  278. for (AbstractCpsObject cps : tempSelected) {
  279. if (!model.getSelectedCpsObjects().contains(cps)) {
  280. controller.addSelectedObject(cps);
  281. }
  282. }
  283. controller.getObjectsInDepth();
  284. tempSelected.clear();
  285. }
  286. }
  287. int[] determineMousePositionOnEdge(CpsEdge p) {
  288. int lx, ly, hx, hy;
  289. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  290. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  291. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  292. } else {
  293. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  294. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  295. }
  296. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  297. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  298. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  299. } else {
  300. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  301. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  302. }
  303. return new int[]{lx, ly, hx, hy};
  304. }
  305. /**
  306. * Checks if a double click was made.
  307. *
  308. * @return true if doublecklick, false if not
  309. */
  310. boolean doubleClick() {
  311. if (click) {
  312. click = false;
  313. return true;
  314. } else {
  315. click = true;
  316. java.util.Timer t = new java.util.Timer("doubleclickTimer", false);
  317. t.schedule(new TimerTask() {
  318. @Override
  319. public void run() {
  320. click = false;
  321. }
  322. }, 500);
  323. }
  324. return false;
  325. }
  326. boolean setToolTipInfoAndPosition(boolean on, AbstractCpsObject cps) {
  327. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  328. on = true;
  329. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  330. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  331. toolTipText = cps.getName() + ", " + cps.getId();
  332. }
  333. return on;
  334. }
  335. abstract void drawDeleteEdge();
  336. void triggerUpdateController() {
  337. updCon.paintProperties(tempCps);
  338. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  339. updCon.refreshTableProperties(model.getPropertyTable());
  340. }
  341. }