MyCanvas.java 22 KB

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