MyCanvas.java 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. controller.addTextToConsole(imgPath);
  362. // Paint the Background
  363. if (!imgPath.isEmpty()) {
  364. img = new ImageIcon(imgPath).getImage();
  365. switch (backgroundMode) {
  366. case BackgroundPopUp.IMAGE_PIXELS:
  367. g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
  368. break;
  369. case BackgroundPopUp.STRETCHED:
  370. g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(), null);
  371. break;
  372. case BackgroundPopUp.CUSTOM:
  373. g2.drawImage(img, 0, 0, backgroundWidth, backgroundHeight, null);
  374. break;
  375. default:
  376. break;
  377. }
  378. }
  379. // Test SubNet Coloring
  380. int i = 0;
  381. for (SubNet s : controller.getSimManager().getSubNets()) {
  382. if (model.getSubNetColors().size() - 1 < i) {
  383. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  384. (int) (Math.random() * 255)));
  385. }
  386. if (showedInformation[3]) {
  387. for (HolonObject cps : s.getObjects()) {
  388. cps.setBorderColor(model.getSubNetColors().get(i));
  389. }
  390. }
  391. i++;
  392. }
  393. // drawEdges that is being dragged
  394. if (drawEdge) {
  395. g2.setColor(Color.BLACK);
  396. g2.setStroke(new BasicStroke(2));
  397. g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
  398. tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
  399. }
  400. for (CpsEdge con : model.getEdgesOnCanvas()) {
  401. if (con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
  402. && con != edgeHighlight) {
  403. if (con.getConnected() == 0) {
  404. if (con.getState()) {
  405. g2.setColor(Color.GREEN);
  406. if (con.getCapacity() != -1) {
  407. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  408. }
  409. } else {
  410. g2.setColor(Color.RED);
  411. g2.setStroke(new BasicStroke(2));
  412. }
  413. } else {
  414. g2.setColor(Color.DARK_GRAY);
  415. g2.setStroke(new BasicStroke(2));
  416. }
  417. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  418. con.getA().getPosition().y + controller.getScaleDiv2(),
  419. con.getB().getPosition().x + controller.getScaleDiv2(),
  420. con.getB().getPosition().y + controller.getScaleDiv2());
  421. if (con.getCapacity() == -1) {
  422. maxCap = Character.toString('\u221e');
  423. } else if (con.getCapacity() == -2) {
  424. maxCap = "???";
  425. } else {
  426. maxCap = String.valueOf(con.getCapacity());
  427. }
  428. if (showedInformation[0]) {
  429. if (con.getConnected() == 0 || con.getConnected() == 1) {
  430. g2.drawString(con.getFlow() + "/" + maxCap,
  431. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  432. + controller.getScaleDiv2(),
  433. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  434. + controller.getScaleDiv2());
  435. } else {
  436. g2.drawString("not connected",
  437. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  438. + controller.getScaleDiv2(),
  439. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  440. + controller.getScaleDiv2());
  441. }
  442. }
  443. }
  444. }
  445. // Highlighted Edge
  446. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  447. g2.setColor(Color.BLUE);
  448. for (CpsEdge con : model.getEdgesOnCanvas()) {
  449. if (con.getFlow() <= con.getCapacity()) {
  450. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  451. } else {
  452. g2.setStroke(new BasicStroke(2));
  453. }
  454. if (con.getA().getId() == model.getSelectedObjectID()
  455. || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
  456. || con.getB().getId() == model.getSelectedObjectID()
  457. || model.getSelectedCpsObjects().contains(con.getB())
  458. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  459. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  460. con.getA().getPosition().y + controller.getScaleDiv2(),
  461. con.getB().getPosition().x + controller.getScaleDiv2(),
  462. con.getB().getPosition().y + controller.getScaleDiv2());
  463. if (con.getCapacity() == -1) {
  464. maxCap = Character.toString('\u221e');
  465. } else if (con.getCapacity() == -2) {
  466. maxCap = "???";
  467. } else {
  468. maxCap = String.valueOf(con.getCapacity());
  469. }
  470. if (showedInformation[0]) {
  471. if (con.getConnected() == 0 || con.getConnected() == 1) {
  472. g2.drawString(con.getFlow() + "/" + maxCap,
  473. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  474. + controller.getScaleDiv2(),
  475. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  476. + controller.getScaleDiv2());
  477. } else {
  478. g2.drawString("not connected",
  479. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  480. + controller.getScaleDiv2(),
  481. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  482. + controller.getScaleDiv2());
  483. }
  484. }
  485. }
  486. }
  487. } else if (edgeHighlight != null) {
  488. g2.setColor(Color.BLUE);
  489. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  490. g2.setStroke(new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 4), 4)));
  491. } else {
  492. g2.setStroke(new BasicStroke(2));
  493. }
  494. g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
  495. edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
  496. edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
  497. edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
  498. if (edgeHighlight.getCapacity() == -1) {
  499. maxCap = Character.toString('\u221e');
  500. } else if (edgeHighlight.getCapacity() == -2) {
  501. maxCap = "???";
  502. } else {
  503. maxCap = String.valueOf(edgeHighlight.getCapacity());
  504. }
  505. if (showedInformation[0]) {
  506. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  507. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
  508. + controller.getScaleDiv2(),
  509. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
  510. + controller.getScaleDiv2());
  511. }
  512. }
  513. // Objects
  514. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  515. // Border Highlighting
  516. if (showedInformation[3]) {
  517. g2.setColor(cps.getBorderColor());
  518. if (g2.getColor() != Color.WHITE) {
  519. g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3),
  520. (int) (cps.getPosition().y - scalediv20 - 3),
  521. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  522. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  523. }
  524. }
  525. // node image
  526. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  527. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  528. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  529. } else {
  530. if (cps instanceof HolonSwitch) {
  531. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  532. ((HolonSwitch) cps).setAutoState(true);
  533. } else {
  534. ((HolonSwitch) cps).setAutoState(false);
  535. }
  536. }
  537. // Highlighting
  538. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  539. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  540. g2.setColor(Color.BLUE);
  541. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  542. (int) (controller.getScale() + (scalediv20 * 2)),
  543. (int) (controller.getScale() + (scalediv20 * 2)));
  544. if (showedInformation[1] && cps instanceof HolonObject) {
  545. g2.setColor(Color.BLACK);
  546. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  547. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  548. }
  549. } else if (cps instanceof HolonObject) {
  550. g2.setColor(((HolonObject) cps).getColor());
  551. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  552. (int) (controller.getScale() + (scalediv20 * 2)),
  553. (int) (controller.getScale() + (scalediv20 * 2)));
  554. if (showedInformation[1]) {
  555. g2.setColor(Color.BLACK);
  556. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  557. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  558. }
  559. }
  560. // draw image
  561. File checkPath = new File(cps.getImage());
  562. if (checkPath.exists()) {
  563. img = new ImageIcon(cps.getImage()).getImage();
  564. } else {
  565. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  566. }
  567. }
  568. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  569. null);
  570. }
  571. // Tooltip
  572. if (toolTip) {
  573. g2.setColor(new Color(255, 225, 150));
  574. g2.setStroke(new BasicStroke(1));
  575. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  576. // width
  577. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2(); // Position
  578. // fixed
  579. // x
  580. // Position
  581. // to
  582. // the
  583. // screen
  584. int fixYPos = toolTipPos.y; //// Position fixed y Position to the
  585. //// screen
  586. if (fixXPos < 0) {
  587. fixXPos = 0;
  588. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  589. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  590. }
  591. if (fixYPos + 16 > this.getHeight()) {
  592. fixYPos -= (fixYPos + 16) - this.getHeight();
  593. }
  594. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  595. g2.setColor(Color.BLACK);
  596. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  597. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  598. }
  599. // Dragg Highlighting
  600. if (doMark) {
  601. g2.setColor(Color.BLACK);
  602. g2.setStroke(new BasicStroke(1));
  603. if (sx > x && sy > y) {
  604. g2.drawRect(x, y, sx - x, sy - y);
  605. } else if (sx < x && sy < y) {
  606. g2.drawRect(sx, sy, x - sx, y - sy);
  607. } else if (sx >= x) {
  608. g2.drawRect(x, sy, sx - x, y - sy);
  609. } else if (sy >= y) {
  610. g2.drawRect(sx, y, x - sx, sy - y);
  611. }
  612. }
  613. }
  614. @Override
  615. public void mouseClicked(MouseEvent e) {
  616. if (e.getButton() == e.BUTTON1) {
  617. if (model.getPropertyTable().getRowCount() > 0) {
  618. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  619. model.getPropertyTable().removeRow(i);
  620. }
  621. }
  622. updCon.paintProperties(tempCps);
  623. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  624. updCon.refreshTableProperties(model.getPropertyTable());
  625. }
  626. }
  627. @Override
  628. public void mouseEntered(MouseEvent e) {
  629. }
  630. @Override
  631. public void mouseExited(MouseEvent e) {
  632. }
  633. @Override
  634. public void mousePressed(MouseEvent e) {
  635. tempCps = null;
  636. dataSelected = null;
  637. edgeHighlight = null;
  638. controller.setSelecteEdge(null);
  639. // Object Selection
  640. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  641. cx = cps.getPosition().x;
  642. cy = cps.getPosition().y;
  643. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  644. tempCps = cps;
  645. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  646. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  647. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  648. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  649. dragging = true;
  650. if (e.isControlDown() && tempCps != null) {
  651. if (model.getSelectedCpsObjects().contains(tempCps)) {
  652. controller.deleteSelectedObject(tempCps);
  653. } else {
  654. controller.addSelectedObject(tempCps);
  655. }
  656. }
  657. // If drawing an Edge (CTRL down)
  658. if (tempCps.getClass() == HolonObject.class) {
  659. HolonObject tempObj = ((HolonObject) tempCps);
  660. dataSelected = tempObj.getElements();
  661. }
  662. if (e.isShiftDown()) {
  663. drawEdge = true;
  664. dragging = false;
  665. }
  666. }
  667. }
  668. // Edge Selection
  669. if (tempCps == null) {
  670. edgeHighlight = mousePositionOnEdge(x, y);
  671. controller.setSelecteEdge(edgeHighlight);
  672. controller.setSelectedObjectID(0);
  673. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  674. model.getSelectedCpsObjects().clear();
  675. }
  676. }
  677. if (edgeHighlight == null && tempCps == null) {
  678. sx = e.getX();
  679. sy = e.getY();
  680. doMark = true;
  681. }
  682. repaint();
  683. }
  684. @Override
  685. public void mouseReleased(MouseEvent e) {
  686. dragging = false;
  687. if (drawEdge) {
  688. drawEdge = false;
  689. drawDeleteEdge();
  690. }
  691. if (dragged == true) {
  692. try {
  693. controller.autoSave();
  694. } catch (IOException ex) {
  695. // TODO Auto-generated catch block
  696. ex.printStackTrace();
  697. }
  698. }
  699. if (!e.isControlDown() && dragged == false && tempCps != null && e.BUTTON3 != e.getButton()) {
  700. model.getSelectedCpsObjects().clear();
  701. controller.addSelectedObject(tempCps);
  702. }
  703. dragged = false;
  704. // Rightclick List
  705. if (e.getButton() == MouseEvent.BUTTON3) {
  706. if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
  707. itemDelete.setEnabled(true);
  708. itemCut.setEnabled(true);
  709. itemCopy.setEnabled(true);
  710. if (tempCps != null)
  711. itemGroup.setEnabled(true);
  712. if (tempCps instanceof CpsUpperNode)
  713. itemUngroup.setEnabled(true);
  714. else
  715. itemUngroup.setEnabled(false);
  716. /*
  717. * if (!(tempCps instanceof HolonSwitch)) {
  718. * itemTrack.setEnabled(true); itemUntrack.setEnabled(true); }
  719. * else {
  720. */
  721. itemTrack.setEnabled(true);
  722. itemUntrack.setEnabled(true);
  723. /*
  724. * }
  725. */
  726. if (model.getSelectedCpsObjects().size() == 0) {
  727. controller.addSelectedObject(tempCps);
  728. }
  729. } else {
  730. itemCut.setEnabled(false);
  731. itemCopy.setEnabled(false);
  732. itemDelete.setEnabled(false);
  733. itemGroup.setEnabled(false);
  734. itemUngroup.setEnabled(false);
  735. itemTrack.setEnabled(false);
  736. itemUntrack.setEnabled(false);
  737. }
  738. mousePosition = this.getMousePosition();
  739. popmenu.show(e.getComponent(), e.getX(), e.getY());
  740. }
  741. if (doMark) {
  742. doMark = false;
  743. for (AbstractCpsObject cps : tempSelected) {
  744. if (!model.getSelectedCpsObjects().contains(cps)) {
  745. controller.addSelectedObject(cps);
  746. }
  747. }
  748. controller.getObjectsInDepth();
  749. tempSelected.clear();
  750. }
  751. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch && MouseEvent.BUTTON3 != e.getButton()) {
  752. ((HolonSwitch) tempCps).switchState();
  753. }
  754. controller.calculateStateForTimeStep(model.getCurIteration());
  755. repaint();
  756. }
  757. @Override
  758. public void mouseDragged(MouseEvent e) {
  759. // If Edge is drawn
  760. x = e.getX();
  761. y = e.getY();
  762. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  763. model.getSelectedCpsObjects().clear();
  764. if (tempCps != null) {
  765. controller.addSelectedObject(tempCps);
  766. }
  767. }
  768. if (dragging) {
  769. try {
  770. dragged = true;
  771. float xDist, yDist; // Distance
  772. x = e.getX() - controller.getScaleDiv2();
  773. y = e.getY() - controller.getScaleDiv2();
  774. // Make sure its in bounds
  775. if (e.getX() < controller.getScaleDiv2())
  776. x = 0;
  777. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  778. x = this.getWidth() - controller.getScale();
  779. if (e.getY() < controller.getScaleDiv2())
  780. y = 0;
  781. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  782. y = this.getHeight() - controller.getScale();
  783. // Distance
  784. xDist = x - tempCps.getPosition().x;
  785. yDist = y - tempCps.getPosition().y;
  786. tempCps.setPosition(x, y); // Drag Position
  787. // TipText Position and name
  788. toolTip = true;
  789. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  790. toolTipPos.x = tempCps.getPosition().x;
  791. toolTipPos.y = tempCps.getPosition().y + model.getScale();
  792. // All Selected Objects
  793. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  794. if (cps != tempCps) {
  795. x = (int) (cps.getPosition().x + xDist);
  796. y = (int) (cps.getPosition().y + yDist);
  797. // Make sure its in bounds
  798. if (x <= 0)
  799. x = 0;
  800. else if (x > this.getWidth() - controller.getScale())
  801. x = this.getWidth() - controller.getScale();
  802. if (y <= 0)
  803. y = 0;
  804. else if (y > this.getHeight() - controller.getScale())
  805. y = this.getHeight() - controller.getScale();
  806. cps.setPosition(x, y);
  807. }
  808. }
  809. repaint();
  810. } catch (Exception eex) {
  811. }
  812. }
  813. // Mark Objects
  814. if (doMark) {
  815. tempSelected.clear();
  816. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  817. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  818. if (sx >= x) {
  819. x1 = x;
  820. x2 = sx;
  821. }
  822. if (sy >= y) {
  823. y1 = y;
  824. y2 = sy;
  825. }
  826. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  827. && x2 >= cps.getPosition().x + model.getScaleDiv2()
  828. && y2 >= cps.getPosition().y + model.getScaleDiv2()) {
  829. tempSelected.add(cps);
  830. }
  831. }
  832. }
  833. repaint();
  834. }
  835. @Override
  836. public void mouseMoved(MouseEvent e) {
  837. x = e.getX();
  838. y = e.getY();
  839. // Everytghing for the tooltip :)
  840. boolean on = false;
  841. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  842. cx = cps.getPosition().x;
  843. cy = cps.getPosition().y;
  844. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  845. on = true;
  846. toolTipPos.x = cps.getPosition().x;
  847. toolTipPos.y = cps.getPosition().y + model.getScale();
  848. toolTipText = cps.getName() + ", " + cps.getId();
  849. }
  850. }
  851. if (on) {
  852. toolTip = true;
  853. } else {
  854. toolTip = false;
  855. }
  856. repaint();
  857. }
  858. /**
  859. * Draws or Deletes an Edge.
  860. */
  861. private void drawDeleteEdge() {
  862. boolean node = true;
  863. boolean newEdge = true;
  864. boolean onEdge = true;
  865. boolean deleteNode = false;
  866. CpsEdge e = null;
  867. AbstractCpsObject tempCPS = null;
  868. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  869. cx = cps.getPosition().x;
  870. cy = cps.getPosition().y;
  871. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  872. && cps != tempCps) {
  873. node = false;
  874. onEdge = false;
  875. for (CpsEdge p : tempCps.getConnections()) {
  876. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  877. newEdge = false;
  878. e = p;
  879. }
  880. }
  881. if (!newEdge) {
  882. controller.removeEdgesOnCanvas(e);
  883. // Node ohne Edge?
  884. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  885. tempCps = e.getA();
  886. deleteNode = true;
  887. }
  888. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  889. tempCPS = e.getB();
  890. deleteNode = true;
  891. }
  892. }
  893. if (newEdge) {
  894. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  895. controller.addEdgeOnCanvas(e);
  896. }
  897. }
  898. }
  899. // Edge auf eine Edge gezogen?
  900. if (onEdge) {
  901. CpsEdge p = mousePositionOnEdge(x, y);
  902. if (p != null) {
  903. CpsEdge e1 = null;
  904. CpsEdge e2 = null;
  905. node = false;
  906. CpsNode n = new CpsNode("Node");
  907. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  908. controller.addObjectCanvas(n);
  909. AbstractCpsObject r, k;
  910. r = p.getA();
  911. k = p.getB();
  912. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  913. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  914. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  915. controller.removeEdgesOnCanvas(p);
  916. controller.addEdgeOnCanvas(e);
  917. controller.addEdgeOnCanvas(e1);
  918. controller.addEdgeOnCanvas(e2);
  919. }
  920. }
  921. // ins leere Gedragged
  922. if (node) {
  923. CpsNode n = new CpsNode("Node");
  924. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  925. controller.addObjectCanvas(n);
  926. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  927. controller.addEdgeOnCanvas(e);
  928. }
  929. // Wenn ein Node ohne Connections da ist
  930. if (deleteNode) {
  931. controller.delCanvasObject(tempCps, true);
  932. tempCps = null;
  933. }
  934. }
  935. /**
  936. * Checks if the mouse is on an Edge.
  937. *
  938. * @param x
  939. * Position of the Mouse
  940. * @param y
  941. * Position of the Mouse
  942. *
  943. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  944. */
  945. public CpsEdge mousePositionOnEdge(int x, int y) {
  946. int lx, ly, hx, hy;
  947. for (CpsEdge p : model.getEdgesOnCanvas()) {
  948. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  949. p.getB().getPosition().y);
  950. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  951. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  952. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  953. } else {
  954. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  955. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  956. }
  957. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  958. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  959. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  960. } else {
  961. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  962. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  963. }
  964. // distance from a point to a line and between both Objects
  965. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  966. && y < hy) {
  967. return p;
  968. }
  969. }
  970. return null;
  971. }
  972. public void updateLanguages() {
  973. itemCut.setText(Languages.getLanguage()[95]);
  974. itemCopy.setText(Languages.getLanguage()[96]);
  975. itemPaste.setText(Languages.getLanguage()[97]);
  976. itemDelete.setText(Languages.getLanguage()[98]);
  977. itemGroup.setText(Languages.getLanguage()[99]);
  978. itemUngroup.setText(Languages.getLanguage()[100]);
  979. itemTrack.setText(Languages.getLanguage()[101]);
  980. itemUntrack.setText(Languages.getLanguage()[102]);
  981. }
  982. /**
  983. * Checks if a double click was made.
  984. *
  985. * @return
  986. *
  987. * @return true if doublecklick, false if not
  988. */
  989. private boolean doubleClick() {
  990. if (click) {
  991. click = false;
  992. return true;
  993. } else {
  994. click = true;
  995. Timer t = new Timer("doubleclickTimer", false);
  996. t.schedule(new TimerTask() {
  997. @Override
  998. public void run() {
  999. click = false;
  1000. }
  1001. }, 500);
  1002. }
  1003. return false;
  1004. }
  1005. /**
  1006. * Set if Information should be shown.
  1007. *
  1008. * @param connection
  1009. * boolean for conecction
  1010. * @param object
  1011. * boolean for objects
  1012. */
  1013. public void setShowedInformation(boolean connection, boolean object, boolean border) {
  1014. showedInformation[0] = connection;
  1015. showedInformation[1] = object;
  1016. showedInformation[3] = border;
  1017. }
  1018. /**
  1019. * Returns if Information should be shown.
  1020. *
  1021. * @return Array of boolean [0] = connection, [1] = objects
  1022. */
  1023. public boolean[] getShowedInformation() {
  1024. return showedInformation;
  1025. }
  1026. /**
  1027. * Set the Background Image;
  1028. *
  1029. * @param imagePath
  1030. * Image Path
  1031. * @param mode
  1032. * Image Mode
  1033. * @param width
  1034. * Image custom width
  1035. * @param height
  1036. * Image custom height
  1037. */
  1038. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  1039. imgPath = imagePath;
  1040. backgroundMode = mode;
  1041. backgroundWidth = width;
  1042. backgroundHeight = height;
  1043. }
  1044. }