MyCanvas.java 27 KB

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