MyCanvas.java 26 KB

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