MyCanvas.java 23 KB

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