MyCanvas.java 35 KB

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