MyCanvas.java 27 KB

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