MyCanvas.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. package ui.view;
  2. import classes.*;
  3. import com.google.gson.JsonParseException;
  4. import ui.controller.Control;
  5. import ui.controller.UpdateController;
  6. import ui.model.Model;
  7. import javax.swing.*;
  8. import java.awt.*;
  9. import java.awt.datatransfer.UnsupportedFlavorException;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import java.awt.event.MouseMotionListener;
  13. import java.awt.geom.Line2D;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. /**
  17. * This Class is the Canvas. All Objects will be visualized here
  18. *
  19. * @author Gruppe14
  20. */
  21. public class MyCanvas extends AbstractCanvas implements MouseListener, MouseMotionListener {
  22. private static final long serialVersionUID = 1L;
  23. /**
  24. * Constructor.
  25. *
  26. * @param mod the Model
  27. * @param control the Controller
  28. * @param unitGraph
  29. */
  30. public MyCanvas(Model mod, Control control, UnitGraph unitGraph) {
  31. toolTip = false;
  32. this.controller = control;
  33. this.model = mod;
  34. scalediv20 = model.getScale() / 20;
  35. showedInformation[0] = true;
  36. showedInformation[1] = true;
  37. showedInformation[3] = false;
  38. showedInformation[4] = true;
  39. control.setMaxCapacity(10000);
  40. popmenu.add(itemCut);
  41. popmenu.add(itemCopy);
  42. popmenu.add(itemPaste);
  43. popmenu.add(itemDelete);
  44. popmenu.addSeparator();
  45. popmenu.add(itemGroup);
  46. popmenu.add(itemUngroup);
  47. popmenu.add(itemTrack);
  48. popmenu.add(itemUntrack);
  49. updCon = new UpdateController(mod, control);
  50. itemDelete.setEnabled(false);
  51. itemCut.setEnabled(false);
  52. itemCopy.setEnabled(false);
  53. itemPaste.setEnabled(true);
  54. itemGroup.setEnabled(false);
  55. itemUngroup.setEnabled(false);
  56. itemTrack.setEnabled(false);
  57. itemUntrack.setEnabled(false);
  58. itemCut.setText(Languages.getLanguage()[95]);
  59. itemGroup.addActionListener(actionEvent -> {
  60. // calculate uppernode pos (taken from the controller)
  61. unPos = new Position(0, 0);
  62. animCps = new ArrayList<>();
  63. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  64. animCps.add(cps); // add to animation Cps ArrayList
  65. unPos.x += cps.getPosition().x;
  66. unPos.y += cps.getPosition().y;
  67. }
  68. unPos.x /= animCps.size();
  69. unPos.y /= animCps.size();
  70. // save old Position
  71. savePos = new ArrayList<>();
  72. for (int i = 0; i < animCps.size(); i++) {
  73. savePos.add(new Position(0, 0));
  74. savePos.get(i).x = animCps.get(i).getPosition().x;
  75. savePos.get(i).y = animCps.get(i).getPosition().y;
  76. }
  77. animT = new javax.swing.Timer(animDelay, actionEvent1 -> {
  78. if (animDuration - animDelay > 0 && animCps.size() > 1) {
  79. for (AbstractCpsObject animCpObject : animCps) {
  80. double x1 = animCpObject.getPosition().x - unPos.x;
  81. double y1 = animCpObject.getPosition().y - unPos.y;
  82. animCpObject.getPosition().x -= x1 / animSteps;
  83. animCpObject.getPosition().y -= y1 / animSteps;
  84. }
  85. repaint();
  86. animDuration -= animDelay;
  87. animSteps--;
  88. } else {
  89. animDuration = ANIMTIME;
  90. animSteps = animDuration / animDelay;
  91. animT.stop();
  92. for (int i = 0; i < animCps.size(); i++) {
  93. animCps.get(i).getPosition().x = savePos.get(i).x;
  94. animCps.get(i).getPosition().y = savePos.get(i).y;
  95. }
  96. controller.addUpperNode("NodeOfNode", null, animCps);
  97. controller.calculateStateForCurrentTimeStep();
  98. triggerUpdateController();
  99. repaint();
  100. }
  101. });
  102. animT.start();
  103. });
  104. itemUngroup.addActionListener(actionEvent -> {
  105. // save old Position
  106. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent().getParent().getParent();
  107. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  108. if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInner.getComponentAt(i)).getViewport()
  109. .getComponent(0)).upperNode.getId() == tempCps.getId()) {
  110. tabbedPaneInner.remove(i);
  111. break;
  112. }
  113. }
  114. savePos = new ArrayList<>();
  115. animCps = ((CpsUpperNode) tempCps).getNodes();
  116. controller.delUpperNode((CpsUpperNode) tempCps, null);
  117. for (int i = 0; i < animCps.size(); i++) {
  118. savePos.add(new Position(0, 0));
  119. savePos.get(i).x = animCps.get(i).getPosition().x;
  120. savePos.get(i).y = animCps.get(i).getPosition().y;
  121. }
  122. for (AbstractCpsObject cps : animCps) {
  123. int x = tempCps.getPosition().x;
  124. int y = tempCps.getPosition().y;
  125. cps.setPosition(new Position(x, y));
  126. }
  127. animT = new javax.swing.Timer(animDelay, actionEvent1 -> {
  128. if (animDuration - animDelay >= 0) {
  129. for (int i = 0; i < animCps.size(); i++) {
  130. double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
  131. double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
  132. animCps.get(i).getPosition().x -= x1 / animSteps;
  133. animCps.get(i).getPosition().y -= y1 / animSteps;
  134. }
  135. repaint();
  136. animDuration -= animDelay;
  137. animSteps--;
  138. } else {
  139. animDuration = ANIMTIME;
  140. animSteps = animDuration / animDelay;
  141. animT.stop();
  142. for (int i = 0; i < animCps.size(); i++) {
  143. animCps.get(i).getPosition().x = savePos.get(i).x;
  144. animCps.get(i).getPosition().y = savePos.get(i).y;
  145. }
  146. controller.calculateStateForCurrentTimeStep();
  147. triggerUpdateController();
  148. repaint();
  149. }
  150. });
  151. animT.start();
  152. });
  153. // adds the selected object(s) to the statistic panel
  154. itemTrack.addActionListener(actionEvent -> {
  155. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  156. boolean found = false;
  157. if (controller.getTrackingObj() != null) {
  158. if (controller.getTrackingObj().contains(o)) {
  159. found = true;
  160. }
  161. }
  162. if (!found) {
  163. controller.addTrackingObj(o);
  164. if (o instanceof HolonObject) {
  165. ((HolonObject) o).updateTrackingInfo();
  166. }
  167. }
  168. if (model.getShowConsoleLog()) {
  169. controller.addTextToConsole("Tracking: ", Color.BLACK, 12, false, false, false);
  170. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  171. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  172. controller.addTextToConsole("" + o.getId(), Color.RED, 12, true, false, true);
  173. }
  174. }
  175. });
  176. itemUntrack.addActionListener(actionEvent -> {
  177. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  178. if (o instanceof HolonObject) {
  179. boolean found = false;
  180. if (controller.getTrackingObj() != null) {
  181. for (AbstractCpsObject obj : controller.getTrackingObj()) {
  182. if (obj instanceof HolonObject) {
  183. if (obj.getId() == o.getId()) {
  184. found = true;
  185. }
  186. }
  187. }
  188. }
  189. if (found) {
  190. // Removed from tracking array and tracking
  191. // information reseted
  192. controller.removeTrackingObj(o);
  193. ((HolonObject) o).setTrackingProd(new float[100]);
  194. ((HolonObject) o).setTrackingCons(new float[100]);
  195. }
  196. if (model.getShowConsoleLog()) {
  197. controller.addTextToConsole("Untracking: ", Color.BLACK, 12, false, false, false);
  198. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  199. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  200. controller.addTextToConsole("" + o.getId(), Color.RED, 12, true, false, true);
  201. }
  202. }
  203. }
  204. });
  205. itemDelete.addActionListener(actionEvent -> {
  206. // Remove the selected Object objects
  207. boolean save = false;
  208. for (int j = 0; j < model.getSelectedCpsObjects().size(); j++) {
  209. AbstractCpsObject cps = model.getSelectedCpsObjects().get(j);
  210. if (j == model.getSelectedCpsObjects().size() - 1)
  211. save = true;
  212. controller.delCanvasObject(cps, save);
  213. controller.removeTrackingObj(cps);
  214. // Remove UpperNodeTab if UpperNode deleted
  215. if (cps instanceof CpsUpperNode) {
  216. JSplitPane tempSplit = (JSplitPane) getParent().getParent().getParent().getParent();
  217. JTabbedPane tabbedPane;
  218. JTabbedPane tabbedPane2;
  219. // if SplitView is activated
  220. if (tempSplit.getLeftComponent() instanceof JTabbedPane
  221. && tempSplit.getRightComponent() instanceof JTabbedPane) {
  222. tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
  223. tabbedPane2 = (JTabbedPane) tempSplit.getRightComponent();
  224. } else {
  225. tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
  226. tabbedPane2 = null;
  227. }
  228. // Look if the uppernode is open in a Tab
  229. for (int i = 4; i < tabbedPane.getTabCount(); i++) {
  230. if (tabbedPane.getComponentAt(i) != null
  231. && ((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  232. .getComponent(0)).upperNode.getId() == cps.getId()) {
  233. ((ButtonTabComponent) tabbedPane.getTabComponentAt(i)).removeTabs();
  234. break;
  235. }
  236. }
  237. // If SplitView is on and the view on
  238. // tabbedPane2 is the deleted upperNode
  239. try {
  240. if (tabbedPane2 != null
  241. && ((UpperNodeCanvas) ((JScrollPane) tabbedPane2.getSelectedComponent())
  242. .getViewport().getComponent(0)).upperNode.getId() == cps.getId()) {
  243. ((ButtonTabComponent) tabbedPane.getTabComponentAt(tabbedPane2.getSelectedIndex()))
  244. .removeTabs();
  245. }
  246. } catch (Exception e2) {
  247. }
  248. }
  249. toolTip = false;
  250. }
  251. model.getSelectedCpsObjects().clear();
  252. tempCps = null;
  253. repaint();
  254. });
  255. itemCut.addActionListener(actionEvent
  256. -> {
  257. controller.cut(null);
  258. itemPaste.setEnabled(true);
  259. repaint();
  260. });
  261. itemCopy.addActionListener(actionEvent -> {
  262. controller.copy(null);
  263. itemPaste.setEnabled(true);
  264. repaint();
  265. });
  266. itemPaste.addActionListener(actionEvent -> {
  267. try {
  268. controller.paste(null, mousePosition);
  269. unitGraph.update(model.getSelectedCpsObjects());
  270. } catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
  271. JLabel message = new JLabel("The Clipboard information cannot be pastet into Application.");
  272. JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
  273. }
  274. repaint();
  275. });
  276. this.addMouseListener(this);
  277. this.addMouseMotionListener(this);
  278. }
  279. /**
  280. * Paints all Components on the Canvas.
  281. *
  282. * @param g Graphics
  283. */
  284. public void paintComponent(Graphics g) {
  285. String maxCap = null;
  286. super.paintComponent(g);
  287. // Rendering
  288. g2 = (Graphics2D) g;
  289. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  290. g2.setRenderingHints(rh);
  291. // Paint the Background
  292. if (!model.getCanvasImagePath().isEmpty()) {
  293. img = new ImageIcon(model.getCanvasImagePath()).getImage();
  294. switch (model.getCanvasImageMode()) {
  295. case BackgroundPopUp.IMAGE_PIXELS:
  296. g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
  297. break;
  298. case BackgroundPopUp.STRETCHED:
  299. g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(), null);
  300. break;
  301. case BackgroundPopUp.CUSTOM:
  302. g2.drawImage(img, 0, 0, model.getCanvasImageWidth(), model.getCanvasImageHeight(), null);
  303. break;
  304. default:
  305. break;
  306. }
  307. }
  308. // SubNet Coloring
  309. int i = 0;
  310. for (SubNet s : controller.getSimManager().getSubNets()) {
  311. if (model.getSubNetColors().size() - 1 < i) {
  312. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  313. (int) (Math.random() * 255)));
  314. }
  315. if (showedInformation[3]) {
  316. for (HolonObject cps : s.getObjects()) {
  317. cps.setBorderColor(model.getSubNetColors().get(i));
  318. }
  319. }
  320. i++;
  321. }
  322. // drawEdges that is being dragged
  323. if (drawEdge) {
  324. g2.setColor(Color.BLACK);
  325. g2.setStroke(new BasicStroke(2));
  326. g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  327. }
  328. for (CpsEdge con : model.getEdgesOnCanvas()) {
  329. maxCap = paintEdge(con, maxCap);
  330. }
  331. // Highlighted Edge
  332. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  333. g2.setColor(Color.BLUE);
  334. for (CpsEdge con : model.getEdgesOnCanvas()) {
  335. if (con.getFlow() <= con.getCapacity()) {
  336. g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  337. } else {
  338. g2.setStroke(new BasicStroke(2));
  339. }
  340. maxCap = drawEdgeLine(con, maxCap);
  341. }
  342. } else if (edgeHighlight != null) {
  343. g2.setColor(Color.BLUE);
  344. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  345. g2.setStroke(new BasicStroke(
  346. Math.min(((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3) + 1), 4)));
  347. } else {
  348. g2.setStroke(new BasicStroke(2));
  349. }
  350. g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight.getA().getPosition().y,
  351. edgeHighlight.getB().getPosition().x, edgeHighlight.getB().getPosition().y);
  352. maxCap = setCapacityString(edgeHighlight, maxCap);
  353. if (showedInformation[0]) {
  354. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  355. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2,
  356. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2);
  357. }
  358. }
  359. // Objects
  360. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  361. // Border Highlighting
  362. if (showedInformation[3]) {
  363. g2.setColor(cps.getBorderColor());
  364. if (g2.getColor() != Color.WHITE && !(cps instanceof CpsNode)) {
  365. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20 - 3),
  366. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20 - 3),
  367. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  368. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  369. }
  370. }
  371. setEdgePictureAndHighlighting(cps);
  372. g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
  373. cps.getPosition().y - controller.getScaleDiv2(), controller.getScale(), controller.getScale(),
  374. null);
  375. }
  376. // Dragged marker Highlighting
  377. if (doMark) {
  378. g2.setColor(Color.BLACK);
  379. g2.setStroke(new BasicStroke(1));
  380. drawMarker();
  381. }
  382. // Tooltip
  383. showTooltip(g);
  384. }
  385. @Override
  386. public void mouseClicked(MouseEvent e) {
  387. if (e.getButton() == MouseEvent.BUTTON1) {
  388. if (model.getPropertyTable().getRowCount() > 0) {
  389. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  390. model.getPropertyTable().removeRow(i);
  391. }
  392. }
  393. triggerUpdateController();
  394. }
  395. }
  396. @Override
  397. public void mouseEntered(MouseEvent e) {
  398. }
  399. @Override
  400. public void mouseExited(MouseEvent e) {
  401. }
  402. @Override
  403. public void mousePressed(MouseEvent e) {
  404. tempCps = null;
  405. edgeHighlight = null;
  406. controller.setSelecteEdge(null);
  407. // Object Selection
  408. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  409. cx = cps.getPosition().x - controller.getScaleDiv2();
  410. cy = cps.getPosition().y - controller.getScaleDiv2();
  411. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  412. tempCps = cps;
  413. setConsoleTextAfterSelect(cps);
  414. dragging = true;
  415. if (e.isControlDown() && tempCps != null) {
  416. if (model.getSelectedCpsObjects().contains(tempCps)) {
  417. controller.deleteSelectedObject(tempCps);
  418. } else {
  419. controller.addSelectedObject(tempCps);
  420. }
  421. }
  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. x = e.getX();
  452. y = e.getY();
  453. dragging = false;
  454. if (drawEdge) {
  455. drawEdge = false;
  456. drawDeleteEdge();
  457. }
  458. if (dragged) {
  459. try {
  460. controller.autoSave();
  461. } catch (IOException ex) {
  462. ex.printStackTrace();
  463. }
  464. }
  465. if (!e.isControlDown() && !dragged && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  466. model.getSelectedCpsObjects().clear();
  467. controller.addSelectedObject(tempCps);
  468. }
  469. dragged = false;
  470. // Rightclick List
  471. setRightClickMenu(e);
  472. markObjects();
  473. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch && MouseEvent.BUTTON3 != e.getButton()) {
  474. ((HolonSwitch) tempCps).switchState();
  475. }
  476. controller.calculateStateForTimeStep(model.getCurIteration());
  477. triggerUpdateController();
  478. repaint();
  479. }
  480. @Override
  481. public void mouseDragged(MouseEvent e) {
  482. // If Edge is drawn
  483. x = e.getX();
  484. y = e.getY();
  485. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  486. model.getSelectedCpsObjects().clear();
  487. if (tempCps != null) {
  488. controller.addSelectedObject(tempCps);
  489. }
  490. }
  491. if (dragging) {
  492. try {
  493. dragged = true;
  494. float xDist, yDist; // Distance
  495. x = e.getX();
  496. y = e.getY();
  497. // Make sure its in bounds
  498. if (e.getX() < controller.getScaleDiv2())
  499. x = controller.getScaleDiv2();
  500. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  501. x = this.getWidth() - controller.getScaleDiv2();
  502. if (e.getY() < controller.getScaleDiv2())
  503. y = controller.getScaleDiv2();
  504. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  505. y = this.getHeight() - controller.getScaleDiv2();
  506. // Distance
  507. xDist = x - tempCps.getPosition().x;
  508. yDist = y - tempCps.getPosition().y;
  509. tempCps.setPosition(x, y); // Drag Position
  510. // ToolTipText Position and name
  511. toolTip = true;
  512. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  513. toolTipPos.x = tempCps.getPosition().x - controller.getScaleDiv2();
  514. toolTipPos.y = tempCps.getPosition().y + controller.getScaleDiv2();
  515. // All Selected Objects
  516. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  517. if (cps != tempCps) {
  518. x = (int) (cps.getPosition().x + xDist);
  519. y = (int) (cps.getPosition().y + yDist);
  520. // Make sure its in bounds
  521. if (x <= controller.getScaleDiv2())
  522. x = controller.getScaleDiv2();
  523. else if (x > this.getWidth() - controller.getScaleDiv2())
  524. x = this.getWidth() - controller.getScaleDiv2();
  525. if (y <= controller.getScaleDiv2())
  526. y = controller.getScaleDiv2();
  527. else if (y > this.getHeight() - controller.getScaleDiv2())
  528. y = this.getHeight() - controller.getScaleDiv2();
  529. cps.setPosition(x, y);
  530. }
  531. }
  532. repaint();
  533. } catch (Exception eex) {
  534. }
  535. }
  536. // Mark Objects
  537. if (doMark) {
  538. tempSelected.clear();
  539. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  540. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  541. if (sx >= x) {
  542. x1 = x;
  543. x2 = sx;
  544. }
  545. if (sy >= y) {
  546. y1 = y;
  547. y2 = sy;
  548. }
  549. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  550. && x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
  551. tempSelected.add(cps);
  552. }
  553. }
  554. }
  555. repaint();
  556. }
  557. @Override
  558. public void mouseMoved(MouseEvent e) {
  559. x = e.getX();
  560. y = e.getY();
  561. // Everything for the tooltip :)
  562. boolean on = false;
  563. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  564. cx = cps.getPosition().x - controller.getScaleDiv2();
  565. cy = cps.getPosition().y - controller.getScaleDiv2();
  566. on = setToolTipInfoAndPosition(on, cps);
  567. }
  568. toolTip = on;
  569. repaint();
  570. }
  571. /**
  572. * Draws or Deletes an Edge.
  573. */
  574. void drawDeleteEdge() {
  575. if (getMousePosition() != null) {
  576. boolean node = true;
  577. boolean newEdge = true;
  578. boolean onEdge = true;
  579. boolean deleteNode = false;
  580. CpsEdge e = null;
  581. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  582. cx = cps.getPosition().x - controller.getScaleDiv2();
  583. cy = cps.getPosition().y - controller.getScaleDiv2();
  584. if (x - controller.getScale() <= cx
  585. && y - controller.getScale() <= cy
  586. && x >= cx && y >= cy
  587. && cps != tempCps) {
  588. node = false;
  589. onEdge = false;
  590. for (CpsEdge p : tempCps.getConnections()) {
  591. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  592. newEdge = false;
  593. e = p;
  594. }
  595. }
  596. if (!newEdge) {
  597. controller.removeEdgesOnCanvas(e);
  598. // Node ohne Edge?
  599. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  600. tempCps = e.getA();
  601. deleteNode = true;
  602. }
  603. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  604. deleteNode = true;
  605. }
  606. } else {
  607. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  608. controller.addEdgeOnCanvas(e);
  609. }
  610. }
  611. }
  612. // Edge auf eine Edge gezogen?
  613. if (onEdge) {
  614. CpsEdge p = mousePositionOnEdge(x, y);
  615. if (p != null) {
  616. CpsEdge e1;
  617. CpsEdge e2;
  618. node = false;
  619. CpsNode n = new CpsNode("Node");
  620. n.setPosition(x, y);
  621. controller.addObjectCanvas(n);
  622. AbstractCpsObject r, k;
  623. r = p.getA();
  624. k = p.getB();
  625. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  626. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  627. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  628. controller.removeEdgesOnCanvas(p);
  629. controller.addEdgeOnCanvas(e);
  630. controller.addEdgeOnCanvas(e1);
  631. controller.addEdgeOnCanvas(e2);
  632. }
  633. }
  634. // ins leere Gedragged
  635. if (node) {
  636. CpsNode n = new CpsNode("Node");
  637. n.setPosition(x, y);
  638. controller.addObjectCanvas(n);
  639. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  640. controller.addEdgeOnCanvas(e);
  641. }
  642. // Wenn ein Node ohne Connections da ist
  643. if (deleteNode) {
  644. controller.delCanvasObject(tempCps, true);
  645. tempCps = null;
  646. }
  647. }
  648. }
  649. /**
  650. * Checks if the mouse is on an Edge.
  651. *
  652. * @param x Position of the Mouse
  653. * @param y Position of the Mouse
  654. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  655. */
  656. private CpsEdge mousePositionOnEdge(int x, int y) {
  657. x += controller.getScaleDiv2();
  658. y += controller.getScaleDiv2();
  659. for (CpsEdge p : model.getEdgesOnCanvas()) {
  660. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  661. p.getB().getPosition().y);
  662. int[] positions = determineMousePositionOnEdge(p);
  663. int lx = positions[0];
  664. int ly = positions[1];
  665. int hx = positions[2];
  666. int hy = positions[3];
  667. // distance from a point to a line and between both Objects
  668. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  669. && y < hy) {
  670. return p;
  671. }
  672. }
  673. return null;
  674. }
  675. void updateLanguages() {
  676. itemCut.setText(Languages.getLanguage()[95]);
  677. itemCopy.setText(Languages.getLanguage()[96]);
  678. itemPaste.setText(Languages.getLanguage()[97]);
  679. itemDelete.setText(Languages.getLanguage()[98]);
  680. itemGroup.setText(Languages.getLanguage()[99]);
  681. itemUngroup.setText(Languages.getLanguage()[100]);
  682. itemTrack.setText(Languages.getLanguage()[101]);
  683. itemUntrack.setText(Languages.getLanguage()[102]);
  684. }
  685. /**
  686. * Set if Information should be shown.
  687. *
  688. * @param connection boolean for conecction
  689. * @param object boolean for objects
  690. * @param nodeOfnode
  691. */
  692. void setShowedInformation(boolean connection, boolean object, boolean border, boolean nodeOfnode) {
  693. showedInformation[0] = connection;
  694. showedInformation[1] = object;
  695. showedInformation[3] = border;
  696. showedInformation[4] = nodeOfnode;
  697. }
  698. /**
  699. * Returns if Information should be shown.
  700. *
  701. * @return Array of boolean [0] = connection, [1] = objects
  702. */
  703. boolean[] getShowedInformation() {
  704. return showedInformation;
  705. }
  706. /**
  707. * set toolTip
  708. *
  709. * @param bool
  710. */
  711. void setToolTip(boolean bool) {
  712. this.toolTip = bool;
  713. }
  714. /**
  715. * Set the Mouse
  716. *
  717. * @param x
  718. * @param y
  719. */
  720. void setXY(int x, int y) {
  721. this.x = x;
  722. this.y = y;
  723. }
  724. }