MyCanvas.java 29 KB

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