UpperNodeCanvas.java 26 KB

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