MyCanvas.java 32 KB

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