MyCanvas.java 35 KB

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