MyCanvas.java 29 KB

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