MyCanvas.java 28 KB

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