MyCanvas.java 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. for (CpsEdge con : model.getEdgesOnCanvas()) {
  396. maxCap = paintEdge(con, maxCap);
  397. }
  398. // Highlighted Edge
  399. if (model.getSelectedObjectID() > 0
  400. || !model.getSelectedCpsObjects().isEmpty()
  401. || !tempSelected.isEmpty()) {
  402. g2.setColor(Color.BLUE);
  403. for (CpsEdge con : model.getEdgesOnCanvas()) {
  404. if (con.getFlow() <= con.getCapacity()) {
  405. g2.setStroke(new BasicStroke(Math.min(
  406. ((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  407. } else {
  408. g2.setStroke(new BasicStroke(2));
  409. }
  410. maxCap = drawEdgeLine(con, maxCap);
  411. }
  412. } else if (edgeHighlight != null) {
  413. g2.setColor(Color.BLUE);
  414. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  415. g2.setStroke(new BasicStroke(Math.min(((edgeHighlight.getFlow()
  416. / edgeHighlight.getCapacity() * 3) + 1), 4)));
  417. } else {
  418. g2.setStroke(new BasicStroke(2));
  419. }
  420. g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight
  421. .getA().getPosition().y,
  422. edgeHighlight.getB().getPosition().x, edgeHighlight.getB()
  423. .getPosition().y);
  424. maxCap = setCapacityString(edgeHighlight, maxCap);
  425. if (showedInformation[0]) {
  426. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  427. (edgeHighlight.getA().getPosition().x + edgeHighlight
  428. .getB().getPosition().x) / 2, (edgeHighlight
  429. .getA().getPosition().y + edgeHighlight.getB()
  430. .getPosition().y) / 2);
  431. }
  432. }
  433. /**
  434. * highlight the Object that would be replaced
  435. */
  436. highlightMayBeReplaced(g2);
  437. // Objects
  438. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  439. // Border Highlighting
  440. if (showedInformation[3]) {
  441. g2.setColor(cps.getBorderColor());
  442. if (g2.getColor() != Color.WHITE && !(cps instanceof CpsNode)) {
  443. g2.fillRect(
  444. (int) (cps.getPosition().x
  445. - controller.getScaleDiv2() - scalediv20 - 3),
  446. (int) (cps.getPosition().y
  447. - controller.getScaleDiv2() - scalediv20 - 3),
  448. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  449. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  450. }
  451. }
  452. setEdgePictureAndHighlighting(cps);
  453. g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
  454. cps.getPosition().y - controller.getScaleDiv2(),
  455. controller.getScale(), controller.getScale(), null);
  456. paintSupplyBar(g, cps);
  457. }
  458. // Dragged marker Highlighting
  459. if (doMark) {
  460. g2.setColor(Color.BLACK);
  461. g2.setStroke(new BasicStroke(0));
  462. drawMarker();
  463. }
  464. // Tooltip
  465. showTooltip(g);
  466. }
  467. @Override
  468. public void mouseClicked(MouseEvent e) {
  469. if (e.getButton() == MouseEvent.BUTTON1) {
  470. if (model.getPropertyTable().getRowCount() > 0) {
  471. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  472. model.getPropertyTable().removeRow(i);
  473. }
  474. }
  475. triggerUpdateController();
  476. }
  477. stopEditing();
  478. }
  479. @Override
  480. public void mouseEntered(MouseEvent e) {
  481. }
  482. @Override
  483. public void mouseExited(MouseEvent e) {
  484. }
  485. @Override
  486. public void mousePressed(MouseEvent e) {
  487. stopEditing();
  488. tempCps = null;
  489. edgeHighlight = null;
  490. controller.setSelecteEdge(null);
  491. // Object Selection
  492. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  493. cx = cps.getPosition().x - controller.getScaleDiv2();
  494. cy = cps.getPosition().y - controller.getScaleDiv2();
  495. if (x - controller.getScale() <= cx
  496. && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  497. tempCps = cps;
  498. setConsoleTextAfterSelect(cps);
  499. dragging = true;
  500. if (e.isControlDown() && tempCps != null) {
  501. if (model.getSelectedCpsObjects().contains(tempCps)) {
  502. controller.deleteSelectedObject(tempCps);
  503. //TODO: RemoveDepth
  504. } else {
  505. controller.addSelectedObject(tempCps);
  506. if(tempCps instanceof CpsUpperNode)
  507. controller.getObjectsInDepth();
  508. }
  509. }
  510. // If drawing an Edge (CTRL down)
  511. if (tempCps.getClass() == HolonObject.class) {
  512. HolonObject tempObj = ((HolonObject) tempCps);
  513. dataSelected = tempObj.getElements();
  514. }
  515. if (e.isShiftDown()) {
  516. drawEdge = true;
  517. dragging = false;
  518. }
  519. }
  520. }
  521. // Edge Selection
  522. if (tempCps == null) {
  523. edgeHighlight = mousePositionOnEdge(x, y);
  524. controller.setSelecteEdge(edgeHighlight);
  525. controller.setSelectedObjectID(0);
  526. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  527. model.getSelectedCpsObjects().clear();
  528. }
  529. }
  530. if (edgeHighlight == null && tempCps == null) {
  531. sx = e.getX();
  532. sy = e.getY();
  533. doMark = true;
  534. }
  535. repaint();
  536. }
  537. @Override
  538. public void mouseReleased(MouseEvent e) {
  539. x = e.getX();
  540. y = e.getY();
  541. dragging = false;
  542. if (drawEdge) {
  543. drawEdge = false;
  544. drawDeleteEdge();
  545. }
  546. if (dragged) {
  547. try {
  548. /**
  549. * Save before further Dragged interactions happen
  550. */
  551. controller.autoSave();
  552. } catch (IOException ex) {
  553. System.err.println("AutoSave error by dragging");
  554. ex.printStackTrace();
  555. }
  556. /**
  557. * check if a unique tempCps could replace an Object on the canvas
  558. */
  559. if(model.getSelectedCpsObjects().size()==1
  560. && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
  561. /**
  562. * if UpperNode would be replaced, close its tabs
  563. */
  564. if(mayBeReplaced instanceof CpsUpperNode)
  565. closeUpperNodeTab(mayBeReplaced.getId());
  566. /**
  567. * replace on canvas (will save)
  568. */
  569. controller.replaceCanvasObject(mayBeReplaced, tempCps);
  570. mayBeReplaced=null;
  571. }
  572. }
  573. if (!e.isControlDown() && !dragged && tempCps != null
  574. && MouseEvent.BUTTON3 != e.getButton()) {
  575. model.getSelectedCpsObjects().clear();
  576. controller.addSelectedObject(tempCps);
  577. model.setSelectedCpsObject(tempCps);
  578. if(tempCps instanceof CpsUpperNode)
  579. controller.getObjectsInDepth();
  580. }
  581. dragged = false;
  582. // Rightclick List
  583. setRightClickMenu(e);
  584. markObjects();
  585. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch
  586. && MouseEvent.BUTTON3 != e.getButton()) {
  587. ((HolonSwitch) tempCps).switchState();
  588. }
  589. controller.calculateStateForTimeStep(model.getCurIteration());
  590. triggerUpdateController();
  591. repaint();
  592. }
  593. @Override
  594. public void mouseDragged(MouseEvent e) {
  595. // If Edge is drawn
  596. x = e.getX();
  597. y = e.getY();
  598. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  599. model.getSelectedCpsObjects().clear();
  600. if (tempCps != null) {
  601. controller.addSelectedObject(tempCps);
  602. }
  603. }
  604. if (dragging) {
  605. try {
  606. dragged = true;
  607. float xDist, yDist; // Distance
  608. x = e.getX();
  609. y = e.getY();
  610. // Make sure its in bounds
  611. if (e.getX() < controller.getScaleDiv2())
  612. x = controller.getScaleDiv2();
  613. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  614. x = this.getWidth() - controller.getScaleDiv2();
  615. if (e.getY() < controller.getScaleDiv2())
  616. y = controller.getScaleDiv2();
  617. else if (e.getY() > this.getHeight()
  618. - controller.getScaleDiv2())
  619. y = this.getHeight() - controller.getScaleDiv2();
  620. // Distance
  621. xDist = x - tempCps.getPosition().x;
  622. yDist = y - tempCps.getPosition().y;
  623. tempCps.setPosition(x, y); // Drag Position
  624. // ToolTipText Position and name
  625. toolTip = true;
  626. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  627. toolTipPos.x = tempCps.getPosition().x
  628. - controller.getScaleDiv2();
  629. toolTipPos.y = tempCps.getPosition().y
  630. + controller.getScaleDiv2();
  631. // All Selected Objects
  632. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  633. if (cps != tempCps) {
  634. x = (int) (cps.getPosition().x + xDist);
  635. y = (int) (cps.getPosition().y + yDist);
  636. // Make sure its in bounds
  637. if (x <= controller.getScaleDiv2())
  638. x = controller.getScaleDiv2();
  639. else if (x > this.getWidth()
  640. - controller.getScaleDiv2())
  641. x = this.getWidth() - controller.getScaleDiv2();
  642. if (y <= controller.getScaleDiv2())
  643. y = controller.getScaleDiv2();
  644. else if (y > this.getHeight()
  645. - controller.getScaleDiv2())
  646. y = this.getHeight() - controller.getScaleDiv2();
  647. cps.setPosition(x, y);
  648. }
  649. }
  650. /**
  651. * check if something might be replaced
  652. */
  653. if(model.getSelectedCpsObjects().size()==1)
  654. checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
  655. repaint();
  656. } catch (Exception eex) {
  657. }
  658. }
  659. // Mark Objects
  660. if (doMark) {
  661. tempSelected.clear();
  662. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  663. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  664. if (sx >= x) {
  665. x1 = x;
  666. x2 = sx;
  667. }
  668. if (sy >= y) {
  669. y1 = y;
  670. y2 = sy;
  671. }
  672. if (x1 <= cps.getPosition().x + model.getScaleDiv2()
  673. && y1 <= cps.getPosition().y + model.getScaleDiv2()
  674. && x2 >= cps.getPosition().x
  675. && y2 >= cps.getPosition().y) {
  676. tempSelected.add(cps);
  677. }
  678. }
  679. }
  680. repaint();
  681. }
  682. @Override
  683. public void mouseMoved(MouseEvent e) {
  684. x = e.getX();
  685. y = e.getY();
  686. // Everything for the tooltip :)
  687. boolean on = false;
  688. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  689. cx = cps.getPosition().x - controller.getScaleDiv2();
  690. cy = cps.getPosition().y - controller.getScaleDiv2();
  691. on = setToolTipInfoAndPosition(on, cps);
  692. }
  693. toolTip = on;
  694. repaint();
  695. }
  696. /**
  697. * Draws or Deletes an Edge.
  698. */
  699. void drawDeleteEdge() {
  700. if (getMousePosition() != null) {
  701. boolean node = true;
  702. boolean newEdge = true;
  703. boolean onEdge = true;
  704. boolean deleteNode = false;
  705. CpsEdge e = null;
  706. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  707. cx = cps.getPosition().x - controller.getScaleDiv2();
  708. cy = cps.getPosition().y - controller.getScaleDiv2();
  709. if (x - controller.getScale() <= cx
  710. && y - controller.getScale() <= cy && x >= cx
  711. && y >= cy && cps != tempCps) {
  712. node = false;
  713. onEdge = false;
  714. for (CpsEdge p : tempCps.getConnections()) {
  715. if ((p.getA() == tempCps && p.getB() == cps)
  716. || (p.getB() == tempCps && p.getA() == cps)) {
  717. newEdge = false;
  718. e = p;
  719. }
  720. }
  721. if (!newEdge) {
  722. controller.removeEdgesOnCanvas(e);
  723. // Node ohne Edge?
  724. if (e.getA().getClass() == CpsNode.class
  725. && e.getA().getConnections().isEmpty()) {
  726. tempCps = e.getA();
  727. deleteNode = true;
  728. }
  729. if (e.getB().getClass() == CpsNode.class
  730. && e.getB().getConnections().isEmpty()) {
  731. deleteNode = true;
  732. }
  733. } else {
  734. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  735. controller.addEdgeOnCanvas(e);
  736. }
  737. }
  738. }
  739. // Edge auf eine Edge gezogen?
  740. if (onEdge && !checkForReplacement(x, y)) {
  741. CpsEdge p = mousePositionOnEdge(x, y);
  742. if (p != null) {
  743. CpsEdge e1;
  744. CpsEdge e2;
  745. node = false;
  746. CpsNode n = new CpsNode("Node");
  747. n.setPosition(x, y);
  748. controller.addObjectCanvas(n);
  749. AbstractCpsObject r, k;
  750. r = p.getA();
  751. k = p.getB();
  752. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  753. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  754. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  755. controller.removeEdgesOnCanvas(p);
  756. controller.addEdgeOnCanvas(e);
  757. controller.addEdgeOnCanvas(e1);
  758. controller.addEdgeOnCanvas(e2);
  759. }
  760. }else{
  761. mayBeReplaced = null;
  762. }
  763. // ins leere Gedragged
  764. if (node && !checkForReplacement(x, y)) {
  765. CpsNode n = new CpsNode("Node");
  766. n.setPosition(x, y);
  767. controller.addObjectCanvas(n);
  768. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  769. controller.addEdgeOnCanvas(e);
  770. }else{
  771. mayBeReplaced = null;
  772. }
  773. // Wenn ein Node ohne Connections da ist
  774. if (deleteNode) {
  775. controller.delCanvasObject(tempCps, true);
  776. tempCps = null;
  777. }
  778. }
  779. }
  780. /**
  781. * Checks if the mouse is on an Edge.
  782. *
  783. * @param x
  784. * Position of the Mouse
  785. * @param y
  786. * Position of the Mouse
  787. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  788. */
  789. private CpsEdge mousePositionOnEdge(int x, int y) {
  790. x += controller.getScaleDiv2();
  791. y += controller.getScaleDiv2();
  792. for (CpsEdge p : model.getEdgesOnCanvas()) {
  793. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA()
  794. .getPosition().y, p.getB().getPosition().x, p.getB()
  795. .getPosition().y);
  796. int[] positions = determineMousePositionOnEdge(p);
  797. int lx = positions[0];
  798. int ly = positions[1];
  799. int hx = positions[2];
  800. int hy = positions[3];
  801. // distance from a point to a line and between both Objects
  802. if (l.ptLineDistSq(x - model.getScaleDiv2(),
  803. y - model.getScaleDiv2()) < 20
  804. && x > lx && x < hx && y > ly && y < hy) {
  805. return p;
  806. }
  807. }
  808. return null;
  809. }
  810. void updateLanguages() {
  811. itemCut.setText(Languages.getLanguage()[95]);
  812. itemCopy.setText(Languages.getLanguage()[96]);
  813. itemPaste.setText(Languages.getLanguage()[97]);
  814. itemDelete.setText(Languages.getLanguage()[98]);
  815. itemGroup.setText(Languages.getLanguage()[99]);
  816. itemUngroup.setText(Languages.getLanguage()[100]);
  817. itemTrack.setText(Languages.getLanguage()[101]);
  818. itemUntrack.setText(Languages.getLanguage()[102]);
  819. }
  820. /**
  821. * Set if Information should be shown.
  822. *
  823. * @param connection
  824. * boolean for conecction
  825. * @param object
  826. * boolean for objects
  827. * @param nodeOfnode
  828. */
  829. void setShowedInformation(boolean connection, boolean object,
  830. boolean border, boolean nodeOfnode) {
  831. showedInformation[0] = connection;
  832. showedInformation[1] = object;
  833. showedInformation[3] = border;
  834. showedInformation[4] = nodeOfnode;
  835. }
  836. /**
  837. * Returns if Information should be shown.
  838. *
  839. * @return Array of boolean [0] = connection, [1] = objects
  840. */
  841. boolean[] getShowedInformation() {
  842. return showedInformation;
  843. }
  844. /**
  845. * set toolTip
  846. *
  847. * @param bool
  848. */
  849. void setToolTip(boolean bool) {
  850. this.toolTip = bool;
  851. }
  852. /**
  853. * Set the Mouse
  854. *
  855. * @param x
  856. * @param y
  857. */
  858. void setXY(int x, int y) {
  859. this.x = x;
  860. this.y = y;
  861. }
  862. @Override
  863. public boolean checkForReplacement(int x, int y) {
  864. return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
  865. }
  866. @Override
  867. public void tryToAlignObjects(){
  868. /**
  869. * Align all Objects
  870. */
  871. for(AbstractCpsObject cps: model.getObjectsOnCanvas())
  872. align(cps,3*model.getScaleDiv2());
  873. /**
  874. * AutoSave new Positons
  875. */
  876. try{
  877. controller.autoSave();
  878. } catch (IOException ex) {
  879. System.err.println("AutoSave error by aligning");
  880. ex.printStackTrace();
  881. }
  882. }
  883. @Override
  884. public void closeUpperNodeTab(int upperNodeId) {
  885. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
  886. .getParent().getParent().getParent();
  887. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  888. if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInner
  889. .getComponentAt(i)).getViewport().getComponent(
  890. 0)).upperNode.getId() == upperNodeId) {
  891. tabbedPaneInner.remove(i);
  892. break;
  893. }
  894. }
  895. }
  896. }