UpperNodeCanvas.java 26 KB

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