MyCanvas.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.Image;
  7. import java.awt.Point;
  8. import java.awt.RenderingHints;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import java.awt.event.MouseMotionListener;
  14. import java.awt.geom.Line2D;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.util.ArrayList;
  18. import java.util.Timer;
  19. import java.util.TimerTask;
  20. import javax.swing.ImageIcon;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JPanel;
  23. import javax.swing.JPopupMenu;
  24. import javax.swing.JScrollPane;
  25. import javax.swing.JTabbedPane;
  26. import javax.swing.JToolTip;
  27. import classes.CpsEdge;
  28. import classes.CpsNode;
  29. import classes.CpsUpperNode;
  30. import classes.AbstractCpsObject;
  31. import classes.HolonElement;
  32. import classes.HolonObject;
  33. import classes.HolonSwitch;
  34. import classes.SubNet;
  35. import ui.controller.Control;
  36. import ui.model.Model;
  37. /**
  38. * This Class is the Canvas. All Objects will be visualized here
  39. *
  40. * @author Gruppe14
  41. */
  42. public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
  43. private static final long serialVersionUID = 1L;
  44. private Image img = null; // Contains the image to draw on MyCanvas
  45. private int x = 0;
  46. private int y = 0;
  47. // edge Object Start Point
  48. private Model model;
  49. private final Control controller;
  50. Graphics2D g2; // For Painting
  51. private int cx, cy;
  52. private int sx, sy; // Mark Coords
  53. private float scalediv20;
  54. ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
  55. ArrayList<AbstractCpsObject> tempSelected = new ArrayList<AbstractCpsObject>();
  56. private boolean[] showedInformation = new boolean[3];
  57. private boolean dragging = false; // for dragging
  58. private boolean dragged = false; // if an object/objects was/were dragged
  59. private boolean drawEdge = false; // for drawing edges
  60. public boolean click = false; // for double click
  61. private boolean doMark = false; // for double click
  62. public AbstractCpsObject tempCps = null;
  63. private CpsEdge edgeHighlight = null;
  64. // PopUpMenu
  65. private JPopupMenu popmenu = new JPopupMenu();
  66. private JMenuItem itemDelete = new JMenuItem("Delete");
  67. private JMenuItem itemCut = new JMenuItem("Cut");
  68. private JMenuItem itemCopy = new JMenuItem("Copy");
  69. public JMenuItem itemPaste = new JMenuItem("Paste");
  70. public JMenuItem itemGroup = new JMenuItem("Group");
  71. public JMenuItem itemUngroup = new JMenuItem("Ungroup");
  72. public JMenuItem itemTrack = new JMenuItem("Track");
  73. public JMenuItem itemUntrack = new JMenuItem("Untrack");
  74. private JToolTip objectTT = new JToolTip();
  75. private Point mousePosition = new Point(); // Mouse Position when
  76. // rightclicked
  77. // contains the value of the Capacity for new created Edges
  78. /**
  79. * Constructor.
  80. *
  81. * @param mod
  82. * the Model
  83. * @param control
  84. * the Controller
  85. */
  86. public MyCanvas(Model mod, Control control) {
  87. this.add(objectTT);
  88. this.controller = control;
  89. this.model = mod;
  90. scalediv20 = model.getScale() / 20;
  91. showedInformation[0] = true;
  92. showedInformation[1] = true;
  93. control.setMaxCapacity(10000);
  94. popmenu.add(itemCut);
  95. popmenu.add(itemCopy);
  96. popmenu.add(itemPaste);
  97. popmenu.add(itemDelete);
  98. popmenu.addSeparator();
  99. popmenu.add(itemGroup);
  100. popmenu.add(itemUngroup);
  101. popmenu.add(itemTrack);
  102. popmenu.add(itemUntrack);
  103. itemDelete.setEnabled(false);
  104. itemCut.setEnabled(false);
  105. itemCopy.setEnabled(false);
  106. itemPaste.setEnabled(false);
  107. itemGroup.setEnabled(false);
  108. itemUngroup.setEnabled(false);
  109. itemTrack.setEnabled(false);
  110. itemUntrack.setEnabled(false);
  111. itemGroup.addActionListener(new ActionListener() {
  112. @Override
  113. public void actionPerformed(ActionEvent e) {
  114. controller.addUpperNode("NodeOfNode", null);
  115. repaint();
  116. }
  117. });
  118. itemUngroup.addActionListener(new ActionListener() {
  119. @Override
  120. public void actionPerformed(ActionEvent e) {
  121. controller.delUpperNode((CpsUpperNode) tempCps, null);
  122. repaint();
  123. }
  124. });
  125. itemTrack.addActionListener(new ActionListener() {
  126. @Override
  127. public void actionPerformed(ActionEvent e) {
  128. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  129. if (o instanceof HolonObject) {
  130. boolean found = false;
  131. if (controller.getTrackingObj() != null) {
  132. for (HolonObject obj : controller.getTrackingObj()) {
  133. if (obj.getID() == o.getID()) {
  134. found = true;
  135. }
  136. }
  137. }
  138. if (!found) {
  139. controller.addTrackingObj((HolonObject) o);
  140. ((HolonObject) o).updateTrackingInfo();
  141. }
  142. }
  143. }
  144. System.out.println(controller.getTrackingObj());
  145. }
  146. });
  147. itemUntrack.addActionListener(new ActionListener() {
  148. @Override
  149. public void actionPerformed(ActionEvent e) {
  150. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  151. if (o instanceof HolonObject) {
  152. boolean found = false;
  153. if (controller.getTrackingObj() != null) {
  154. for (HolonObject obj : controller.getTrackingObj()) {
  155. if (obj.getID() == o.getID()) {
  156. found = true;
  157. }
  158. }
  159. }
  160. if (found) {
  161. // Removed from tracking array and tracking
  162. // information reseted
  163. controller.removeTrackingObj((HolonObject) o);
  164. ((HolonObject) o).setTrackingProd(new float[100]);
  165. ((HolonObject) o).setTrackingCons(new float[100]);
  166. }
  167. }
  168. }
  169. System.out.println(controller.getTrackingObj());
  170. }
  171. });
  172. itemDelete.addActionListener(new ActionListener() {
  173. @Override
  174. public void actionPerformed(ActionEvent e) {
  175. // Remove the selected Object objects
  176. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  177. controller.delCanvasObject(cps);
  178. // Remove UpperNodeTab if UpperNode deleted
  179. if(cps instanceof CpsUpperNode){
  180. JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
  181. for (int i = 2; i < tabbedPane.getTabCount(); i++) {
  182. if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport().getComponent(0)).upperNode
  183. .getID() == cps.getID()) {
  184. tabbedPane.remove(i);
  185. i = tabbedPane.getTabCount();
  186. }
  187. }
  188. }
  189. }
  190. model.getSelectedCpsObjects().clear();
  191. tempCps = null;
  192. repaint();
  193. }
  194. });
  195. itemCut.addActionListener(new ActionListener() {
  196. @Override
  197. public void actionPerformed(ActionEvent e) {
  198. controller.cutObjects();
  199. itemPaste.setEnabled(true);
  200. repaint();
  201. }
  202. });
  203. itemCopy.addActionListener(new ActionListener() {
  204. @Override
  205. public void actionPerformed(ActionEvent e) {
  206. controller.copyObjects();
  207. itemPaste.setEnabled(true);
  208. repaint();
  209. }
  210. });
  211. itemPaste.addActionListener(new ActionListener() {
  212. @Override
  213. public void actionPerformed(ActionEvent e) {
  214. controller.pasteObjects(mousePosition);
  215. repaint();
  216. }
  217. });
  218. this.addMouseListener(this);
  219. this.addMouseMotionListener(this);
  220. }
  221. /**
  222. * Paints all Components on the Canvas.
  223. *
  224. * @param g
  225. * Graphics
  226. */
  227. public void paintComponent(Graphics g) {
  228. String maxCap;
  229. super.paintComponent(g);
  230. // Rendering
  231. g2 = (Graphics2D) g;
  232. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  233. g2.setRenderingHints(rh);
  234. // Test SubNet Coloring
  235. int i = 0;
  236. for (SubNet s : controller.getSimManager().getSubNets()) {
  237. if (model.getSubNetColors().size() - 1 < i) {
  238. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  239. (int) (Math.random() * 255)));
  240. }
  241. for (HolonObject cps : s.getObjects()) {
  242. cps.setBorderColor(model.getSubNetColors().get(i));
  243. }
  244. i++;
  245. }
  246. // drawEdges that is being dragged
  247. if (drawEdge) {
  248. g2.setColor(Color.BLACK);
  249. g2.setStroke(new BasicStroke(2));
  250. g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
  251. tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
  252. }
  253. for (CpsEdge con : model.getEdgesOnCanvas()) {
  254. if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID()
  255. && con != edgeHighlight) {
  256. if (con.getState()) {
  257. g2.setColor(Color.GREEN);
  258. if (con.getCapacity() != -1) {
  259. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  260. }
  261. } else {
  262. g2.setColor(Color.RED);
  263. g2.setStroke(new BasicStroke(2));
  264. }
  265. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  266. con.getA().getPosition().y + controller.getScaleDiv2(),
  267. con.getB().getPosition().x + controller.getScaleDiv2(),
  268. con.getB().getPosition().y + controller.getScaleDiv2());
  269. if (con.getCapacity() == -1) {
  270. maxCap = Character.toString('\u221e');
  271. } else {
  272. maxCap = String.valueOf(con.getCapacity());
  273. }
  274. if (showedInformation[0]) {
  275. g2.drawString(con.getFlow() + "/" + maxCap,
  276. (con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
  277. (con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
  278. }
  279. }
  280. }
  281. // Highlighted Edge
  282. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  283. g2.setColor(Color.BLUE);
  284. for (CpsEdge con : model.getEdgesOnCanvas()) {
  285. if (con.getFlow() <= con.getCapacity()) {
  286. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  287. } else {
  288. g2.setStroke(new BasicStroke(2));
  289. }
  290. if (con.getA().getID() == model.getSelectedObjectID()
  291. || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
  292. || con.getB().getID() == model.getSelectedObjectID()
  293. || model.getSelectedCpsObjects().contains(con.getB())
  294. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  295. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  296. con.getA().getPosition().y + controller.getScaleDiv2(),
  297. con.getB().getPosition().x + controller.getScaleDiv2(),
  298. con.getB().getPosition().y + controller.getScaleDiv2());
  299. if (con.getCapacity() == -1) {
  300. maxCap = Character.toString('\u221e');
  301. } else {
  302. maxCap = String.valueOf(con.getCapacity());
  303. }
  304. if (showedInformation[0]) {
  305. g2.drawString(con.getFlow() + "/" + maxCap,
  306. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  307. + controller.getScaleDiv2(),
  308. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  309. + controller.getScaleDiv2());
  310. }
  311. }
  312. }
  313. } else if (edgeHighlight != null) {
  314. g2.setColor(Color.BLUE);
  315. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  316. g2.setStroke(new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 4), 4)));
  317. } else {
  318. g2.setStroke(new BasicStroke(2));
  319. }
  320. g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
  321. edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
  322. edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
  323. edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
  324. if (edgeHighlight.getCapacity() == -1) {
  325. maxCap = Character.toString('\u221e');
  326. } else {
  327. maxCap = String.valueOf(edgeHighlight.getCapacity());
  328. }
  329. if (showedInformation[0]) {
  330. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  331. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
  332. + controller.getScaleDiv2(),
  333. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
  334. + controller.getScaleDiv2());
  335. }
  336. }
  337. // Objects
  338. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  339. // Border Highlighting
  340. g2.setColor(cps.getBorderColor());
  341. if (g2.getColor() != Color.WHITE) {
  342. g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3), (int) (cps.getPosition().y - scalediv20 - 3),
  343. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  344. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  345. }
  346. // node image
  347. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  348. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  349. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  350. } else {
  351. if (cps instanceof HolonSwitch) {
  352. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  353. ((HolonSwitch) cps).setAutoState(true);
  354. } else {
  355. ((HolonSwitch) cps).setAutoState(false);
  356. }
  357. }
  358. // Highlighting
  359. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  360. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  361. g2.setColor(Color.BLUE);
  362. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  363. (int) (controller.getScale() + (scalediv20 * 2)),
  364. (int) (controller.getScale() + (scalediv20 * 2)));
  365. if (showedInformation[1] && cps instanceof HolonObject) {
  366. g2.setColor(Color.BLACK);
  367. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  368. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  369. }
  370. } else if (cps instanceof HolonObject) {
  371. g2.setColor(((HolonObject) cps).getColor());
  372. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  373. (int) (controller.getScale() + (scalediv20 * 2)),
  374. (int) (controller.getScale() + (scalediv20 * 2)));
  375. if (showedInformation[1]) {
  376. g2.setColor(Color.BLACK);
  377. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  378. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  379. }
  380. }
  381. // draw image
  382. File checkPath = new File(cps.getImage());
  383. if (checkPath.exists()) {
  384. img = new ImageIcon(cps.getImage()).getImage();
  385. } else {
  386. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  387. }
  388. }
  389. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  390. null);
  391. }
  392. // Dragg Highlighting
  393. if (doMark) {
  394. g2.setColor(Color.BLACK);
  395. g2.setStroke(new BasicStroke(1));
  396. if (sx > x && sy > y) {
  397. g2.drawRect(x, y, sx - x, sy - y);
  398. } else if (sx < x && sy < y) {
  399. g2.drawRect(sx, sy, x - sx, y - sy);
  400. } else if (sx >= x) {
  401. g2.drawRect(x, sy, sx - x, y - sy);
  402. } else if (sy >= y) {
  403. g2.drawRect(sx, y, x - sx, sy - y);
  404. }
  405. }
  406. }
  407. @Override
  408. public void mouseClicked(MouseEvent e) {
  409. }
  410. @Override
  411. public void mouseEntered(MouseEvent e) {
  412. }
  413. @Override
  414. public void mouseExited(MouseEvent e) {
  415. }
  416. @Override
  417. public void mousePressed(MouseEvent e) {
  418. tempCps = null;
  419. dataSelected = null;
  420. edgeHighlight = null;
  421. controller.setSelecteEdge(null);
  422. // Object Selection
  423. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  424. cx = cps.getPosition().x;
  425. cy = cps.getPosition().y;
  426. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  427. tempCps = cps;
  428. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  429. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  430. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  431. controller.addTextToConsole("" + cps.getID(), Color.RED, 12, true, false, true);
  432. dragging = true;
  433. controller.setSelectedObjectID(tempCps.getID());
  434. // If drawing an Edge (CTRL down)
  435. if (tempCps.getClass() == HolonObject.class) {
  436. HolonObject tempObj = ((HolonObject) tempCps);
  437. dataSelected = tempObj.getElements();
  438. }
  439. if (e.isShiftDown()) {
  440. drawEdge = true;
  441. dragging = false;
  442. }
  443. }
  444. }
  445. // Edge Selection
  446. if (tempCps == null) {
  447. edgeHighlight = mousePositionOnEdge(x, y);
  448. controller.setSelecteEdge(edgeHighlight);
  449. controller.setSelectedObjectID(0);
  450. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  451. model.getSelectedCpsObjects().clear();
  452. }
  453. }
  454. if (edgeHighlight == null && tempCps == null) {
  455. sx = e.getX();
  456. sy = e.getY();
  457. doMark = true;
  458. }
  459. repaint();
  460. }
  461. @Override
  462. public void mouseReleased(MouseEvent e) {
  463. dragging = false;
  464. if (drawEdge) {
  465. drawEdge = false;
  466. drawDeleteEdge();
  467. }
  468. if (dragged == true) {
  469. try {
  470. controller.autoSave();
  471. } catch (IOException ex) {
  472. // TODO Auto-generated catch block
  473. ex.printStackTrace();
  474. }
  475. }
  476. if (!e.isControlDown() && dragged == false && tempCps != null && e.BUTTON3 != e.getButton()) {
  477. model.getSelectedCpsObjects().clear();
  478. controller.addSelectedObject(tempCps);
  479. }
  480. dragged = false;
  481. // Rightclick List
  482. if (e.getButton() == MouseEvent.BUTTON3) {
  483. if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
  484. itemDelete.setEnabled(true);
  485. itemCut.setEnabled(true);
  486. itemCopy.setEnabled(true);
  487. if (tempCps != null)
  488. itemGroup.setEnabled(true);
  489. if (tempCps instanceof CpsUpperNode)
  490. itemUngroup.setEnabled(true);
  491. else
  492. itemUngroup.setEnabled(false);
  493. if (!(tempCps instanceof HolonSwitch)) {
  494. itemTrack.setEnabled(true);
  495. itemUntrack.setEnabled(true);
  496. } else {
  497. itemTrack.setEnabled(false);
  498. itemUntrack.setEnabled(false);
  499. }
  500. if (model.getSelectedCpsObjects().size() == 0) {
  501. controller.addSelectedObject(tempCps);
  502. }
  503. } else {
  504. itemCut.setEnabled(false);
  505. itemCopy.setEnabled(false);
  506. itemDelete.setEnabled(false);
  507. itemGroup.setEnabled(false);
  508. itemUngroup.setEnabled(false);
  509. itemTrack.setEnabled(false);
  510. itemUntrack.setEnabled(false);
  511. }
  512. mousePosition = this.getMousePosition();
  513. popmenu.show(e.getComponent(), e.getX(), e.getY());
  514. }
  515. if (doMark) {
  516. doMark = false;
  517. for (AbstractCpsObject cps : tempSelected) {
  518. if (!model.getSelectedCpsObjects().contains(cps)) {
  519. controller.addSelectedObject(cps);
  520. }
  521. }
  522. tempSelected.clear();
  523. }
  524. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch) {
  525. ((HolonSwitch) tempCps).switchState();
  526. }
  527. controller.calculateStateForTimeStep(model.getCurIteration());
  528. repaint();
  529. }
  530. @Override
  531. public void mouseDragged(MouseEvent e) {
  532. // If Edge is drawn
  533. x = e.getX();
  534. y = e.getY();
  535. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  536. model.getSelectedCpsObjects().clear();
  537. if (tempCps != null) {
  538. controller.addSelectedObject(tempCps);
  539. }
  540. }
  541. if (dragging) {
  542. try {
  543. dragged = true;
  544. float xDist, yDist; // Distance
  545. x = e.getX() - controller.getScaleDiv2();
  546. y = e.getY() - controller.getScaleDiv2();
  547. // Make sure its in bounds
  548. if (e.getX() < controller.getScaleDiv2())
  549. x = 0;
  550. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  551. x = this.getWidth() - controller.getScale();
  552. if (e.getY() < controller.getScaleDiv2())
  553. y = 0;
  554. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  555. y = this.getHeight() - controller.getScale();
  556. // Distance
  557. xDist = x - tempCps.getPosition().x;
  558. yDist = y - tempCps.getPosition().y;
  559. tempCps.setPosition(x, y); // Drag Position
  560. // TipText Position and name
  561. objectTT.setTipText(tempCps.getName() + ", " + tempCps.getID());
  562. objectTT.setLocation(x, y + controller.getScale());
  563. // All Selected Objects
  564. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  565. if (cps != tempCps) {
  566. x = (int) (cps.getPosition().x + xDist);
  567. y = (int) (cps.getPosition().y + yDist);
  568. // Make sure its in bounds
  569. if (x <= 0)
  570. x = 0;
  571. else if (x > this.getWidth() - controller.getScale())
  572. x = this.getWidth() - controller.getScale();
  573. if (y <= 0)
  574. y = 0;
  575. else if (y > this.getHeight() - controller.getScale())
  576. y = this.getHeight() - controller.getScale();
  577. cps.setPosition(x, y);
  578. }
  579. }
  580. repaint();
  581. } catch (Exception eex) {
  582. }
  583. }
  584. // Mark Objects
  585. if (doMark) {
  586. tempSelected.clear();
  587. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  588. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  589. if (sx >= x) {
  590. x1 = x;
  591. x2 = sx;
  592. }
  593. if (sy >= y) {
  594. y1 = y;
  595. y2 = sy;
  596. }
  597. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  598. && x2 >= cps.getPosition().x + model.getScaleDiv2()
  599. && y2 >= cps.getPosition().y + model.getScaleDiv2()) {
  600. tempSelected.add(cps);
  601. }
  602. }
  603. }
  604. repaint();
  605. }
  606. @Override
  607. public void mouseMoved(MouseEvent e) {
  608. x = e.getX();
  609. y = e.getY();
  610. // Everytghing for the tooltip :)
  611. boolean on = false;
  612. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  613. cx = cps.getPosition().x;
  614. cy = cps.getPosition().y;
  615. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  616. objectTT.setTipText(cps.getName() + ", " + cps.getID());
  617. objectTT.setLocation(cx, cy + controller.getScale());
  618. on = true;
  619. }
  620. }
  621. if (!on) {
  622. objectTT.setLocation(-200, -200);
  623. objectTT.setTipText("");
  624. }
  625. }
  626. /**
  627. * Draws or Deletes an Edge.
  628. */
  629. private void drawDeleteEdge() {
  630. boolean node = true;
  631. boolean newEdge = true;
  632. boolean onEdge = true;
  633. boolean deleteNode = false;
  634. CpsEdge e = null;
  635. AbstractCpsObject tempCPS = null;
  636. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  637. cx = cps.getPosition().x;
  638. cy = cps.getPosition().y;
  639. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  640. && cps != tempCps) {
  641. node = false;
  642. onEdge = false;
  643. for (CpsEdge p : tempCps.getConnections()) {
  644. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  645. newEdge = false;
  646. e = p;
  647. }
  648. }
  649. if (!newEdge) {
  650. controller.removeEdgesOnCanvas(e);
  651. // Node ohne Edge?
  652. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  653. tempCps = e.getA();
  654. deleteNode = true;
  655. }
  656. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  657. tempCPS = e.getB();
  658. deleteNode = true;
  659. }
  660. }
  661. if (newEdge) {
  662. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  663. controller.addEdgeOnCanvas(e);
  664. }
  665. }
  666. }
  667. // Edge auf eine Edge gezogen?
  668. if (onEdge) {
  669. CpsEdge p = mousePositionOnEdge(x, y);
  670. if (p != null) {
  671. CpsEdge e1 = null;
  672. CpsEdge e2 = null;
  673. node = false;
  674. CpsNode n = new CpsNode("Node");
  675. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  676. controller.addObjectCanvas(n);
  677. AbstractCpsObject r, k;
  678. r = p.getA();
  679. k = p.getB();
  680. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  681. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  682. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  683. controller.removeEdgesOnCanvas(p);
  684. controller.addEdgeOnCanvas(e);
  685. controller.addEdgeOnCanvas(e1);
  686. controller.addEdgeOnCanvas(e2);
  687. }
  688. }
  689. // ins leere Gedragged
  690. if (node) {
  691. CpsNode n = new CpsNode("Node");
  692. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  693. controller.addObjectCanvas(n);
  694. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  695. controller.addEdgeOnCanvas(e);
  696. }
  697. // Wenn ein Node ohne Connections da ist
  698. if (deleteNode) {
  699. controller.delCanvasObject(tempCps);
  700. tempCps = null;
  701. }
  702. }
  703. /**
  704. * Checks if the mouse is on an Edge.
  705. *
  706. * @param x
  707. * Position of the Mouse
  708. * @param y
  709. * Position of the Mouse
  710. *
  711. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  712. */
  713. public CpsEdge mousePositionOnEdge(int x, int y) {
  714. int lx, ly, hx, hy;
  715. for (CpsEdge p : model.getEdgesOnCanvas()) {
  716. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  717. p.getB().getPosition().y);
  718. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  719. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  720. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  721. } else {
  722. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  723. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  724. }
  725. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  726. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  727. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  728. } else {
  729. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  730. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  731. }
  732. // distance from a point to a line and between both Objects
  733. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  734. && y < hy) {
  735. return p;
  736. }
  737. }
  738. return null;
  739. }
  740. /**
  741. * Checks if a double click was made.
  742. *
  743. * @return
  744. *
  745. * @return true if doublecklick, false if not
  746. */
  747. private boolean doubleClick() {
  748. if (click) {
  749. click = false;
  750. return true;
  751. } else {
  752. click = true;
  753. Timer t = new Timer("doubleclickTimer", false);
  754. t.schedule(new TimerTask() {
  755. @Override
  756. public void run() {
  757. click = false;
  758. }
  759. }, 500);
  760. }
  761. return false;
  762. }
  763. /**
  764. * Set if Information should be shown.
  765. *
  766. * @param connection
  767. * boolean for conecction
  768. * @param object
  769. * boolean for objects
  770. */
  771. public void setShowedInformation(boolean connection, boolean object) {
  772. showedInformation[0] = connection;
  773. showedInformation[1] = object;
  774. }
  775. /**
  776. * Returns if Information should be shown.
  777. *
  778. * @return Array of boolean [0] = connection, [1] = objects
  779. */
  780. public boolean[] getShowedInformation() {
  781. return showedInformation;
  782. }
  783. }