UpperNodeCanvas.java 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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.datatransfer.UnsupportedFlavorException;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.awt.geom.Line2D;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Timer;
  20. import java.util.TimerTask;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JLabel;
  23. import javax.swing.JMenuItem;
  24. import javax.swing.JOptionPane;
  25. import javax.swing.JPanel;
  26. import javax.swing.JPopupMenu;
  27. import javax.swing.JScrollPane;
  28. import javax.swing.JTabbedPane;
  29. import com.google.gson.JsonParseException;
  30. import classes.CpsEdge;
  31. import classes.CpsNode;
  32. import classes.CpsUpperNode;
  33. import classes.AbstractCpsObject;
  34. import classes.HolonElement;
  35. import classes.HolonObject;
  36. import classes.HolonSwitch;
  37. import classes.Position;
  38. import classes.SubNet;
  39. import ui.controller.Control;
  40. import ui.controller.UpdateController;
  41. import ui.model.Model;
  42. /**
  43. * This Class is the Canvas. All Objects will be visualized here
  44. *
  45. * @author Gruppe14
  46. */
  47. public class UpperNodeCanvas extends JPanel implements MouseListener, MouseMotionListener {
  48. private static final long serialVersionUID = 1L;
  49. private Image img = null; // Contains the image to draw on MyCanvas
  50. private int x = 0;
  51. private int y = 0;
  52. // edge Object Start Point
  53. private Model model;
  54. private final Control controller;
  55. Graphics2D g2; // For Painting
  56. private int cx, cy;
  57. private int sx, sy; // Mark Coords
  58. private float scalediv20;
  59. public int borderPos = 0; // Border Position
  60. // Path
  61. public String path;
  62. private JLabel breadCrumb;
  63. ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
  64. ArrayList<AbstractCpsObject> tempSelected = new ArrayList<AbstractCpsObject>();
  65. // The UpperNode
  66. public CpsUpperNode upperNode;
  67. private boolean[] showedInformation = new boolean[3];
  68. private boolean dragging = false; // for dragging
  69. private boolean dragged = false; // if an object/objects was/were dragged
  70. private boolean drawEdge = false; // for drawing edges
  71. private boolean click = false; // for double click
  72. private boolean doMark = false; // for double click
  73. public AbstractCpsObject tempCps = null;
  74. private CpsEdge edgeHighlight = null;
  75. // PopUpMenu
  76. private JPopupMenu popmenu = new JPopupMenu();
  77. private JMenuItem itemDelete = new JMenuItem("Delete");
  78. private JMenuItem itemCut = new JMenuItem("Cut");
  79. private JMenuItem itemCopy = new JMenuItem("Copy");
  80. public JMenuItem itemPaste = new JMenuItem("Paste");
  81. public JMenuItem itemGroup = new JMenuItem("Group");
  82. public JMenuItem itemUngroup = new JMenuItem("Ungroup");
  83. public JMenuItem itemTrack = new JMenuItem("Track");
  84. public JMenuItem itemUntrack = new JMenuItem("Untrack");
  85. // Tooltip
  86. private boolean toolTip; // Tooltip on or off
  87. private Position toolTipPos = new Position(); // Tooltip Position
  88. private String toolTipText = "";
  89. private Point mousePosition = new Point(); // Mouse Position when
  90. // rightclicked
  91. // contains the value of the Capacity for new created Edges
  92. private UpdateController updCon;
  93. // Animation Stuff
  94. javax.swing.Timer animT; // animation Timer
  95. private final int ANIMTIME = 500; // animation Time
  96. private ArrayList<AbstractCpsObject> animCps = null;
  97. private int animFPS = 60;
  98. private int animDuration = ANIMTIME; // animation Duration
  99. private int animDelay = 1000 / animFPS; // animation Delay
  100. private int animSteps = animDuration / animDelay; // animation Steps;
  101. private Position unPos;
  102. private ArrayList<Position> savePos;
  103. /**
  104. * Constructor.
  105. *
  106. * @param mod
  107. * the Model
  108. * @param control
  109. * the Controller
  110. */
  111. public UpperNodeCanvas(Model mod, Control control, CpsUpperNode UpperNode, String parentPath) {
  112. toolTip = false;
  113. this.controller = control;
  114. this.model = mod;
  115. this.upperNode = UpperNode;
  116. this.path = parentPath + upperNode.getName();
  117. this.breadCrumb = new JLabel(path);
  118. // this.add(breadCrumb);
  119. scalediv20 = model.getScale() / 20;
  120. // Cps objecte aus dem border links schieben
  121. borderPos = (int) (model.getScale() + controller.getScaleDiv2() + scalediv20 + scalediv20 + 10);
  122. for (AbstractCpsObject cps : upperNode.getNodes()) {
  123. if (cps.getPosition().x < model.getScaleDiv2() + borderPos + 5) {
  124. cps.setPosition(new Position(borderPos + 5, cps.getPosition().y));
  125. }
  126. }
  127. showedInformation[0] = true;
  128. showedInformation[1] = true;
  129. popmenu.add(itemCut);
  130. popmenu.add(itemCopy);
  131. popmenu.add(itemPaste);
  132. popmenu.add(itemDelete);
  133. popmenu.addSeparator();
  134. popmenu.add(itemGroup);
  135. popmenu.add(itemUngroup);
  136. popmenu.add(itemTrack);
  137. popmenu.add(itemUntrack);
  138. itemDelete.setEnabled(false);
  139. itemCut.setEnabled(false);
  140. itemCopy.setEnabled(false);
  141. itemPaste.setEnabled(true);
  142. itemGroup.setEnabled(false);
  143. itemUngroup.setEnabled(false);
  144. itemTrack.setEnabled(false);
  145. itemUntrack.setEnabled(false);
  146. updCon = new UpdateController(model, controller);
  147. itemGroup.addActionListener(new ActionListener() {
  148. @Override
  149. public void actionPerformed(ActionEvent e) {
  150. // calculate uppernode pos (taken from the controller)
  151. unPos = new Position(0, 0);
  152. animCps = new ArrayList<>();
  153. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  154. animCps.add(cps); // add to animation Cps ArrayList
  155. unPos.x += cps.getPosition().x;
  156. unPos.y += cps.getPosition().y;
  157. }
  158. unPos.x /= animCps.size();
  159. unPos.y /= animCps.size();
  160. // save old Position
  161. savePos = new ArrayList<>();
  162. for (int i = 0; i < animCps.size(); i++) {
  163. savePos.add(new Position(0, 0));
  164. savePos.get(i).x = animCps.get(i).getPosition().x;
  165. savePos.get(i).y = animCps.get(i).getPosition().y;
  166. }
  167. animT = new javax.swing.Timer(animDelay, new ActionListener() {
  168. @Override
  169. public void actionPerformed(ActionEvent e) {
  170. if (animDuration - animDelay > 0 && animCps.size() > 1) {
  171. for (int i = 0; i < animCps.size(); i++) {
  172. double x1 = animCps.get(i).getPosition().x - unPos.x;
  173. double y1 = animCps.get(i).getPosition().y - unPos.y;
  174. animCps.get(i).getPosition().x -= x1 / animSteps;
  175. animCps.get(i).getPosition().y -= y1 / animSteps;
  176. }
  177. repaint();
  178. animDuration -= animDelay;
  179. animSteps--;
  180. } else {
  181. animDuration = ANIMTIME;
  182. animSteps = animDuration / animDelay;
  183. animT.stop();
  184. for (int i = 0; i < animCps.size(); i++) {
  185. animCps.get(i).getPosition().x = savePos.get(i).x;
  186. animCps.get(i).getPosition().y = savePos.get(i).y;
  187. }
  188. controller.addUpperNode("NodeOfNode", upperNode, model.getSelectedCpsObjects());
  189. controller.calculateStateForCurrentTimeStep();
  190. repaint();
  191. }
  192. }
  193. });
  194. animT.start();
  195. }
  196. });
  197. itemUngroup.addActionListener(new ActionListener() {
  198. @Override
  199. public void actionPerformed(ActionEvent e) {
  200. // save old Position
  201. JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
  202. for (int i = 4; i < tabbedPane.getTabCount(); i++) {
  203. if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  204. .getComponent(0)).upperNode.getId() == ((CpsUpperNode) tempCps).getId()) {
  205. tabbedPane.remove(i);
  206. break;
  207. }
  208. }
  209. savePos = new ArrayList<>();
  210. animCps = ((CpsUpperNode) tempCps).getNodes();
  211. controller.delUpperNode((CpsUpperNode) tempCps, upperNode);
  212. for (int i = 0; i < animCps.size(); i++) {
  213. savePos.add(new Position(0, 0));
  214. savePos.get(i).x = animCps.get(i).getPosition().x;
  215. savePos.get(i).y = animCps.get(i).getPosition().y;
  216. }
  217. for (AbstractCpsObject cps : animCps) {
  218. int x = ((CpsUpperNode) tempCps).getPosition().x;
  219. int y = ((CpsUpperNode) tempCps).getPosition().y;
  220. cps.setPosition(new Position(x, y));
  221. }
  222. animT = new javax.swing.Timer(animDelay, new ActionListener() {
  223. @Override
  224. public void actionPerformed(ActionEvent e) {
  225. if (animDuration - animDelay >= 0) {
  226. for (int i = 0; i < animCps.size(); i++) {
  227. double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
  228. double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
  229. animCps.get(i).getPosition().x -= x1 / animSteps;
  230. animCps.get(i).getPosition().y -= y1 / animSteps;
  231. }
  232. repaint();
  233. animDuration -= animDelay;
  234. animSteps--;
  235. } else {
  236. animDuration = ANIMTIME;
  237. animSteps = animDuration / animDelay;
  238. animT.stop();
  239. for (int i = 0; i < animCps.size(); i++) {
  240. animCps.get(i).getPosition().x = savePos.get(i).x;
  241. animCps.get(i).getPosition().y = savePos.get(i).y;
  242. }
  243. controller.calculateStateForCurrentTimeStep();
  244. repaint();
  245. }
  246. }
  247. });
  248. animT.start();
  249. }
  250. });
  251. itemTrack.addActionListener(new ActionListener() {
  252. @Override
  253. public void actionPerformed(ActionEvent e) {
  254. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  255. if (o instanceof HolonObject) {
  256. boolean found = false;
  257. if (controller.getTrackingObj() != null) {
  258. for (AbstractCpsObject obj : controller.getTrackingObj()) {
  259. if (obj instanceof HolonObject) {
  260. if (obj.getId() == o.getId()) {
  261. found = true;
  262. }
  263. }
  264. }
  265. }
  266. if (!found) {
  267. controller.addTrackingObj((HolonObject) o);
  268. ((HolonObject) o).updateTrackingInfo();
  269. }
  270. controller.addTextToConsole("Tracking: ", Color.BLACK, 12, false, false, false);
  271. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  272. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  273. controller.addTextToConsole("" + o.getId(), Color.RED, 12, true, false, true);
  274. }
  275. }
  276. }
  277. });
  278. itemUntrack.addActionListener(new ActionListener() {
  279. @Override
  280. public void actionPerformed(ActionEvent e) {
  281. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  282. if (o instanceof HolonObject) {
  283. boolean found = false;
  284. if (controller.getTrackingObj() != null) {
  285. for (AbstractCpsObject obj : controller.getTrackingObj()) {
  286. if (obj instanceof HolonObject) {
  287. if (obj.getId() == o.getId()) {
  288. found = true;
  289. }
  290. }
  291. }
  292. }
  293. if (found) {
  294. // Removed from tracking array and tracking
  295. // information reseted
  296. controller.removeTrackingObj((HolonObject) o);
  297. ((HolonObject) o).setTrackingProd(new float[100]);
  298. ((HolonObject) o).setTrackingCons(new float[100]);
  299. }
  300. }
  301. }
  302. System.out.println(controller.getTrackingObj());
  303. }
  304. });
  305. itemDelete.addActionListener(new ActionListener() {
  306. @Override
  307. public void actionPerformed(ActionEvent e) {
  308. // Remove the selected Object objects
  309. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  310. if (upperNode.getNodes().contains(cps)) {
  311. controller.delObjUpperNode(cps, upperNode);
  312. // Removes the object from the tracked objects, in case
  313. // it was tracked
  314. controller.removeTrackingObj(cps);
  315. // Remove UpperNodeTab if UpperNode deleted
  316. if (cps instanceof CpsUpperNode) {
  317. JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
  318. for (int i = 4; i < tabbedPane.getTabCount(); i++) {
  319. if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  320. .getComponent(0)).upperNode.getId() == cps.getId()) {
  321. tabbedPane.remove(i);
  322. i = tabbedPane.getTabCount();
  323. }
  324. }
  325. }
  326. }
  327. }
  328. model.getSelectedCpsObjects().clear();
  329. tempCps = null;
  330. repaint();
  331. }
  332. });
  333. itemCut.addActionListener(new ActionListener() {
  334. @Override
  335. public void actionPerformed(ActionEvent e) {
  336. controller.cut(upperNode);
  337. itemPaste.setEnabled(true);
  338. repaint();
  339. }
  340. });
  341. itemCopy.addActionListener(new ActionListener() {
  342. @Override
  343. public void actionPerformed(ActionEvent e) {
  344. controller.copy(upperNode);
  345. itemPaste.setEnabled(true);
  346. repaint();
  347. }
  348. });
  349. itemPaste.addActionListener(new ActionListener() {
  350. @Override
  351. public void actionPerformed(ActionEvent e) {
  352. try {
  353. controller.paste(upperNode, mousePosition);
  354. } catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
  355. // TODO Auto-generated catch block
  356. JLabel message = new JLabel("The Clipboard information can't be pastet into Application.");
  357. JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
  358. }
  359. repaint();
  360. }
  361. });
  362. this.addMouseListener(this);
  363. this.addMouseMotionListener(this);
  364. }
  365. /**
  366. * Paints all Components on the Canvas.
  367. *
  368. * @param g
  369. * Graphics
  370. */
  371. public void paintComponent(Graphics g) {
  372. String maxCap;
  373. super.paintComponent(g);
  374. ((JScrollPane) this.getParent().getParent()).setColumnHeaderView(breadCrumb);
  375. // Rendering
  376. g2 = (Graphics2D) g;
  377. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  378. g2.setRenderingHints(rh);
  379. // Left Border
  380. borderPos = (int) (model.getScale() + scalediv20 + scalediv20 + 10);
  381. // Paint the Background
  382. if (!upperNode.getImagePath().isEmpty()) {
  383. img = new ImageIcon(upperNode.getImagePath()).getImage();
  384. switch (upperNode.getBackgroundMode()) {
  385. case BackgroundPopUp.IMAGE_PIXELS:
  386. g2.drawImage(img, borderPos, 0, img.getWidth(null), img.getHeight(null), null);
  387. break;
  388. case BackgroundPopUp.STRETCHED:
  389. g2.drawImage(img, borderPos, 0, model.getCanvasX(), model.getCanvasY(), null);
  390. break;
  391. case BackgroundPopUp.CUSTOM:
  392. g2.drawImage(img, borderPos, 0, upperNode.getImageWidht(), upperNode.getImageHeight(), null);
  393. break;
  394. default:
  395. break;
  396. }
  397. }
  398. // Draw Left Border
  399. g2.setColor(new Color(230, 230, 230));
  400. g2.fillRect(0, 0, borderPos, this.getHeight());
  401. g2.setColor(Color.BLACK);
  402. g2.drawLine(0, 0, this.getWidth(), 0);
  403. // Test SubNet Coloring
  404. int i = 0;
  405. for (SubNet s : controller.getSimManager().getSubNets()) {
  406. if (model.getSubNetColors().size() - 1 < i) {
  407. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  408. (int) (Math.random() * 255)));
  409. }
  410. for (HolonObject cps : s.getObjects()) {
  411. cps.setBorderColor(model.getSubNetColors().get(i));
  412. }
  413. i++;
  414. }
  415. // drawEdges that is being dragged
  416. if (drawEdge) {
  417. g2.setColor(Color.BLACK);
  418. g2.setStroke(new BasicStroke(2));
  419. // If TempCps is an outside Object
  420. if (!upperNode.getNodes().contains(tempCps)) {
  421. int count = 0;
  422. for (CpsEdge e : upperNode.getConnections()) {
  423. if (e.getA().equals(tempCps)) {
  424. g2.drawLine(borderPos >> 1, (int) (model.getScaleDiv2() + scalediv20 + 5
  425. + (model.getScale() + scalediv20 + 10) * count), x, y);
  426. } else if (e.getB().equals(tempCps)) {
  427. g2.drawLine(borderPos >> 1, (int) (model.getScaleDiv2() + scalediv20 + 5
  428. + (model.getScale() + scalediv20 + 10) * count), x, y);
  429. }
  430. count++;
  431. }
  432. } else {
  433. g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  434. }
  435. }
  436. for (CpsEdge con : upperNode.getNodeEdges()) {
  437. if (con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
  438. && con != edgeHighlight) {
  439. if (con.getConnected() == 0) {
  440. if (con.getState()) {
  441. g2.setColor(Color.GREEN);
  442. if (con.getCapacity() != -1) {
  443. g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  444. }
  445. } else {
  446. g2.setColor(Color.RED);
  447. g2.setStroke(new BasicStroke(2));
  448. }
  449. } else {
  450. g2.setColor(Color.DARK_GRAY);
  451. g2.setStroke(new BasicStroke(2));
  452. }
  453. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  454. con.getB().getPosition().y);
  455. if (con.getCapacity() == -1) {
  456. maxCap = Character.toString('\u221e');
  457. } else if (con.getCapacity() == -2) {
  458. maxCap = "???";
  459. } else {
  460. maxCap = String.valueOf(con.getCapacity());
  461. }
  462. if (showedInformation[0]) {
  463. if (con.getConnected() == 0 || con.getConnected() == 1) {
  464. g2.drawString(con.getFlow() + "/" + maxCap,
  465. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  466. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  467. } else {
  468. g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  469. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  470. }
  471. }
  472. }
  473. }
  474. // Highlighted Edge
  475. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  476. g2.setColor(Color.BLUE);
  477. for (CpsEdge con : upperNode.getNodeEdges()) {
  478. if (con.getFlow() <= con.getCapacity()) {
  479. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 3) + 1, 4)));
  480. } else {
  481. g2.setStroke(new BasicStroke(2));
  482. }
  483. if (con.getA().getId() == model.getSelectedObjectID()
  484. || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
  485. || con.getB().getId() == model.getSelectedObjectID()
  486. || model.getSelectedCpsObjects().contains(con.getB())
  487. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  488. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  489. con.getB().getPosition().y);
  490. if (con.getCapacity() == -1) {
  491. maxCap = Character.toString('\u221e');
  492. } else if (con.getCapacity() == -2) {
  493. maxCap = "???";
  494. } else {
  495. maxCap = String.valueOf(con.getCapacity());
  496. }
  497. if (showedInformation[0]) {
  498. if (con.getConnected() == 0 || con.getConnected() == 1) {
  499. g2.drawString(con.getFlow() + "/" + maxCap,
  500. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  501. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  502. } else {
  503. g2.drawString("not connected",
  504. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  505. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  506. }
  507. }
  508. }
  509. }
  510. } else if (edgeHighlight != null) {
  511. g2.setColor(Color.BLUE);
  512. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  513. g2.setStroke(
  514. new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3) + 1, 4)));
  515. } else {
  516. g2.setStroke(new BasicStroke(2));
  517. }
  518. g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight.getA().getPosition().y,
  519. edgeHighlight.getB().getPosition().x, edgeHighlight.getB().getPosition().y);
  520. if (edgeHighlight.getCapacity() == -1) {
  521. maxCap = Character.toString('\u221e');
  522. } else if (edgeHighlight.getCapacity() == -2) {
  523. maxCap = "???";
  524. } else {
  525. maxCap = String.valueOf(edgeHighlight.getCapacity());
  526. }
  527. if (showedInformation[0]) {
  528. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  529. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2,
  530. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2);
  531. }
  532. }
  533. // Objects in upper node
  534. for (AbstractCpsObject cps : upperNode.getNodes()) {
  535. // Border Highlighting
  536. if (showedInformation[3]) {
  537. g2.setColor(cps.getBorderColor());
  538. if (g2.getColor() != Color.WHITE) {
  539. g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3),
  540. (int) (cps.getPosition().y - scalediv20 - 3),
  541. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  542. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  543. }
  544. }
  545. // node image
  546. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  547. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  548. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  549. } else {
  550. if (cps instanceof HolonSwitch) {
  551. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  552. ((HolonSwitch) cps).setAutoState(true);
  553. } else {
  554. ((HolonSwitch) cps).setAutoState(false);
  555. }
  556. }
  557. // Highlighting
  558. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  559. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  560. g2.setColor(Color.BLUE);
  561. g2.fillRect((int) (cps.getPosition().x - model.getScaleDiv2() - scalediv20),
  562. (int) (cps.getPosition().y - model.getScaleDiv2() - scalediv20),
  563. (int) (controller.getScale() + (scalediv20 * 2)),
  564. (int) (controller.getScale() + (scalediv20 * 2)));
  565. if (showedInformation[1] && cps instanceof HolonObject) {
  566. g2.setColor(Color.BLACK);
  567. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  568. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - model.getScaleDiv2(),
  569. cps.getPosition().y - model.getScaleDiv2() - 10);
  570. }
  571. } else if (cps instanceof HolonObject) {
  572. g2.setColor(((HolonObject) cps).getColor());
  573. g2.fillRect((int) (cps.getPosition().x - model.getScaleDiv2() - scalediv20),
  574. (int) (cps.getPosition().y - model.getScaleDiv2() - scalediv20),
  575. (int) (controller.getScale() + (scalediv20 * 2)),
  576. (int) (controller.getScale() + (scalediv20 * 2)));
  577. if (showedInformation[1]) {
  578. g2.setColor(Color.BLACK);
  579. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  580. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - model.getScaleDiv2(),
  581. cps.getPosition().y - model.getScaleDiv2() - 10);
  582. }
  583. }
  584. // draw image
  585. File checkPath = new File(cps.getImage());
  586. if (checkPath.exists()) {
  587. img = new ImageIcon(cps.getImage()).getImage();
  588. } else {
  589. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  590. }
  591. }
  592. g2.drawImage(img, cps.getPosition().x - model.getScaleDiv2(), cps.getPosition().y - model.getScaleDiv2(),
  593. controller.getScale(), controller.getScale(), null);
  594. }
  595. // Objects connected to upperNode
  596. int count = 0;
  597. for (CpsEdge e : upperNode.getConnections()) {
  598. AbstractCpsObject cps;
  599. if (e.getA().equals(this.upperNode)) {
  600. cps = e.getB();
  601. } else {
  602. cps = e.getA();
  603. }
  604. // Show and Highlight
  605. if (model.getSelectedCpsObjects().contains(cps)) {
  606. for (CpsEdge ed : cps.getConnections()) {
  607. AbstractCpsObject obj = null;
  608. if (upperNode.getNodes().contains(ed.getA())) {
  609. obj = ed.getA();
  610. } else if (upperNode.getNodes().contains(ed.getB())) {
  611. obj = ed.getB();
  612. }
  613. if (obj != null) {
  614. g2.setColor(Color.BLUE);
  615. g2.drawLine(obj.getPosition().x, obj.getPosition().y, (borderPos >> 1),
  616. (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)
  617. + model.getScaleDiv2());
  618. }
  619. }
  620. }
  621. // Border Highlighting
  622. if (showedInformation[3]) {
  623. g2.setColor(cps.getBorderColor());
  624. if (g2.getColor() != Color.WHITE) {
  625. g2.fillRect((int) ((borderPos >> 1) - model.getScaleDiv2() - scalediv20) - 3,
  626. (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count - scalediv20) - 3,
  627. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  628. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  629. }
  630. }
  631. // node image
  632. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  633. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  634. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  635. } else {
  636. if (cps instanceof HolonSwitch) {
  637. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  638. ((HolonSwitch) cps).setAutoState(true);
  639. } else {
  640. ((HolonSwitch) cps).setAutoState(false);
  641. }
  642. }
  643. // Highlighting
  644. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  645. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  646. g2.setColor(Color.BLUE);
  647. g2.fillRect((int) ((borderPos >> 1) - model.getScaleDiv2() - scalediv20),
  648. (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count - scalediv20),
  649. (int) (controller.getScale() + (scalediv20 * 2)),
  650. (int) (controller.getScale() + (scalediv20 * 2)));
  651. } else if (cps instanceof HolonObject) {
  652. g2.setColor(((HolonObject) cps).getColor());
  653. g2.fillRect((int) ((borderPos >> 1) - model.getScaleDiv2() - scalediv20),
  654. (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count - scalediv20),
  655. (int) (controller.getScale() + (scalediv20 * 2)),
  656. (int) (controller.getScale() + (scalediv20 * 2)));
  657. }
  658. // draw image
  659. File checkPath = new File(cps.getImage());
  660. if (checkPath.exists()) {
  661. img = new ImageIcon(cps.getImage()).getImage();
  662. } else {
  663. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  664. }
  665. }
  666. g2.drawImage(img, (borderPos >> 1) - model.getScaleDiv2(),
  667. (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count), controller.getScale(),
  668. controller.getScale(), null);
  669. count++;
  670. }
  671. // Dragg Highlighting
  672. g2.setStroke(new BasicStroke(1));
  673. if (doMark) {
  674. g2.setColor(Color.BLACK);
  675. if (sx > x && sy > y) {
  676. g2.drawRect(x, y, sx - x, sy - y);
  677. } else if (sx < x && sy < y) {
  678. g2.drawRect(sx, sy, x - sx, y - sy);
  679. } else if (sx >= x) {
  680. g2.drawRect(x, sy, sx - x, y - sy);
  681. } else if (sy >= y) {
  682. g2.drawRect(sx, y, x - sx, sy - y);
  683. }
  684. }
  685. // Border Line
  686. g2.setColor(Color.BLACK);
  687. g2.drawLine(borderPos, 0, borderPos, this.getHeight());
  688. // Tooltip
  689. if (toolTip) {
  690. g2.setColor(new Color(255, 225, 150));
  691. g2.setStroke(new BasicStroke(1));
  692. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  693. // width
  694. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2(); // Position
  695. // fixed
  696. // x
  697. // Position
  698. // to
  699. // the
  700. // screen
  701. int fixYPos = toolTipPos.y; //// Position fixed y Position to the
  702. //// screen
  703. if (fixXPos < 0) {
  704. fixXPos = 0;
  705. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  706. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  707. }
  708. if (fixYPos + 16 > this.getHeight()) {
  709. fixYPos -= (fixYPos + 16) - this.getHeight();
  710. }
  711. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  712. g2.setColor(Color.BLACK);
  713. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  714. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  715. }
  716. }
  717. @Override
  718. public void mouseClicked(MouseEvent e) {
  719. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  720. }
  721. @Override
  722. public void mouseEntered(MouseEvent e) {
  723. }
  724. @Override
  725. public void mouseExited(MouseEvent e) {
  726. }
  727. @Override
  728. public void mousePressed(MouseEvent e) {
  729. tempCps = null;
  730. dataSelected = null;
  731. edgeHighlight = null;
  732. controller.setSelecteEdge(null);
  733. // Object Selection
  734. // Erase old data in the PropertyTable
  735. if (model.getPropertyTable().getRowCount() > 0) {
  736. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  737. model.getPropertyTable().removeRow(i);
  738. }
  739. }
  740. if (e.getX() > borderPos) {
  741. for (AbstractCpsObject cps : upperNode.getNodes()) {
  742. cx = cps.getPosition().x - model.getScaleDiv2();
  743. cy = cps.getPosition().y - model.getScaleDiv2();
  744. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  745. tempCps = cps;
  746. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  747. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  748. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  749. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  750. dragging = true;
  751. if (e.isControlDown() && tempCps != null) {
  752. if (model.getSelectedCpsObjects().contains(tempCps)) {
  753. controller.deleteSelectedObject(tempCps);
  754. } else {
  755. controller.addSelectedObject(tempCps);
  756. }
  757. }
  758. // If drawing an Edge (CTRL down)
  759. if (tempCps.getClass() == HolonObject.class) {
  760. HolonObject tempObj = ((HolonObject) tempCps);
  761. dataSelected = tempObj.getElements();
  762. }
  763. if (e.isShiftDown()) {
  764. drawEdge = true;
  765. dragging = false;
  766. }
  767. break;
  768. }
  769. }
  770. } else {
  771. // look for objects connected to uppernode
  772. int count = 0;
  773. for (CpsEdge ed : upperNode.getConnections()) {
  774. AbstractCpsObject cps;
  775. if (ed.getA().equals(this.upperNode)) {
  776. cps = ed.getB();
  777. } else {
  778. cps = ed.getA();
  779. }
  780. if (x - controller.getScale() <= ((borderPos >> 1) - model.getScaleDiv2())
  781. && y - controller.getScale() <= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)
  782. && x >= (borderPos >> 1) - model.getScaleDiv2()
  783. && y >= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)) {
  784. tempCps = cps;
  785. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  786. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  787. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  788. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  789. controller.setSelectedObjectID(tempCps.getId());
  790. // If drawing an Edge (CTRL down)
  791. if (tempCps.getClass() == HolonObject.class) {
  792. HolonObject tempObj = ((HolonObject) tempCps);
  793. dataSelected = tempObj.getElements();
  794. }
  795. if (e.isShiftDown()) {
  796. drawEdge = true;
  797. }
  798. }
  799. count++;
  800. }
  801. }
  802. // Selection of CpsObject
  803. // model.setSelectedCpsObject(tempCps);
  804. // Edge Selection
  805. if (e.getButton() == e.BUTTON1) {
  806. if (tempCps == null) {
  807. edgeHighlight = mousePositionOnEdge(x, y);
  808. controller.setSelecteEdge(edgeHighlight);
  809. controller.setSelectedObjectID(0);
  810. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  811. model.getSelectedCpsObjects().clear();
  812. }
  813. updCon.deleteRows(model.getMultiTable());
  814. updCon.deleteRows(model.getSingleTable());
  815. }
  816. if (edgeHighlight == null && tempCps == null) {
  817. sx = e.getX();
  818. sy = e.getY();
  819. doMark = true;
  820. }
  821. // System.out.println("Selected Objects");
  822. // for (AbstractCpsObject temp : model.getSelectedCpsObjects()) {
  823. // System.out.println(temp.getName() + " " + temp.getID());
  824. // }
  825. updCon.paintProperties(tempCps);
  826. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  827. updCon.refreshTableProperties(model.getPropertyTable());
  828. repaint();
  829. }
  830. }
  831. @Override
  832. public void mouseReleased(MouseEvent e) {
  833. dragging = false;
  834. if (model.getSelectedCpsObjects().size() > 1) {
  835. model.getTableHolonElement().setModel(model.getMultiTable());
  836. } else if (model.getSelectedCpsObjects().size() == 1) {
  837. model.getTableHolonElement().setModel(model.getSingleTable());
  838. }
  839. if (drawEdge) {
  840. drawEdge = false;
  841. drawDeleteEdge();
  842. }
  843. if (dragged == true) {
  844. try {
  845. controller.autoSave();
  846. } catch (IOException ex) {
  847. // TODO Auto-generated catch block
  848. ex.printStackTrace();
  849. }
  850. }
  851. if (!e.isControlDown() && dragged == false && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  852. model.getSelectedCpsObjects().clear();
  853. controller.addSelectedObject(tempCps);
  854. }
  855. dragged = false;
  856. // Rightclick List
  857. if (e.getButton() == MouseEvent.BUTTON3) {
  858. if (tempCps != null) {
  859. itemDelete.setEnabled(true);
  860. itemCut.setEnabled(true);
  861. itemCopy.setEnabled(true);
  862. if (tempCps != null) {
  863. itemGroup.setEnabled(true);
  864. itemTrack.setEnabled(true);
  865. itemUntrack.setEnabled(true);
  866. }
  867. if (tempCps instanceof CpsUpperNode)
  868. itemUngroup.setEnabled(true);
  869. else
  870. itemUngroup.setEnabled(false);
  871. if (model.getSelectedCpsObjects().size() == 0) {
  872. controller.addSelectedObject(tempCps);
  873. }
  874. } else {
  875. itemCut.setEnabled(false);
  876. itemCopy.setEnabled(false);
  877. itemDelete.setEnabled(false);
  878. itemGroup.setEnabled(false);
  879. itemUngroup.setEnabled(false);
  880. itemTrack.setEnabled(false);
  881. itemUntrack.setEnabled(false);
  882. }
  883. mousePosition = this.getMousePosition();
  884. popmenu.show(e.getComponent(), e.getX(), e.getY());
  885. }
  886. if (doMark) {
  887. doMark = false;
  888. for (AbstractCpsObject cps : tempSelected) {
  889. if (!model.getSelectedCpsObjects().contains(cps)) {
  890. controller.addSelectedObject(cps);
  891. }
  892. }
  893. controller.getObjectsInDepth();
  894. tempSelected.clear();
  895. }
  896. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch) {
  897. ((HolonSwitch) tempCps).switchState();
  898. }
  899. controller.calculateStateForTimeStep(model.getCurIteration());
  900. repaint();
  901. }
  902. @Override
  903. public void mouseDragged(MouseEvent e) {
  904. // If Edge is drawn
  905. x = e.getX();
  906. y = e.getY();
  907. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  908. model.getSelectedCpsObjects().clear();
  909. if (tempCps != null) {
  910. controller.addSelectedObject(tempCps);
  911. }
  912. }
  913. if (dragging) {
  914. try {
  915. // tempCps in the upperNode? else its a connected Object from
  916. // outside
  917. if (upperNode.getNodes().contains(tempCps)) {
  918. dragged = true;
  919. float xDist, yDist; // Distance
  920. x = e.getX();
  921. y = e.getY();
  922. // Make sure its in bounds
  923. if (e.getX() < controller.getScaleDiv2() + borderPos + 5)
  924. x = controller.getScaleDiv2() + borderPos + 5;
  925. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  926. x = this.getWidth() - controller.getScaleDiv2();
  927. if (e.getY() < controller.getScaleDiv2())
  928. y = controller.getScaleDiv2();
  929. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  930. y = this.getHeight() - controller.getScaleDiv2();
  931. // Distance
  932. xDist = x - tempCps.getPosition().x;
  933. yDist = y - tempCps.getPosition().y;
  934. tempCps.setPosition(x, y); // Drag Position
  935. // TipText Position and name
  936. toolTip = true;
  937. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  938. toolTipPos.x = tempCps.getPosition().x - model.getScaleDiv2();
  939. toolTipPos.y = tempCps.getPosition().y + model.getScaleDiv2();
  940. // All Selected Objects
  941. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  942. if (cps != tempCps) {
  943. x = (int) (cps.getPosition().x + xDist);
  944. y = (int) (cps.getPosition().y + yDist);
  945. // Make sure its in bounds
  946. if (x < borderPos + 5 + controller.getScaleDiv2())
  947. x = controller.getScaleDiv2() + borderPos + 5;
  948. else if (x > this.getWidth() - controller.getScaleDiv2())
  949. x = this.getWidth() - controller.getScaleDiv2();
  950. if (y <= controller.getScaleDiv2())
  951. y = controller.getScaleDiv2();
  952. else if (y > this.getHeight() - controller.getScaleDiv2())
  953. y = this.getHeight() - controller.getScaleDiv2();
  954. cps.setPosition(x, y);
  955. }
  956. }
  957. }
  958. repaint();
  959. } catch (Exception eex) {
  960. }
  961. }
  962. // Mark Objects
  963. if (doMark) {
  964. tempSelected.clear();
  965. for (AbstractCpsObject cps : upperNode.getNodes()) {
  966. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  967. if (sx >= x) {
  968. x1 = x;
  969. x2 = sx;
  970. }
  971. if (sy >= y) {
  972. y1 = y;
  973. y2 = sy;
  974. }
  975. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  976. && x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
  977. tempSelected.add(cps);
  978. }
  979. }
  980. int count = 0;
  981. for (CpsEdge ed : upperNode.getConnections()) {
  982. AbstractCpsObject cps = null;
  983. if (ed.getA().equals(upperNode)) {
  984. cps = ed.getB();
  985. } else {
  986. cps = ed.getA();
  987. }
  988. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  989. if (sx >= x) {
  990. x1 = x;
  991. x2 = sx;
  992. }
  993. if (sy >= y) {
  994. y1 = y;
  995. y2 = sy;
  996. }
  997. if (x1 <= borderPos >> 1
  998. && y1 <= (int) (5 + (model.getScale() + scalediv20 + 10) * count) + model.getScaleDiv2()
  999. && x2 >= borderPos >> 1 && y2 >= (int) (5 + (model.getScale() + scalediv20 + 10) * count)) {
  1000. tempSelected.add(cps);
  1001. }
  1002. count++;
  1003. }
  1004. }
  1005. repaint();
  1006. }
  1007. @Override
  1008. public void mouseMoved(MouseEvent e) {
  1009. x = e.getX();
  1010. y = e.getY();
  1011. // Everytghing for the tooltip :)
  1012. boolean on = false;
  1013. for (AbstractCpsObject cps : upperNode.getNodes()) {
  1014. cx = cps.getPosition().x - controller.getScaleDiv2();
  1015. cy = cps.getPosition().y - controller.getScaleDiv2();
  1016. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  1017. on = true;
  1018. toolTipPos.x = cps.getPosition().x - model.getScaleDiv2();
  1019. toolTipPos.y = cps.getPosition().y + model.getScaleDiv2();
  1020. toolTipText = cps.getName() + ", " + cps.getId();
  1021. }
  1022. }
  1023. int count = 0;
  1024. for (CpsEdge ed : upperNode.getConnections()) {
  1025. AbstractCpsObject cps;
  1026. if (ed.getA().equals(this.upperNode)) {
  1027. cps = ed.getB();
  1028. } else {
  1029. cps = ed.getA();
  1030. }
  1031. cx = borderPos >> 1;
  1032. cy = (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count);
  1033. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  1034. on = true;
  1035. toolTipPos.x = cx - model.getScaleDiv2();
  1036. toolTipPos.y = cy + controller.getScale();
  1037. toolTipText = cps.getName() + ", " + cps.getId();
  1038. }
  1039. count++;
  1040. }
  1041. if (on) {
  1042. toolTip = true;
  1043. } else {
  1044. toolTip = false;
  1045. }
  1046. repaint();
  1047. }
  1048. /**
  1049. * Draws or Deletes an Edge.
  1050. */
  1051. private void drawDeleteEdge() {
  1052. boolean node = true; // new node?
  1053. boolean newEdge = true;
  1054. boolean onEdge = true;
  1055. boolean deleteNode = false;
  1056. boolean outsideCon = !upperNode.getNodes().contains(tempCps); // Connection
  1057. // to
  1058. // the
  1059. // outside
  1060. boolean found = false; // dont search for outside connetion if inside
  1061. // connection is found
  1062. CpsEdge e = null;
  1063. for (AbstractCpsObject cps : upperNode.getNodes()) {
  1064. cx = cps.getPosition().x - controller.getScaleDiv2();
  1065. cy = cps.getPosition().y - controller.getScaleDiv2();
  1066. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  1067. && cps != tempCps) {
  1068. found = true;
  1069. node = false;
  1070. onEdge = false;
  1071. for (CpsEdge p : tempCps.getConnections()) {
  1072. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  1073. newEdge = false;
  1074. e = p;
  1075. }
  1076. }
  1077. if (!newEdge) {
  1078. if (outsideCon) {
  1079. controller.disconnectNodes(e, upperNode);
  1080. } else {
  1081. controller.delEdgeUpperNode(e, upperNode);
  1082. }
  1083. // Node ohne Edge?
  1084. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  1085. tempCps = e.getA();
  1086. deleteNode = true;
  1087. }
  1088. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  1089. deleteNode = true;
  1090. }
  1091. }
  1092. if (newEdge) {
  1093. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  1094. if (outsideCon) {
  1095. controller.connectNodes(e, upperNode);
  1096. } else {
  1097. controller.addEdgeUpperNode(e, upperNode);
  1098. }
  1099. }
  1100. }
  1101. }
  1102. if (!found && !outsideCon) {
  1103. int count = 0;
  1104. for (CpsEdge ed : upperNode.getConnections()) {
  1105. AbstractCpsObject cps = null;
  1106. if (ed.getA().equals(upperNode)) {
  1107. cps = ed.getB();
  1108. } else {
  1109. cps = ed.getA();
  1110. }
  1111. cx = borderPos >> 1;
  1112. cy = (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count);
  1113. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  1114. && cps != tempCps) {
  1115. outsideCon = true;
  1116. node = false;
  1117. onEdge = false;
  1118. for (CpsEdge p : tempCps.getConnections()) {
  1119. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  1120. newEdge = false;
  1121. e = p;
  1122. }
  1123. }
  1124. if (!newEdge) {
  1125. if (outsideCon) {
  1126. controller.disconnectNodes(e, upperNode);
  1127. } else {
  1128. controller.delEdgeUpperNode(e, upperNode);
  1129. }
  1130. // Node ohne Edge?
  1131. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  1132. tempCps = e.getA();
  1133. deleteNode = true;
  1134. }
  1135. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  1136. deleteNode = true;
  1137. }
  1138. }
  1139. if (newEdge) {
  1140. CpsEdge edge = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  1141. if (outsideCon) {
  1142. controller.connectNodes(edge, upperNode);
  1143. } else {
  1144. controller.addEdgeUpperNode(edge, upperNode);
  1145. }
  1146. }
  1147. }
  1148. count++;
  1149. }
  1150. }
  1151. // Edge auf eine Edge gezogen?
  1152. if (onEdge) {
  1153. CpsEdge p = mousePositionOnEdge(x, y);
  1154. if (p != null) {
  1155. CpsEdge e1 = null;
  1156. CpsEdge e2 = null;
  1157. node = false;
  1158. CpsNode n = new CpsNode("Node");
  1159. n.setPosition(x, y);
  1160. controller.addObjUpperNode(n, upperNode);
  1161. AbstractCpsObject r, k;
  1162. r = p.getA();
  1163. k = p.getB();
  1164. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  1165. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  1166. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  1167. controller.delEdgeUpperNode(p, upperNode);
  1168. if (outsideCon) {
  1169. controller.connectNodes(e, upperNode);
  1170. } else {
  1171. controller.addEdgeUpperNode(e, upperNode);
  1172. }
  1173. controller.addEdgeUpperNode(e1, upperNode);
  1174. controller.addEdgeUpperNode(e2, upperNode);
  1175. }
  1176. }
  1177. // ins leere Gedragged
  1178. if (node && x > borderPos) {
  1179. CpsNode n = new CpsNode("Node");
  1180. n.setPosition(x, y);
  1181. controller.addObjUpperNode(n, upperNode);
  1182. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  1183. if (outsideCon) {
  1184. controller.connectNodes(e, upperNode);
  1185. } else {
  1186. controller.addEdgeUpperNode(e, upperNode);
  1187. }
  1188. }
  1189. // Wenn ein Node ohne Connections da ist
  1190. if (deleteNode) {
  1191. controller.delCanvasObject(tempCps, true);
  1192. tempCps = null;
  1193. }
  1194. }
  1195. /**
  1196. * Checks if the mouse is on an Edge.
  1197. *
  1198. * @param x
  1199. * Position of the Mouse
  1200. * @param y
  1201. * Position of the Mouse
  1202. *
  1203. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  1204. */
  1205. public CpsEdge mousePositionOnEdge(int x, int y) {
  1206. x += controller.getScaleDiv2();
  1207. y += controller.getScaleDiv2();
  1208. int lx, ly, hx, hy;
  1209. for (CpsEdge p : upperNode.getNodeEdges()) {
  1210. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  1211. p.getB().getPosition().y);
  1212. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  1213. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  1214. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  1215. } else {
  1216. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  1217. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  1218. }
  1219. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  1220. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  1221. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  1222. } else {
  1223. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  1224. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  1225. }
  1226. // distance from a point to a line and between both Objects
  1227. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  1228. && y < hy) {
  1229. return p;
  1230. }
  1231. }
  1232. return null;
  1233. }
  1234. /**
  1235. * Checks if a double click was made.
  1236. *
  1237. * @return true if doublecklick, false if not
  1238. */
  1239. private boolean doubleClick() {
  1240. if (click) {
  1241. click = false;
  1242. return true;
  1243. } else {
  1244. click = true;
  1245. Timer t = new Timer("doubleclickTimer", false);
  1246. t.schedule(new TimerTask() {
  1247. @Override
  1248. public void run() {
  1249. click = false;
  1250. }
  1251. }, 500);
  1252. }
  1253. return false;
  1254. }
  1255. /**
  1256. * sets the Edge Capacity.
  1257. *
  1258. * @param cap
  1259. * capacity
  1260. */
  1261. public void setEdgeCapacity(float cap) {
  1262. controller.setMaxCapacity(cap);
  1263. }
  1264. /**
  1265. * Set if Information should be shown.
  1266. *
  1267. * @param connection
  1268. * boolean for conecction
  1269. * @param object
  1270. * boolean for objects
  1271. */
  1272. public void setShowedInformation(boolean connection, boolean object) {
  1273. showedInformation[0] = connection;
  1274. showedInformation[1] = object;
  1275. }
  1276. /**
  1277. * copies a set of given informations
  1278. *
  1279. * @param informations
  1280. */
  1281. public void setShowedInformation(boolean[] informations) {
  1282. showedInformation = informations;
  1283. }
  1284. /**
  1285. * Returns if Information should be shown.
  1286. *
  1287. * @return Array of boolean [0] = connection, [1] = objects
  1288. */
  1289. public boolean[] getShowedInformation() {
  1290. return showedInformation;
  1291. }
  1292. /**
  1293. * Set the Background Image;
  1294. *
  1295. * @param imagePath
  1296. * Image Path
  1297. * @param mode
  1298. * Image Mode
  1299. * @param width
  1300. * Image custom width
  1301. * @param height
  1302. * Image custom height
  1303. */
  1304. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  1305. upperNode.setBackgroundImage(imagePath, mode, width, height);
  1306. }
  1307. }