MyCanvas.java 22 KB

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