UpperNodeCanvas.java 46 KB

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