MyCanvas.java 36 KB

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