UpperNodeCanvas.java 48 KB

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