MyCanvas.java 28 KB

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