MyCanvas.java 26 KB

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