UpperNodeCanvas.java 31 KB

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