MyCanvas.java 32 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. 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. // TODO Auto-generated catch block
  270. JLabel message = new JLabel("The Clipboard information cannot be pastet into Application.");
  271. JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
  272. }
  273. repaint();
  274. });
  275. this.addMouseListener(this);
  276. this.addMouseMotionListener(this);
  277. }
  278. /**
  279. * Paints all Components on the Canvas.
  280. *
  281. * @param g Graphics
  282. */
  283. public void paintComponent(Graphics g) {
  284. String maxCap = null;
  285. super.paintComponent(g);
  286. // Rendering
  287. g2 = (Graphics2D) g;
  288. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  289. g2.setRenderingHints(rh);
  290. // Paint the Background
  291. if (!model.getCanvasImagePath().isEmpty()) {
  292. img = new ImageIcon(model.getCanvasImagePath()).getImage();
  293. switch (model.getCanvasImageMode()) {
  294. case BackgroundPopUp.IMAGE_PIXELS:
  295. g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
  296. break;
  297. case BackgroundPopUp.STRETCHED:
  298. g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(), null);
  299. break;
  300. case BackgroundPopUp.CUSTOM:
  301. g2.drawImage(img, 0, 0, model.getCanvasImageWidth(), model.getCanvasImageHeight(), null);
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. // SubNet Coloring
  308. int i = 0;
  309. for (SubNet s : controller.getSimManager().getSubNets()) {
  310. if (model.getSubNetColors().size() - 1 < i) {
  311. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  312. (int) (Math.random() * 255)));
  313. }
  314. if (showedInformation[3]) {
  315. for (HolonObject cps : s.getObjects()) {
  316. cps.setBorderColor(model.getSubNetColors().get(i));
  317. }
  318. }
  319. i++;
  320. }
  321. // drawEdges that is being dragged
  322. if (drawEdge) {
  323. g2.setColor(Color.BLACK);
  324. g2.setStroke(new BasicStroke(2));
  325. g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  326. }
  327. for (CpsEdge con : model.getEdgesOnCanvas()) {
  328. maxCap = paintEdge(con, maxCap);
  329. }
  330. // Highlighted Edge
  331. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  332. g2.setColor(Color.BLUE);
  333. for (CpsEdge con : model.getEdgesOnCanvas()) {
  334. if (con.getFlow() <= con.getCapacity()) {
  335. g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  336. } else {
  337. g2.setStroke(new BasicStroke(2));
  338. }
  339. maxCap = drawEdgeLine(con, maxCap);
  340. }
  341. } else if (edgeHighlight != null) {
  342. g2.setColor(Color.BLUE);
  343. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  344. g2.setStroke(new BasicStroke(
  345. Math.min(((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3) + 1), 4)));
  346. } else {
  347. g2.setStroke(new BasicStroke(2));
  348. }
  349. g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight.getA().getPosition().y,
  350. edgeHighlight.getB().getPosition().x, edgeHighlight.getB().getPosition().y);
  351. maxCap = setCapacityString(edgeHighlight, maxCap);
  352. if (showedInformation[0]) {
  353. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  354. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2,
  355. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2);
  356. }
  357. }
  358. // Objects
  359. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  360. // Border Highlighting
  361. if (showedInformation[3]) {
  362. g2.setColor(cps.getBorderColor());
  363. if (g2.getColor() != Color.WHITE && !(cps instanceof CpsNode)) {
  364. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20 - 3),
  365. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20 - 3),
  366. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  367. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  368. }
  369. }
  370. setEdgePictureAndHighlighting(cps);
  371. g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
  372. cps.getPosition().y - controller.getScaleDiv2(), controller.getScale(), controller.getScale(),
  373. null);
  374. }
  375. // Dragged marker Highlighting
  376. if (doMark) {
  377. g2.setColor(Color.BLACK);
  378. g2.setStroke(new BasicStroke(1));
  379. drawMarker();
  380. }
  381. // Tooltip
  382. showTooltip(g);
  383. }
  384. @Override
  385. public void mouseClicked(MouseEvent e) {
  386. if (e.getButton() == MouseEvent.BUTTON1) {
  387. if (model.getPropertyTable().getRowCount() > 0) {
  388. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  389. model.getPropertyTable().removeRow(i);
  390. }
  391. }
  392. updCon.paintProperties(tempCps);
  393. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  394. updCon.refreshTableProperties(model.getPropertyTable());
  395. }
  396. }
  397. @Override
  398. public void mouseEntered(MouseEvent e) {
  399. }
  400. @Override
  401. public void mouseExited(MouseEvent e) {
  402. }
  403. @Override
  404. public void mousePressed(MouseEvent e) {
  405. tempCps = null;
  406. edgeHighlight = null;
  407. controller.setSelecteEdge(null);
  408. // Object Selection
  409. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  410. cx = cps.getPosition().x - controller.getScaleDiv2();
  411. cy = cps.getPosition().y - controller.getScaleDiv2();
  412. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  413. tempCps = cps;
  414. setConsoleTextAfterSelect(cps);
  415. dragging = true;
  416. if (e.isControlDown() && tempCps != null) {
  417. if (model.getSelectedCpsObjects().contains(tempCps)) {
  418. controller.deleteSelectedObject(tempCps);
  419. } else {
  420. controller.addSelectedObject(tempCps);
  421. }
  422. }
  423. // If drawing an Edge (CTRL down)
  424. if (tempCps.getClass() == HolonObject.class) {
  425. HolonObject tempObj = ((HolonObject) tempCps);
  426. dataSelected = tempObj.getElements();
  427. }
  428. if (e.isShiftDown()) {
  429. drawEdge = true;
  430. dragging = false;
  431. }
  432. }
  433. }
  434. // Edge Selection
  435. if (tempCps == null) {
  436. edgeHighlight = mousePositionOnEdge(x, y);
  437. controller.setSelecteEdge(edgeHighlight);
  438. controller.setSelectedObjectID(0);
  439. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  440. model.getSelectedCpsObjects().clear();
  441. }
  442. }
  443. if (edgeHighlight == null && tempCps == null) {
  444. sx = e.getX();
  445. sy = e.getY();
  446. doMark = true;
  447. }
  448. repaint();
  449. }
  450. @Override
  451. public void mouseReleased(MouseEvent e) {
  452. x = e.getX();
  453. y = e.getY();
  454. dragging = false;
  455. if (drawEdge) {
  456. drawEdge = false;
  457. drawDeleteEdge();
  458. }
  459. if (dragged) {
  460. try {
  461. controller.autoSave();
  462. } catch (IOException ex) {
  463. ex.printStackTrace();
  464. }
  465. }
  466. if (!e.isControlDown() && !dragged && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  467. model.getSelectedCpsObjects().clear();
  468. controller.addSelectedObject(tempCps);
  469. }
  470. dragged = false;
  471. // Rightclick List
  472. setRightClickMenu(e);
  473. markObjects();
  474. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch && MouseEvent.BUTTON3 != e.getButton()) {
  475. ((HolonSwitch) tempCps).switchState();
  476. }
  477. controller.calculateStateForTimeStep(model.getCurIteration());
  478. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  479. updCon.refreshTableProperties(model.getPropertyTable());
  480. repaint();
  481. }
  482. @Override
  483. public void mouseDragged(MouseEvent e) {
  484. // If Edge is drawn
  485. x = e.getX();
  486. y = e.getY();
  487. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  488. model.getSelectedCpsObjects().clear();
  489. if (tempCps != null) {
  490. controller.addSelectedObject(tempCps);
  491. }
  492. }
  493. if (dragging) {
  494. try {
  495. dragged = true;
  496. float xDist, yDist; // Distance
  497. x = e.getX();
  498. y = e.getY();
  499. // Make sure its in bounds
  500. if (e.getX() < controller.getScaleDiv2())
  501. x = controller.getScaleDiv2();
  502. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  503. x = this.getWidth() - controller.getScaleDiv2();
  504. if (e.getY() < controller.getScaleDiv2())
  505. y = controller.getScaleDiv2();
  506. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  507. y = this.getHeight() - controller.getScaleDiv2();
  508. // Distance
  509. xDist = x - tempCps.getPosition().x;
  510. yDist = y - tempCps.getPosition().y;
  511. tempCps.setPosition(x, y); // Drag Position
  512. // ToolTipText Position and name
  513. toolTip = true;
  514. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  515. toolTipPos.x = tempCps.getPosition().x - controller.getScaleDiv2();
  516. toolTipPos.y = tempCps.getPosition().y + controller.getScaleDiv2();
  517. // All Selected Objects
  518. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  519. if (cps != tempCps) {
  520. x = (int) (cps.getPosition().x + xDist);
  521. y = (int) (cps.getPosition().y + yDist);
  522. // Make sure its in bounds
  523. if (x <= controller.getScaleDiv2())
  524. x = controller.getScaleDiv2();
  525. else if (x > this.getWidth() - controller.getScaleDiv2())
  526. x = this.getWidth() - controller.getScaleDiv2();
  527. if (y <= controller.getScaleDiv2())
  528. y = controller.getScaleDiv2();
  529. else if (y > this.getHeight() - controller.getScaleDiv2())
  530. y = this.getHeight() - controller.getScaleDiv2();
  531. cps.setPosition(x, y);
  532. }
  533. }
  534. repaint();
  535. } catch (Exception eex) {
  536. }
  537. }
  538. // Mark Objects
  539. if (doMark) {
  540. tempSelected.clear();
  541. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  542. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  543. if (sx >= x) {
  544. x1 = x;
  545. x2 = sx;
  546. }
  547. if (sy >= y) {
  548. y1 = y;
  549. y2 = sy;
  550. }
  551. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  552. && x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
  553. tempSelected.add(cps);
  554. }
  555. }
  556. }
  557. repaint();
  558. }
  559. @Override
  560. public void mouseMoved(MouseEvent e) {
  561. x = e.getX();
  562. y = e.getY();
  563. // Everything for the tooltip :)
  564. boolean on = false;
  565. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  566. cx = cps.getPosition().x - controller.getScaleDiv2();
  567. cy = cps.getPosition().y - controller.getScaleDiv2();
  568. on = setToolTipInfoAndPosition(on, cps);
  569. }
  570. toolTip = on;
  571. repaint();
  572. }
  573. /**
  574. * Draws or Deletes an Edge.
  575. */
  576. private void drawDeleteEdge() {
  577. if (getMousePosition() != null) {
  578. boolean node = true;
  579. boolean newEdge = true;
  580. boolean onEdge = true;
  581. boolean deleteNode = false;
  582. CpsEdge e = null;
  583. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  584. cx = cps.getPosition().x - controller.getScaleDiv2();
  585. cy = cps.getPosition().y - controller.getScaleDiv2();
  586. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && 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. }
  607. if (newEdge) {
  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. }