MyCanvas.java 32 KB

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