MyCanvas.java 26 KB

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