MyCanvas.java 36 KB

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