MyCanvas.java 36 KB

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