MyCanvas.java 27 KB

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