MyCanvas.java 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9. import java.awt.RenderingHints;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.awt.geom.Line2D;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Timer;
  20. import java.util.TimerTask;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JMenuItem;
  23. import javax.swing.JPanel;
  24. import javax.swing.JPopupMenu;
  25. import javax.swing.JScrollPane;
  26. import javax.swing.JTabbedPane;
  27. import javax.swing.JToolTip;
  28. import classes.CpsEdge;
  29. import classes.CpsNode;
  30. import classes.CpsUpperNode;
  31. import classes.AbstractCpsObject;
  32. import classes.HolonElement;
  33. import classes.HolonObject;
  34. import classes.HolonSwitch;
  35. import classes.Position;
  36. import classes.SubNet;
  37. import ui.controller.Control;
  38. import ui.model.Model;
  39. /**
  40. * This Class is the Canvas. All Objects will be visualized here
  41. *
  42. * @author Gruppe14
  43. */
  44. public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
  45. private static final long serialVersionUID = 1L;
  46. private Image img = null; // Contains the image to draw on MyCanvas
  47. private int x = 0;
  48. private int y = 0;
  49. // edge Object Start Point
  50. private Model model;
  51. private final Control controller;
  52. Graphics2D g2; // For Painting
  53. private int cx, cy;
  54. private int sx, sy; // Mark Coords
  55. private float scalediv20;
  56. private Position unPos;
  57. private ArrayList<Position> savePos;
  58. ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
  59. ArrayList<AbstractCpsObject> tempSelected = new ArrayList<AbstractCpsObject>();
  60. private boolean[] showedInformation = new boolean[4];
  61. private boolean dragging = false; // for dragging
  62. private boolean dragged = false; // if an object/objects was/were dragged
  63. private boolean drawEdge = false; // for drawing edges
  64. public boolean click = false; // for double click
  65. private boolean doMark = false; // for double click
  66. public AbstractCpsObject tempCps = null;
  67. private CpsEdge edgeHighlight = null;
  68. // PopUpMenu
  69. private JPopupMenu popmenu = new JPopupMenu();
  70. private JMenuItem itemDelete = new JMenuItem(Languages.getLanguage()[98]);
  71. private JMenuItem itemCut = new JMenuItem(Languages.getLanguage()[95]);
  72. private JMenuItem itemCopy = new JMenuItem(Languages.getLanguage()[96]);
  73. public JMenuItem itemPaste = new JMenuItem(Languages.getLanguage()[97]);
  74. public JMenuItem itemGroup = new JMenuItem(Languages.getLanguage()[99]);
  75. public JMenuItem itemUngroup = new JMenuItem(Languages.getLanguage()[100]);
  76. public JMenuItem itemTrack = new JMenuItem(Languages.getLanguage()[101]);
  77. public JMenuItem itemUntrack = new JMenuItem(Languages.getLanguage()[102]);
  78. private JToolTip objectTT = new JToolTip();
  79. private Point mousePosition = new Point(); // Mouse Position when
  80. // rightclicked
  81. javax.swing.Timer animT; // animation Timer
  82. private final int ANIMTIME = 500; // animation Time
  83. private ArrayList<AbstractCpsObject> animCps = null;
  84. private int animFPS = 60;
  85. private int animDuration = ANIMTIME; // animation Duration
  86. private int animDelay = 1000 / animFPS; // animation Delay
  87. private int animSteps = animDuration / animDelay; // animation Steps;
  88. private long start = 0; // for time
  89. private long elapsedTime = 0; // outprint
  90. // contains the value of the Capacity for new created Edges
  91. /**
  92. * Constructor.
  93. *
  94. * @param mod
  95. * the Model
  96. * @param control
  97. * the Controller
  98. */
  99. public MyCanvas(Model mod, Control control) {
  100. this.add(objectTT);
  101. objectTT.setVisible(false);
  102. this.controller = control;
  103. this.model = mod;
  104. scalediv20 = model.getScale() / 20;
  105. showedInformation[0] = true;
  106. showedInformation[1] = true;
  107. showedInformation[3] = false;
  108. control.setMaxCapacity(10000);
  109. popmenu.add(itemCut);
  110. popmenu.add(itemCopy);
  111. popmenu.add(itemPaste);
  112. popmenu.add(itemDelete);
  113. popmenu.addSeparator();
  114. popmenu.add(itemGroup);
  115. popmenu.add(itemUngroup);
  116. popmenu.add(itemTrack);
  117. popmenu.add(itemUntrack);
  118. itemDelete.setEnabled(false);
  119. itemCut.setEnabled(false);
  120. itemCopy.setEnabled(false);
  121. itemPaste.setEnabled(false);
  122. itemGroup.setEnabled(false);
  123. itemUngroup.setEnabled(false);
  124. itemTrack.setEnabled(false);
  125. itemUntrack.setEnabled(false);
  126. itemCut.setText(Languages.getLanguage()[95]);
  127. itemGroup.addActionListener(new ActionListener() {
  128. @Override
  129. public void actionPerformed(ActionEvent e) {
  130. // calculate uppernode pos (taken from the controller)
  131. unPos = new Position(0, 0);
  132. animCps = new ArrayList<>();
  133. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  134. animCps.add(cps); // add to animation Cps ArrayList
  135. unPos.x += cps.getPosition().x;
  136. unPos.y += cps.getPosition().y;
  137. }
  138. unPos.x /= animCps.size();
  139. unPos.y /= animCps.size();
  140. // save old Position
  141. savePos = new ArrayList<>();
  142. for (int i = 0; i < animCps.size(); i++) {
  143. savePos.add(new Position(0, 0));
  144. savePos.get(i).x = animCps.get(i).getPosition().x;
  145. savePos.get(i).y = animCps.get(i).getPosition().y;
  146. }
  147. animT = new javax.swing.Timer(animDelay, new ActionListener() {
  148. @Override
  149. public void actionPerformed(ActionEvent e) {
  150. if (animDuration - animDelay > 0 && animCps.size() > 1) {
  151. for (int i = 0; i < animCps.size(); i++) {
  152. double x1 = animCps.get(i).getPosition().x - unPos.x;
  153. double y1 = animCps.get(i).getPosition().y - unPos.y;
  154. animCps.get(i).getPosition().x -= x1 / animSteps;
  155. animCps.get(i).getPosition().y -= y1 / animSteps;
  156. }
  157. repaint();
  158. animDuration -= animDelay;
  159. animSteps--;
  160. } else {
  161. animDuration = ANIMTIME;
  162. animSteps = animDuration / animDelay;
  163. animT.stop();
  164. for (int i = 0; i < animCps.size(); i++) {
  165. animCps.get(i).getPosition().x = savePos.get(i).x;
  166. animCps.get(i).getPosition().y = savePos.get(i).y;
  167. }
  168. controller.addUpperNode("NodeOfNode", null, animCps);
  169. controller.calculateStateForCurrentTimeStep();
  170. repaint();
  171. elapsedTime = System.currentTimeMillis() - start;
  172. controller.addTextToConsole(elapsedTime + "");
  173. }
  174. }
  175. });
  176. start = System.currentTimeMillis();
  177. animT.start();
  178. }
  179. });
  180. itemUngroup.addActionListener(new ActionListener() {
  181. @Override
  182. public void actionPerformed(ActionEvent e) {
  183. // save old Position
  184. JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
  185. for (int i = 3; i < tabbedPane.getTabCount(); i++) {
  186. if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  187. .getComponent(0)).upperNode.getID() == ((CpsUpperNode) tempCps).getID()) {
  188. tabbedPane.remove(i);
  189. break;
  190. }
  191. }
  192. savePos = new ArrayList<>();
  193. animCps = ((CpsUpperNode) tempCps).getNodes();
  194. controller.delUpperNode((CpsUpperNode) tempCps, null);
  195. for (int i = 0; i < animCps.size(); i++) {
  196. savePos.add(new Position(0, 0));
  197. savePos.get(i).x = animCps.get(i).getPosition().x;
  198. savePos.get(i).y = animCps.get(i).getPosition().y;
  199. }
  200. for (AbstractCpsObject cps : animCps) {
  201. int x = ((CpsUpperNode) tempCps).getPosition().x;
  202. int y = ((CpsUpperNode) tempCps).getPosition().y;
  203. cps.setPosition(new Position(x, y));
  204. }
  205. animT = new javax.swing.Timer(animDelay, new ActionListener() {
  206. @Override
  207. public void actionPerformed(ActionEvent e) {
  208. if (animDuration - animDelay >= 0) {
  209. for (int i = 0; i < animCps.size(); i++) {
  210. double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
  211. double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
  212. animCps.get(i).getPosition().x -= x1 / animSteps;
  213. animCps.get(i).getPosition().y -= y1 / animSteps;
  214. }
  215. repaint();
  216. animDuration -= animDelay;
  217. animSteps--;
  218. } else {
  219. animDuration = ANIMTIME;
  220. animSteps = animDuration / animDelay;
  221. animT.stop();
  222. for (int i = 0; i < animCps.size(); i++) {
  223. animCps.get(i).getPosition().x = savePos.get(i).x;
  224. animCps.get(i).getPosition().y = savePos.get(i).y;
  225. }
  226. controller.calculateStateForCurrentTimeStep();
  227. repaint();
  228. elapsedTime = System.currentTimeMillis() - start;
  229. controller.addTextToConsole(elapsedTime + "");
  230. }
  231. }
  232. });
  233. start = System.currentTimeMillis();
  234. animT.start();
  235. }
  236. });
  237. // adds the selected object(s) to the statistic panel
  238. itemTrack.addActionListener(new ActionListener() {
  239. @Override
  240. public void actionPerformed(ActionEvent e) {
  241. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  242. boolean found = false;
  243. if (controller.getTrackingObj() != null) {
  244. if (controller.getTrackingObj().contains(o)) {
  245. found = true;
  246. }
  247. }
  248. if (!found) {
  249. controller.addTrackingObj(o);
  250. if (o instanceof HolonObject) {
  251. ((HolonObject) o).updateTrackingInfo();
  252. }
  253. }
  254. controller.addTextToConsole("Tracking: ", Color.BLACK, 12, false, false, false);
  255. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  256. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  257. controller.addTextToConsole("" + o.getID(), Color.RED, 12, true, false, true);
  258. }
  259. }
  260. });
  261. itemUntrack.addActionListener(new ActionListener() {
  262. @Override
  263. public void actionPerformed(ActionEvent e) {
  264. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  265. if (o instanceof HolonObject) {
  266. boolean found = false;
  267. if (controller.getTrackingObj() != null) {
  268. for (AbstractCpsObject obj : controller.getTrackingObj()) {
  269. if (obj instanceof HolonObject) {
  270. if (obj.getID() == o.getID()) {
  271. found = true;
  272. }
  273. }
  274. }
  275. }
  276. if (found) {
  277. // Removed from tracking array and tracking
  278. // information reseted
  279. controller.removeTrackingObj((HolonObject) o);
  280. ((HolonObject) o).setTrackingProd(new float[100]);
  281. ((HolonObject) o).setTrackingCons(new float[100]);
  282. }
  283. controller.addTextToConsole("Untracking: ", Color.BLACK, 12, false, false, false);
  284. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  285. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  286. controller.addTextToConsole("" + o.getID(), Color.RED, 12, true, false, true);
  287. }
  288. }
  289. }
  290. });
  291. itemDelete.addActionListener(new ActionListener() {
  292. @Override
  293. public void actionPerformed(ActionEvent e) {
  294. // Remove the selected Object objects
  295. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  296. controller.delCanvasObject(cps);
  297. controller.removeTrackingObj(cps);
  298. // Remove UpperNodeTab if UpperNode deleted
  299. if (cps instanceof CpsUpperNode) {
  300. JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
  301. for (int i = 3; i < tabbedPane.getTabCount(); i++) {
  302. if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  303. .getComponent(0)).upperNode.getID() == cps.getID()) {
  304. tabbedPane.remove(i);
  305. i = tabbedPane.getTabCount();
  306. }
  307. }
  308. }
  309. }
  310. model.getSelectedCpsObjects().clear();
  311. tempCps = null;
  312. repaint();
  313. }
  314. });
  315. itemCut.addActionListener(new ActionListener() {
  316. @Override
  317. public void actionPerformed(ActionEvent e) {
  318. controller.cutObjects();
  319. itemPaste.setEnabled(true);
  320. repaint();
  321. }
  322. });
  323. itemCopy.addActionListener(new ActionListener() {
  324. @Override
  325. public void actionPerformed(ActionEvent e) {
  326. controller.copyObjects();
  327. itemPaste.setEnabled(true);
  328. repaint();
  329. }
  330. });
  331. itemPaste.addActionListener(new ActionListener() {
  332. @Override
  333. public void actionPerformed(ActionEvent e) {
  334. controller.pasteObjects(mousePosition);
  335. repaint();
  336. }
  337. });
  338. this.addMouseListener(this);
  339. this.addMouseMotionListener(this);
  340. }
  341. /**
  342. * Paints all Components on the Canvas.
  343. *
  344. * @param g
  345. * Graphics
  346. */
  347. public void paintComponent(Graphics g) {
  348. String maxCap;
  349. super.paintComponent(g);
  350. // Rendering
  351. g2 = (Graphics2D) g;
  352. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  353. g2.setRenderingHints(rh);
  354. // Test SubNet Coloring
  355. int i = 0;
  356. for (SubNet s : controller.getSimManager().getSubNets()) {
  357. if (model.getSubNetColors().size() - 1 < i) {
  358. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  359. (int) (Math.random() * 255)));
  360. }
  361. if (showedInformation[3]) {
  362. for (HolonObject cps : s.getObjects()) {
  363. cps.setBorderColor(model.getSubNetColors().get(i));
  364. }
  365. }
  366. i++;
  367. }
  368. // drawEdges that is being dragged
  369. if (drawEdge) {
  370. g2.setColor(Color.BLACK);
  371. g2.setStroke(new BasicStroke(2));
  372. g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
  373. tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
  374. }
  375. for (CpsEdge con : model.getEdgesOnCanvas()) {
  376. if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID()
  377. && con != edgeHighlight) {
  378. if (con.getConnected() == 0) {
  379. if (con.getState()) {
  380. g2.setColor(Color.GREEN);
  381. if (con.getCapacity() != -1) {
  382. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  383. }
  384. } else {
  385. g2.setColor(Color.RED);
  386. g2.setStroke(new BasicStroke(2));
  387. }
  388. } else {
  389. g2.setColor(Color.DARK_GRAY);
  390. g2.setStroke(new BasicStroke(2));
  391. }
  392. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  393. con.getA().getPosition().y + controller.getScaleDiv2(),
  394. con.getB().getPosition().x + controller.getScaleDiv2(),
  395. con.getB().getPosition().y + controller.getScaleDiv2());
  396. if (con.getCapacity() == -1) {
  397. maxCap = Character.toString('\u221e');
  398. } else if (con.getCapacity() == -2) {
  399. maxCap = "???";
  400. } else {
  401. maxCap = String.valueOf(con.getCapacity());
  402. }
  403. if (showedInformation[0]) {
  404. if (con.getConnected() == 0 || con.getConnected() == 1) {
  405. g2.drawString(con.getFlow() + "/" + maxCap,
  406. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  407. + controller.getScaleDiv2(),
  408. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  409. + controller.getScaleDiv2());
  410. } else {
  411. g2.drawString("not connected",
  412. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  413. + controller.getScaleDiv2(),
  414. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  415. + controller.getScaleDiv2());
  416. }
  417. }
  418. }
  419. }
  420. // Highlighted Edge
  421. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  422. g2.setColor(Color.BLUE);
  423. for (CpsEdge con : model.getEdgesOnCanvas()) {
  424. if (con.getFlow() <= con.getCapacity()) {
  425. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  426. } else {
  427. g2.setStroke(new BasicStroke(2));
  428. }
  429. if (con.getA().getID() == model.getSelectedObjectID()
  430. || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
  431. || con.getB().getID() == model.getSelectedObjectID()
  432. || model.getSelectedCpsObjects().contains(con.getB())
  433. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  434. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  435. con.getA().getPosition().y + controller.getScaleDiv2(),
  436. con.getB().getPosition().x + controller.getScaleDiv2(),
  437. con.getB().getPosition().y + controller.getScaleDiv2());
  438. if (con.getCapacity() == -1) {
  439. maxCap = Character.toString('\u221e');
  440. } else if (con.getCapacity() == -2) {
  441. maxCap = "???";
  442. } else {
  443. maxCap = String.valueOf(con.getCapacity());
  444. }
  445. if (showedInformation[0]) {
  446. if (con.getConnected() == 0 || con.getConnected() == 1) {
  447. g2.drawString(con.getFlow() + "/" + maxCap,
  448. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  449. + controller.getScaleDiv2(),
  450. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  451. + controller.getScaleDiv2());
  452. } else {
  453. g2.drawString("not connected",
  454. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  455. + controller.getScaleDiv2(),
  456. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  457. + controller.getScaleDiv2());
  458. }
  459. }
  460. }
  461. }
  462. } else if (edgeHighlight != null) {
  463. g2.setColor(Color.BLUE);
  464. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  465. g2.setStroke(new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 4), 4)));
  466. } else {
  467. g2.setStroke(new BasicStroke(2));
  468. }
  469. g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
  470. edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
  471. edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
  472. edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
  473. if (edgeHighlight.getCapacity() == -1) {
  474. maxCap = Character.toString('\u221e');
  475. } else if (edgeHighlight.getCapacity() == -2) {
  476. maxCap = "???";
  477. } else {
  478. maxCap = String.valueOf(edgeHighlight.getCapacity());
  479. }
  480. if (showedInformation[0]) {
  481. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  482. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
  483. + controller.getScaleDiv2(),
  484. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
  485. + controller.getScaleDiv2());
  486. }
  487. }
  488. // Objects
  489. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  490. // Border Highlighting
  491. if (showedInformation[3]) {
  492. g2.setColor(cps.getBorderColor());
  493. if (g2.getColor() != Color.WHITE) {
  494. g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3),
  495. (int) (cps.getPosition().y - scalediv20 - 3),
  496. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  497. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  498. }
  499. }
  500. // node image
  501. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  502. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  503. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  504. } else {
  505. if (cps instanceof HolonSwitch) {
  506. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  507. ((HolonSwitch) cps).setAutoState(true);
  508. } else {
  509. ((HolonSwitch) cps).setAutoState(false);
  510. }
  511. }
  512. // Highlighting
  513. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  514. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  515. g2.setColor(Color.BLUE);
  516. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  517. (int) (controller.getScale() + (scalediv20 * 2)),
  518. (int) (controller.getScale() + (scalediv20 * 2)));
  519. if (showedInformation[1] && cps instanceof HolonObject) {
  520. g2.setColor(Color.BLACK);
  521. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  522. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  523. }
  524. } else if (cps instanceof HolonObject) {
  525. g2.setColor(((HolonObject) cps).getColor());
  526. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  527. (int) (controller.getScale() + (scalediv20 * 2)),
  528. (int) (controller.getScale() + (scalediv20 * 2)));
  529. if (showedInformation[1]) {
  530. g2.setColor(Color.BLACK);
  531. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  532. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  533. }
  534. }
  535. // draw image
  536. File checkPath = new File(cps.getImage());
  537. if (checkPath.exists()) {
  538. img = new ImageIcon(cps.getImage()).getImage();
  539. } else {
  540. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  541. }
  542. }
  543. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  544. null);
  545. }
  546. // Dragg Highlighting
  547. if (doMark) {
  548. g2.setColor(Color.BLACK);
  549. g2.setStroke(new BasicStroke(1));
  550. if (sx > x && sy > y) {
  551. g2.drawRect(x, y, sx - x, sy - y);
  552. } else if (sx < x && sy < y) {
  553. g2.drawRect(sx, sy, x - sx, y - sy);
  554. } else if (sx >= x) {
  555. g2.drawRect(x, sy, sx - x, y - sy);
  556. } else if (sy >= y) {
  557. g2.drawRect(sx, y, x - sx, sy - y);
  558. }
  559. }
  560. }
  561. @Override
  562. public void mouseClicked(MouseEvent e) {
  563. }
  564. @Override
  565. public void mouseEntered(MouseEvent e) {
  566. }
  567. @Override
  568. public void mouseExited(MouseEvent e) {
  569. }
  570. @Override
  571. public void mousePressed(MouseEvent e) {
  572. tempCps = null;
  573. dataSelected = null;
  574. edgeHighlight = null;
  575. controller.setSelecteEdge(null);
  576. // Object Selection
  577. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  578. cx = cps.getPosition().x;
  579. cy = cps.getPosition().y;
  580. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  581. tempCps = cps;
  582. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  583. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  584. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  585. controller.addTextToConsole("" + cps.getID(), Color.RED, 12, true, false, true);
  586. dragging = true;
  587. if (e.isControlDown() && tempCps != null) {
  588. if (model.getSelectedCpsObjects().contains(tempCps)) {
  589. controller.deleteSelectedObject(tempCps);
  590. } else {
  591. controller.addSelectedObject(tempCps);
  592. }
  593. }
  594. // If drawing an Edge (CTRL down)
  595. if (tempCps.getClass() == HolonObject.class) {
  596. HolonObject tempObj = ((HolonObject) tempCps);
  597. dataSelected = tempObj.getElements();
  598. }
  599. if (e.isShiftDown()) {
  600. drawEdge = true;
  601. dragging = false;
  602. }
  603. }
  604. }
  605. // Edge Selection
  606. if (tempCps == null) {
  607. edgeHighlight = mousePositionOnEdge(x, y);
  608. controller.setSelecteEdge(edgeHighlight);
  609. controller.setSelectedObjectID(0);
  610. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  611. model.getSelectedCpsObjects().clear();
  612. }
  613. }
  614. if (edgeHighlight == null && tempCps == null) {
  615. sx = e.getX();
  616. sy = e.getY();
  617. doMark = true;
  618. }
  619. repaint();
  620. }
  621. @Override
  622. public void mouseReleased(MouseEvent e) {
  623. dragging = false;
  624. if (drawEdge) {
  625. drawEdge = false;
  626. drawDeleteEdge();
  627. }
  628. if (dragged == true) {
  629. try {
  630. controller.autoSave();
  631. } catch (IOException ex) {
  632. // TODO Auto-generated catch block
  633. ex.printStackTrace();
  634. }
  635. }
  636. if (!e.isControlDown() && dragged == false && tempCps != null && e.BUTTON3 != e.getButton()) {
  637. model.getSelectedCpsObjects().clear();
  638. controller.addSelectedObject(tempCps);
  639. }
  640. dragged = false;
  641. // Rightclick List
  642. if (e.getButton() == MouseEvent.BUTTON3) {
  643. if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
  644. itemDelete.setEnabled(true);
  645. itemCut.setEnabled(true);
  646. itemCopy.setEnabled(true);
  647. if (tempCps != null)
  648. itemGroup.setEnabled(true);
  649. if (tempCps instanceof CpsUpperNode)
  650. itemUngroup.setEnabled(true);
  651. else
  652. itemUngroup.setEnabled(false);
  653. /*
  654. * if (!(tempCps instanceof HolonSwitch)) {
  655. * itemTrack.setEnabled(true); itemUntrack.setEnabled(true); }
  656. * else {
  657. */
  658. itemTrack.setEnabled(true);
  659. itemUntrack.setEnabled(true);
  660. /*
  661. * }
  662. */
  663. if (model.getSelectedCpsObjects().size() == 0) {
  664. controller.addSelectedObject(tempCps);
  665. }
  666. } else {
  667. itemCut.setEnabled(false);
  668. itemCopy.setEnabled(false);
  669. itemDelete.setEnabled(false);
  670. itemGroup.setEnabled(false);
  671. itemUngroup.setEnabled(false);
  672. itemTrack.setEnabled(false);
  673. itemUntrack.setEnabled(false);
  674. }
  675. mousePosition = this.getMousePosition();
  676. popmenu.show(e.getComponent(), e.getX(), e.getY());
  677. }
  678. if (doMark) {
  679. doMark = false;
  680. for (AbstractCpsObject cps : tempSelected) {
  681. if (!model.getSelectedCpsObjects().contains(cps)) {
  682. controller.addSelectedObject(cps);
  683. }
  684. }
  685. tempSelected.clear();
  686. }
  687. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch && MouseEvent.BUTTON3 != e.getButton()) {
  688. ((HolonSwitch) tempCps).switchState();
  689. }
  690. controller.calculateStateForTimeStep(model.getCurIteration());
  691. repaint();
  692. }
  693. @Override
  694. public void mouseDragged(MouseEvent e) {
  695. // If Edge is drawn
  696. x = e.getX();
  697. y = e.getY();
  698. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  699. model.getSelectedCpsObjects().clear();
  700. if (tempCps != null) {
  701. controller.addSelectedObject(tempCps);
  702. }
  703. }
  704. if (dragging) {
  705. try {
  706. dragged = true;
  707. float xDist, yDist; // Distance
  708. x = e.getX() - controller.getScaleDiv2();
  709. y = e.getY() - controller.getScaleDiv2();
  710. // Make sure its in bounds
  711. if (e.getX() < controller.getScaleDiv2())
  712. x = 0;
  713. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  714. x = this.getWidth() - controller.getScale();
  715. if (e.getY() < controller.getScaleDiv2())
  716. y = 0;
  717. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  718. y = this.getHeight() - controller.getScale();
  719. // Distance
  720. xDist = x - tempCps.getPosition().x;
  721. yDist = y - tempCps.getPosition().y;
  722. tempCps.setPosition(x, y); // Drag Position
  723. // TipText Position and name
  724. objectTT.setTipText(tempCps.getName() + ", " + tempCps.getID());
  725. objectTT.setLocation(x, y + controller.getScale());
  726. // All Selected Objects
  727. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  728. if (cps != tempCps) {
  729. x = (int) (cps.getPosition().x + xDist);
  730. y = (int) (cps.getPosition().y + yDist);
  731. // Make sure its in bounds
  732. if (x <= 0)
  733. x = 0;
  734. else if (x > this.getWidth() - controller.getScale())
  735. x = this.getWidth() - controller.getScale();
  736. if (y <= 0)
  737. y = 0;
  738. else if (y > this.getHeight() - controller.getScale())
  739. y = this.getHeight() - controller.getScale();
  740. cps.setPosition(x, y);
  741. }
  742. }
  743. repaint();
  744. } catch (Exception eex) {
  745. }
  746. }
  747. // Mark Objects
  748. if (doMark) {
  749. tempSelected.clear();
  750. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  751. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  752. if (sx >= x) {
  753. x1 = x;
  754. x2 = sx;
  755. }
  756. if (sy >= y) {
  757. y1 = y;
  758. y2 = sy;
  759. }
  760. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  761. && x2 >= cps.getPosition().x + model.getScaleDiv2()
  762. && y2 >= cps.getPosition().y + model.getScaleDiv2()) {
  763. tempSelected.add(cps);
  764. }
  765. }
  766. }
  767. repaint();
  768. }
  769. @Override
  770. public void mouseMoved(MouseEvent e) {
  771. x = e.getX();
  772. y = e.getY();
  773. // Everytghing for the tooltip :)
  774. boolean on = false;
  775. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  776. cx = cps.getPosition().x;
  777. cy = cps.getPosition().y;
  778. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  779. objectTT.setLocation(cx, cy + controller.getScale());
  780. objectTT.setTipText(cps.getName() + ", " + cps.getID());
  781. on = true;
  782. }
  783. }
  784. if (on) {
  785. objectTT.setVisible(true);
  786. } else {
  787. objectTT.setVisible(false);
  788. }
  789. }
  790. /**
  791. * Draws or Deletes an Edge.
  792. */
  793. private void drawDeleteEdge() {
  794. boolean node = true;
  795. boolean newEdge = true;
  796. boolean onEdge = true;
  797. boolean deleteNode = false;
  798. CpsEdge e = null;
  799. AbstractCpsObject tempCPS = null;
  800. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  801. cx = cps.getPosition().x;
  802. cy = cps.getPosition().y;
  803. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  804. && cps != tempCps) {
  805. node = false;
  806. onEdge = false;
  807. for (CpsEdge p : tempCps.getConnections()) {
  808. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  809. newEdge = false;
  810. e = p;
  811. }
  812. }
  813. if (!newEdge) {
  814. controller.removeEdgesOnCanvas(e);
  815. // Node ohne Edge?
  816. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  817. tempCps = e.getA();
  818. deleteNode = true;
  819. }
  820. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  821. tempCPS = e.getB();
  822. deleteNode = true;
  823. }
  824. }
  825. if (newEdge) {
  826. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  827. controller.addEdgeOnCanvas(e);
  828. }
  829. }
  830. }
  831. // Edge auf eine Edge gezogen?
  832. if (onEdge) {
  833. CpsEdge p = mousePositionOnEdge(x, y);
  834. if (p != null) {
  835. CpsEdge e1 = null;
  836. CpsEdge e2 = null;
  837. node = false;
  838. CpsNode n = new CpsNode("Node");
  839. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  840. controller.addObjectCanvas(n);
  841. AbstractCpsObject r, k;
  842. r = p.getA();
  843. k = p.getB();
  844. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  845. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  846. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  847. controller.removeEdgesOnCanvas(p);
  848. controller.addEdgeOnCanvas(e);
  849. controller.addEdgeOnCanvas(e1);
  850. controller.addEdgeOnCanvas(e2);
  851. }
  852. }
  853. // ins leere Gedragged
  854. if (node) {
  855. CpsNode n = new CpsNode("Node");
  856. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  857. controller.addObjectCanvas(n);
  858. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  859. controller.addEdgeOnCanvas(e);
  860. }
  861. // Wenn ein Node ohne Connections da ist
  862. if (deleteNode) {
  863. controller.delCanvasObject(tempCps);
  864. tempCps = null;
  865. }
  866. }
  867. /**
  868. * Checks if the mouse is on an Edge.
  869. *
  870. * @param x
  871. * Position of the Mouse
  872. * @param y
  873. * Position of the Mouse
  874. *
  875. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  876. */
  877. public CpsEdge mousePositionOnEdge(int x, int y) {
  878. int lx, ly, hx, hy;
  879. for (CpsEdge p : model.getEdgesOnCanvas()) {
  880. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  881. p.getB().getPosition().y);
  882. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  883. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  884. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  885. } else {
  886. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  887. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  888. }
  889. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  890. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  891. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  892. } else {
  893. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  894. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  895. }
  896. // distance from a point to a line and between both Objects
  897. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  898. && y < hy) {
  899. return p;
  900. }
  901. }
  902. return null;
  903. }
  904. public void updateLanguages() {
  905. itemCut.setText(Languages.getLanguage()[95]);
  906. itemCopy.setText(Languages.getLanguage()[96]);
  907. itemPaste.setText(Languages.getLanguage()[97]);
  908. itemDelete.setText(Languages.getLanguage()[98]);
  909. itemGroup.setText(Languages.getLanguage()[99]);
  910. itemUngroup.setText(Languages.getLanguage()[100]);
  911. itemTrack.setText(Languages.getLanguage()[101]);
  912. itemUntrack.setText(Languages.getLanguage()[102]);
  913. }
  914. /**
  915. * Checks if a double click was made.
  916. *
  917. * @return
  918. *
  919. * @return true if doublecklick, false if not
  920. */
  921. private boolean doubleClick() {
  922. if (click) {
  923. click = false;
  924. return true;
  925. } else {
  926. click = true;
  927. Timer t = new Timer("doubleclickTimer", false);
  928. t.schedule(new TimerTask() {
  929. @Override
  930. public void run() {
  931. click = false;
  932. }
  933. }, 500);
  934. }
  935. return false;
  936. }
  937. /**
  938. * Set if Information should be shown.
  939. *
  940. * @param connection
  941. * boolean for conecction
  942. * @param object
  943. * boolean for objects
  944. */
  945. public void setShowedInformation(boolean connection, boolean object, boolean border) {
  946. showedInformation[0] = connection;
  947. showedInformation[1] = object;
  948. showedInformation[3] = border;
  949. }
  950. /**
  951. * Returns if Information should be shown.
  952. *
  953. * @return Array of boolean [0] = connection, [1] = objects
  954. */
  955. public boolean[] getShowedInformation() {
  956. return showedInformation;
  957. }
  958. }