MyCanvas.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
  35. /**
  36. *
  37. */
  38. private static final long serialVersionUID = 1L;
  39. private Image img = null; // Contains the image to draw on MyCanvas
  40. private int x = 0;
  41. private int y = 0;
  42. // edge Object Start Point
  43. private Model model;
  44. private final Control controller;
  45. Graphics2D g2; // For Painting
  46. private int cx, cy;
  47. private int sx, sy; // Mark Coords
  48. private float scalediv20;
  49. ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
  50. ArrayList<AbstractCpsObject> TempSelected = new ArrayList<AbstractCpsObject>();
  51. private boolean[] showedInformation = new boolean[3];
  52. private boolean dragging = false; // for dragging
  53. private boolean dragged = false; // if an object/objects was/were dragged
  54. private boolean drawEdge = false; // for drawing edges
  55. private boolean click = false; // for double click
  56. private boolean doMark = false; // for double click
  57. public AbstractCpsObject tempCps = null;
  58. private Rectangle selectRect = new Rectangle();
  59. private CpsEdge edgeHighlight = null;
  60. // PopUpMenu
  61. private JPopupMenu popmenu = new JPopupMenu();
  62. private JMenuItem itemDelete = new JMenuItem("Delete");
  63. private JMenuItem itemCut = new JMenuItem("Cut");
  64. private JMenuItem itemCopy = new JMenuItem("Copy");
  65. public JMenuItem itemPaste = new JMenuItem("Paste");
  66. private JToolTip objectTT = new JToolTip();
  67. private Point mousePosition = new Point(); // Mouse Position when
  68. // rightclicked
  69. // contains the value of the Capacity for new created Edges
  70. private float edgeCapacity;
  71. public MyCanvas(Model mod, Control control) {
  72. this.add(objectTT);
  73. this.controller = control;
  74. this.model = mod;
  75. scalediv20 = model.getScale() / 20;
  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 (AbstractCpsObject 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 that is being dragged
  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. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  209. g2.setStroke(new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 4), 4)));
  210. } else {
  211. g2.setStroke(new BasicStroke(2));
  212. }
  213. g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
  214. edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
  215. edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
  216. edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
  217. if (edgeHighlight.getCapacity() == -1) {
  218. maxCap = Character.toString('\u221e');
  219. } else {
  220. maxCap = String.valueOf(edgeHighlight.getCapacity());
  221. }
  222. if (showedInformation[0]) {
  223. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  224. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
  225. + controller.getScaleDiv2(),
  226. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
  227. + controller.getScaleDiv2());
  228. }
  229. }
  230. // Objects
  231. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  232. // Border Highlighting
  233. g2.setColor(cps.getBorderColor());
  234. if (g2.getColor() != Color.WHITE) {
  235. g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3), (int) (cps.getPosition().y - scalediv20 - 3),
  236. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  237. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  238. }
  239. // node image
  240. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  241. || model.getSelectedCpsObjects().contains(cps) || TempSelected.contains(cps))) {
  242. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  243. } else {
  244. if (cps instanceof HolonSwitch) {
  245. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  246. ((HolonSwitch) cps).setAutoState(true);
  247. } else {
  248. ((HolonSwitch) cps).setAutoState(false);
  249. }
  250. }
  251. // Highlighting
  252. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && TempSelected.size() == 0)
  253. || model.getSelectedCpsObjects().contains(cps) || TempSelected.contains(cps)) {
  254. g2.setColor(Color.BLUE);
  255. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  256. (int) (controller.getScale() + (scalediv20 * 2)),
  257. (int) (controller.getScale() + (scalediv20 * 2)));
  258. if (showedInformation[1] && cps instanceof HolonObject) {
  259. g2.setColor(Color.BLACK);
  260. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  261. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  262. }
  263. } else if (cps instanceof HolonObject) {
  264. g2.setColor(((HolonObject) cps).getColor());
  265. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  266. (int) (controller.getScale() + (scalediv20 * 2)),
  267. (int) (controller.getScale() + (scalediv20 * 2)));
  268. if (showedInformation[1]) {
  269. g2.setColor(Color.BLACK);
  270. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  271. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  272. }
  273. }
  274. // draw image
  275. File checkPath = new File(cps.getImage());
  276. if (checkPath.exists()) {
  277. img = new ImageIcon(cps.getImage()).getImage();
  278. } else {
  279. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  280. }
  281. }
  282. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  283. null);
  284. }
  285. // Dragg Highlighting
  286. if (doMark) {
  287. g2.setColor(Color.BLACK);
  288. g2.setStroke(new BasicStroke(1));
  289. if (sx > x && sy > y) {
  290. g2.drawRect(x, y, sx - x, sy - y);
  291. } else if (sx < x && sy < y) {
  292. g2.drawRect(sx, sy, x - sx, y - sy);
  293. } else if (sx >= x) {
  294. g2.drawRect(x, sy, sx - x, y - sy);
  295. } else if (sy >= y) {
  296. g2.drawRect(sx, y, x - sx, sy - y);
  297. }
  298. }
  299. }
  300. @Override
  301. public void mouseClicked(MouseEvent e) {
  302. }
  303. @Override
  304. public void mouseEntered(MouseEvent e) {
  305. }
  306. @Override
  307. public void mouseExited(MouseEvent e) {
  308. }
  309. @Override
  310. public void mousePressed(MouseEvent e) {
  311. tempCps = null;
  312. dataSelected = null;
  313. edgeHighlight = null;
  314. controller.setSelecteEdge(null);
  315. // Object Selection
  316. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  317. cx = cps.getPosition().x;
  318. cy = cps.getPosition().y;
  319. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  320. tempCps = cps;
  321. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  322. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  323. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  324. controller.addTextToConsole("" + cps.getID(), Color.RED, 12, true, false, true);
  325. dragging = true;
  326. // If drawing an Edge (CTRL down)
  327. if (tempCps.getClass() == HolonObject.class) {
  328. HolonObject tempObj = ((HolonObject) tempCps);
  329. dataSelected = tempObj.getElements();
  330. }
  331. if (e.isShiftDown()) {
  332. drawEdge = true;
  333. dragging = false;
  334. }
  335. }
  336. }
  337. // Edge Selection
  338. if (tempCps == null) {
  339. edgeHighlight = mousePositionOnEdge(x, y);
  340. controller.setSelecteEdge(edgeHighlight);
  341. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  342. model.getSelectedCpsObjects().clear();
  343. }
  344. }
  345. if (edgeHighlight == null && tempCps == null) {
  346. sx = e.getX();
  347. sy = e.getY();
  348. doMark = true;
  349. }
  350. // Object Selection Highlighting (selectRect)
  351. objectSelectionHighlighting();
  352. repaint();
  353. }
  354. @Override
  355. public void mouseReleased(MouseEvent e) {
  356. dragging = false;
  357. if (drawEdge) {
  358. drawEdge = false;
  359. drawDeleteEdge();
  360. }
  361. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3 && dragged == false && tempCps != null) {
  362. model.getSelectedCpsObjects().clear();
  363. controller.addSelectedObject(tempCps);
  364. }
  365. if(dragged == true){
  366. try {
  367. controller.autoSave();
  368. } catch (IOException e1) {
  369. // TODO Auto-generated catch block
  370. e1.printStackTrace();
  371. }
  372. }
  373. dragged = false;
  374. // Rightclick List
  375. if (e.getButton() == MouseEvent.BUTTON3) {
  376. if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
  377. itemDelete.setEnabled(true);
  378. itemCut.setEnabled(true);
  379. itemCopy.setEnabled(true);
  380. if (model.getSelectedCpsObjects().size() == 0) {
  381. controller.addSelectedObject(tempCps);
  382. }
  383. } else {
  384. itemCut.setEnabled(false);
  385. itemCopy.setEnabled(false);
  386. itemDelete.setEnabled(false);
  387. }
  388. mousePosition = this.getMousePosition();
  389. popmenu.show(e.getComponent(), e.getX(), e.getY());
  390. }
  391. if (doMark) {
  392. doMark = false;
  393. for (AbstractCpsObject cps : TempSelected) {
  394. if (!model.getSelectedCpsObjects().contains(cps)) {
  395. controller.addSelectedObject(cps);
  396. }
  397. }
  398. TempSelected.clear();
  399. }
  400. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch) {
  401. ((HolonSwitch)tempCps).switchState();
  402. }
  403. controller.calculateStateForTimeStep(model.getCurIteration());
  404. repaint();
  405. }
  406. @Override
  407. public void mouseDragged(MouseEvent e) {
  408. // If Edge is drawn
  409. x = e.getX();
  410. y = e.getY();
  411. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  412. model.getSelectedCpsObjects().clear();
  413. if (tempCps != null) {
  414. controller.addSelectedObject(tempCps);
  415. }
  416. }
  417. if (dragging) {
  418. try {
  419. dragged = true;
  420. float xDist, yDist; // Distance
  421. x = e.getX() - controller.getScaleDiv2();
  422. y = e.getY() - controller.getScaleDiv2();
  423. // Make sure its in bounds
  424. if (e.getX() < controller.getScaleDiv2())
  425. x = 0;
  426. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  427. x = this.getWidth() - controller.getScale();
  428. if (e.getY() < controller.getScaleDiv2())
  429. y = 0;
  430. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  431. y = this.getHeight() - controller.getScale();
  432. // Distance
  433. xDist = x - tempCps.getPosition().x;
  434. yDist = y - tempCps.getPosition().y;
  435. tempCps.setPosition(x, y); // Drag Position
  436. selectRect.setLocation(x - (controller.getScale() / 20), y - (controller.getScale() / 20)); // Highlighting-Position
  437. // TipText Position and name
  438. objectTT.setTipText(tempCps.getName() + ", " + tempCps.getID());
  439. objectTT.setLocation(x, y + controller.getScale());
  440. // All Selected Objects
  441. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  442. if (cps != tempCps) {
  443. x = (int) (cps.getPosition().x + xDist);
  444. y = (int) (cps.getPosition().y + yDist);
  445. // Make sure its in bounds
  446. if (x <= 0)
  447. x = 0;
  448. else if (x > this.getWidth() - controller.getScale())
  449. x = this.getWidth() - controller.getScale();
  450. if (y <= 0)
  451. y = 0;
  452. else if (y > this.getHeight() - controller.getScale())
  453. y = this.getHeight() - controller.getScale();
  454. cps.setPosition(x, y);
  455. }
  456. }
  457. repaint();
  458. } catch (Exception e2) {
  459. }
  460. }
  461. // Mark Objects
  462. if (doMark) {
  463. TempSelected.clear();
  464. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  465. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  466. if (sx >= x) {
  467. x1 = x;
  468. x2 = sx;
  469. }
  470. if (sy >= y) {
  471. y1 = y;
  472. y2 = sy;
  473. }
  474. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  475. && x2 >= cps.getPosition().x + model.getScaleDiv2()
  476. && y2 >= cps.getPosition().y + model.getScaleDiv2()) {
  477. TempSelected.add(cps);
  478. }
  479. }
  480. }
  481. repaint();
  482. }
  483. @Override
  484. public void mouseMoved(MouseEvent e) {
  485. x = e.getX();
  486. y = e.getY();
  487. // Everytghing for the tooltip :)
  488. boolean on = false;
  489. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  490. cx = cps.getPosition().x;
  491. cy = cps.getPosition().y;
  492. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  493. objectTT.setTipText(cps.getName() + ", " + cps.getID());
  494. objectTT.setLocation(cx, cy + controller.getScale());
  495. on = true;
  496. }
  497. }
  498. if (!on) {
  499. objectTT.setLocation(-200, -200);
  500. objectTT.setTipText("");
  501. }
  502. }
  503. /**
  504. * Sets the Highlighting of the Selected Object
  505. */
  506. public void objectSelectionHighlighting() {
  507. if (tempCps != null) {
  508. selectRect.setBounds(tempCps.getPosition().x - (controller.getScale() / 20),
  509. tempCps.getPosition().y - (controller.getScale() / 20),
  510. controller.getScale() + ((controller.getScale() / 20) * 2),
  511. controller.getScale() + ((controller.getScale() / 20) * 2));
  512. controller.setSelectedObjectID(tempCps.getID());
  513. } else {
  514. controller.setSelectedObjectID(0);
  515. selectRect.setRect(0, 0, 0, 0);
  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. objectSelectionHighlighting();
  596. }
  597. }
  598. /**
  599. * Checks if the mouse is on an Edge
  600. *
  601. * @param x
  602. * Position of the Mouse
  603. * @param y
  604. * Position of the Mouse
  605. *
  606. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  607. */
  608. public CpsEdge mousePositionOnEdge(int x, int y) {
  609. int lx, ly, hx, hy;
  610. for (CpsEdge p : model.getEdgesOnCanvas()) {
  611. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  612. p.getB().getPosition().y);
  613. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  614. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  615. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  616. } else {
  617. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  618. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  619. }
  620. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  621. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  622. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  623. } else {
  624. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  625. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  626. }
  627. // distance from a point to a line and between both Objects
  628. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  629. && y < hy) {
  630. return p;
  631. }
  632. }
  633. return null;
  634. }
  635. /**
  636. * Checks if a double click was made
  637. *
  638. * @return true if doublecklick, false if not
  639. */
  640. private boolean doubleClick() {
  641. if (click) {
  642. click = false;
  643. return true;
  644. } else {
  645. click = true;
  646. Timer t = new Timer("doubleclickTimer", false);
  647. t.schedule(new TimerTask() {
  648. @Override
  649. public void run() {
  650. click = false;
  651. }
  652. }, 500);
  653. }
  654. return false;
  655. }
  656. public void setEdgeCapacity(float cap) {
  657. edgeCapacity = cap;
  658. }
  659. public void setShowedInformation(boolean connection, boolean object) {
  660. showedInformation[0] = connection;
  661. showedInformation[1] = object;
  662. }
  663. public boolean[] getShowedInformation() {
  664. return showedInformation;
  665. }
  666. }