MyCanvas.java 28 KB

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