UpperNodeCanvas.java 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  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
  352. .getTabComponentAt(tabbedPane2.getSelectedIndex())).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. } else {
  656. g2.setColor(Color.DARK_GRAY);
  657. g2.setStroke(new BasicStroke(2));
  658. }
  659. g2.drawLine(obj.getPosition().x, obj.getPosition().y, (borderPos >> 1),
  660. (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count) + 25);
  661. if (showedInformation[0]) {
  662. if (ed.getConnected() == 0 || ed.getConnected() == 1) {
  663. g2.drawString(ed.getFlow() + "/" + ed.getCapacity(),
  664. (ed.getA().getPosition().x + ed.getB().getPosition().x) / 2,
  665. (ed.getA().getPosition().y + ed.getB().getPosition().y) / 2);
  666. } else {
  667. g2.drawString("not connected",
  668. (ed.getA().getPosition().x + ed.getB().getPosition().x) / 2,
  669. (ed.getA().getPosition().y + ed.getB().getPosition().y) / 2);
  670. }
  671. }
  672. }
  673. }
  674. }
  675. // Border Highlighting
  676. if (showedInformation[3]) {
  677. g2.setColor(cps.getBorderColor());
  678. if (g2.getColor() != Color.WHITE) {
  679. g2.fillRect((int) ((borderPos >> 1) - 25 - scalediv20) - 3,
  680. (int) (scalediv20 + 5 + (25 + scalediv20 + 10) * count - scalediv20) - 3,
  681. (int) (50 + ((scalediv20 + 3) * 2)), (int) (50 + ((scalediv20 + 3) * 2)));
  682. }
  683. }
  684. // node image
  685. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  686. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  687. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  688. } else {
  689. if (cps instanceof HolonSwitch) {
  690. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  691. ((HolonSwitch) cps).setAutoState(true);
  692. } else {
  693. ((HolonSwitch) cps).setAutoState(false);
  694. }
  695. }
  696. // Highlighting
  697. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  698. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  699. g2.setColor(Color.BLUE);
  700. g2.fillRect((int) ((borderPos >> 1) - 25 - scalediv20),
  701. (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count - scalediv20),
  702. (int) (50 + (scalediv20 * 2)), (int) (50 + (scalediv20 * 2)));
  703. } else if (cps instanceof HolonObject) {
  704. g2.setColor(((HolonObject) cps).getColor());
  705. g2.fillRect((int) ((borderPos >> 1) - 25 - scalediv20),
  706. (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count - scalediv20),
  707. (int) (50 + (scalediv20 * 2)), (int) (50 + (scalediv20 * 2)));
  708. }
  709. // draw image
  710. File checkPath = new File(cps.getImage());
  711. if (checkPath.exists()) {
  712. img = new ImageIcon(cps.getImage()).getImage();
  713. } else {
  714. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  715. }
  716. }
  717. g2.drawImage(img, (borderPos >> 1) - 25, (int) (scalediv20 + 5 + (50 + scalediv20 + 10) * count), 50, 50,
  718. null);
  719. count++;
  720. }
  721. // Dragg Highlighting
  722. g2.setStroke(new BasicStroke(1));
  723. if (doMark) {
  724. g2.setColor(Color.BLACK);
  725. if (sx > x && sy > y) {
  726. g2.drawRect(x, y, sx - x, sy - y);
  727. } else if (sx < x && sy < y) {
  728. g2.drawRect(sx, sy, x - sx, y - sy);
  729. } else if (sx >= x) {
  730. g2.drawRect(x, sy, sx - x, y - sy);
  731. } else if (sy >= y) {
  732. g2.drawRect(sx, y, x - sx, sy - y);
  733. }
  734. }
  735. // Border Line
  736. g2.setColor(Color.BLACK);
  737. g2.drawLine(borderPos, 0, borderPos, this.getHeight());
  738. // Tooltip
  739. if (toolTip) {
  740. g2.setColor(new Color(255, 225, 150));
  741. g2.setStroke(new BasicStroke(1));
  742. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  743. // width
  744. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2(); // Position
  745. // fixed
  746. // x
  747. // Position
  748. // to
  749. // the
  750. // screen
  751. int fixYPos = toolTipPos.y; //// Position fixed y Position to the
  752. //// screen
  753. if (fixXPos < 0) {
  754. fixXPos = 0;
  755. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  756. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  757. }
  758. if (fixYPos + 16 > this.getHeight()) {
  759. fixYPos -= (fixYPos + 16) - this.getHeight();
  760. }
  761. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  762. g2.setColor(Color.BLACK);
  763. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  764. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  765. }
  766. }
  767. @Override
  768. public void mouseClicked(MouseEvent e) {
  769. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  770. }
  771. @Override
  772. public void mouseEntered(MouseEvent e) {
  773. }
  774. @Override
  775. public void mouseExited(MouseEvent e) {
  776. }
  777. @Override
  778. public void mousePressed(MouseEvent e) {
  779. tempCps = null;
  780. dataSelected = null;
  781. edgeHighlight = null;
  782. controller.setSelecteEdge(null);
  783. // Object Selection
  784. // Erase old data in the PropertyTable
  785. if (model.getPropertyTable().getRowCount() > 0) {
  786. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  787. model.getPropertyTable().removeRow(i);
  788. }
  789. }
  790. if (e.getX() > borderPos) {
  791. for (AbstractCpsObject cps : upperNode.getNodes()) {
  792. cx = cps.getPosition().x - model.getScaleDiv2();
  793. cy = cps.getPosition().y - model.getScaleDiv2();
  794. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  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. dragging = true;
  801. if (e.isControlDown() && tempCps != null) {
  802. if (model.getSelectedCpsObjects().contains(tempCps)) {
  803. controller.deleteSelectedObject(tempCps);
  804. } else {
  805. controller.addSelectedObject(tempCps);
  806. }
  807. }
  808. // If drawing an Edge (CTRL down)
  809. if (tempCps.getClass() == HolonObject.class) {
  810. HolonObject tempObj = ((HolonObject) tempCps);
  811. dataSelected = tempObj.getElements();
  812. }
  813. if (e.isShiftDown()) {
  814. drawEdge = true;
  815. dragging = false;
  816. }
  817. break;
  818. }
  819. }
  820. } else {
  821. // look for objects connected to uppernode
  822. int count = 0;
  823. for (CpsEdge ed : upperNode.getConnections()) {
  824. AbstractCpsObject cps;
  825. if (ed.getA().equals(this.upperNode)) {
  826. cps = ed.getB();
  827. } else {
  828. cps = ed.getA();
  829. }
  830. if (x - controller.getScale() <= ((borderPos >> 1) - model.getScaleDiv2())
  831. && y - controller.getScale() <= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)
  832. && x >= (borderPos >> 1) - model.getScaleDiv2()
  833. && y >= (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count)) {
  834. tempCps = cps;
  835. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  836. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  837. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  838. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  839. controller.setSelectedObjectID(tempCps.getId());
  840. // If drawing an Edge (CTRL down)
  841. if (tempCps.getClass() == HolonObject.class) {
  842. HolonObject tempObj = ((HolonObject) tempCps);
  843. dataSelected = tempObj.getElements();
  844. }
  845. if (e.isShiftDown()) {
  846. drawEdge = true;
  847. }
  848. }
  849. count++;
  850. }
  851. }
  852. // Selection of CpsObject
  853. // model.setSelectedCpsObject(tempCps);
  854. // Edge Selection
  855. if (e.getButton() == e.BUTTON1) {
  856. if (tempCps == null) {
  857. edgeHighlight = mousePositionOnEdge(x, y);
  858. controller.setSelecteEdge(edgeHighlight);
  859. controller.setSelectedObjectID(0);
  860. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  861. model.getSelectedCpsObjects().clear();
  862. }
  863. updCon.deleteRows(model.getMultiTable());
  864. updCon.deleteRows(model.getSingleTable());
  865. }
  866. if (edgeHighlight == null && tempCps == null) {
  867. sx = e.getX();
  868. sy = e.getY();
  869. doMark = true;
  870. }
  871. // System.out.println("Selected Objects");
  872. // for (AbstractCpsObject temp : model.getSelectedCpsObjects()) {
  873. // System.out.println(temp.getName() + " " + temp.getID());
  874. // }
  875. updCon.paintProperties(tempCps);
  876. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  877. updCon.refreshTableProperties(model.getPropertyTable());
  878. repaint();
  879. }
  880. }
  881. @Override
  882. public void mouseReleased(MouseEvent e) {
  883. x = e.getX();
  884. y = e.getY();
  885. dragging = false;
  886. if (model.getSelectedCpsObjects().size() > 1) {
  887. model.getTableHolonElement().setModel(model.getMultiTable());
  888. } else if (model.getSelectedCpsObjects().size() == 1) {
  889. model.getTableHolonElement().setModel(model.getSingleTable());
  890. }
  891. if (drawEdge) {
  892. drawEdge = false;
  893. drawDeleteEdge();
  894. }
  895. if (dragged == true) {
  896. try {
  897. controller.autoSave();
  898. } catch (IOException ex) {
  899. // TODO Auto-generated catch block
  900. ex.printStackTrace();
  901. }
  902. }
  903. if (!e.isControlDown() && dragged == false && tempCps != null && MouseEvent.BUTTON3 != e.getButton()) {
  904. model.getSelectedCpsObjects().clear();
  905. controller.addSelectedObject(tempCps);
  906. }
  907. dragged = false;
  908. // Rightclick List
  909. if (e.getButton() == MouseEvent.BUTTON3) {
  910. if (tempCps != null) {
  911. itemDelete.setEnabled(true);
  912. itemCut.setEnabled(true);
  913. itemCopy.setEnabled(true);
  914. if (tempCps != null) {
  915. itemGroup.setEnabled(true);
  916. itemTrack.setEnabled(true);
  917. itemUntrack.setEnabled(true);
  918. }
  919. if (tempCps instanceof CpsUpperNode)
  920. itemUngroup.setEnabled(true);
  921. else
  922. itemUngroup.setEnabled(false);
  923. if (model.getSelectedCpsObjects().size() == 0) {
  924. controller.addSelectedObject(tempCps);
  925. }
  926. } else {
  927. itemCut.setEnabled(false);
  928. itemCopy.setEnabled(false);
  929. itemDelete.setEnabled(false);
  930. itemGroup.setEnabled(false);
  931. itemUngroup.setEnabled(false);
  932. itemTrack.setEnabled(false);
  933. itemUntrack.setEnabled(false);
  934. }
  935. mousePosition = this.getMousePosition();
  936. popmenu.show(e.getComponent(), e.getX(), e.getY());
  937. }
  938. if (doMark) {
  939. doMark = false;
  940. for (AbstractCpsObject cps : tempSelected) {
  941. if (!model.getSelectedCpsObjects().contains(cps)) {
  942. controller.addSelectedObject(cps);
  943. }
  944. }
  945. controller.getObjectsInDepth();
  946. tempSelected.clear();
  947. }
  948. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch) {
  949. ((HolonSwitch) tempCps).switchState();
  950. }
  951. controller.calculateStateForTimeStep(model.getCurIteration());
  952. repaint();
  953. }
  954. @Override
  955. public void mouseDragged(MouseEvent e) {
  956. // If Edge is drawn
  957. x = e.getX();
  958. y = e.getY();
  959. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  960. model.getSelectedCpsObjects().clear();
  961. if (tempCps != null) {
  962. controller.addSelectedObject(tempCps);
  963. }
  964. }
  965. if (dragging) {
  966. try {
  967. // tempCps in the upperNode? else its a connected Object from
  968. // outside
  969. if (upperNode.getNodes().contains(tempCps)) {
  970. dragged = true;
  971. float xDist, yDist; // Distance
  972. x = e.getX();
  973. y = e.getY();
  974. // Make sure its in bounds
  975. if (e.getX() < controller.getScaleDiv2() + borderPos + 5)
  976. x = controller.getScaleDiv2() + borderPos + 5;
  977. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  978. x = this.getWidth() - controller.getScaleDiv2();
  979. if (e.getY() < controller.getScaleDiv2())
  980. y = controller.getScaleDiv2();
  981. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  982. y = this.getHeight() - controller.getScaleDiv2();
  983. // Distance
  984. xDist = x - tempCps.getPosition().x;
  985. yDist = y - tempCps.getPosition().y;
  986. tempCps.setPosition(x, y); // Drag Position
  987. // TipText Position and name
  988. toolTip = true;
  989. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  990. toolTipPos.x = tempCps.getPosition().x - model.getScaleDiv2();
  991. toolTipPos.y = tempCps.getPosition().y + model.getScaleDiv2();
  992. // All Selected Objects
  993. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  994. if (cps != tempCps) {
  995. x = (int) (cps.getPosition().x + xDist);
  996. y = (int) (cps.getPosition().y + yDist);
  997. // Make sure its in bounds
  998. if (x < borderPos + 5 + controller.getScaleDiv2())
  999. x = controller.getScaleDiv2() + borderPos + 5;
  1000. else if (x > this.getWidth() - controller.getScaleDiv2())
  1001. x = this.getWidth() - controller.getScaleDiv2();
  1002. if (y <= controller.getScaleDiv2())
  1003. y = controller.getScaleDiv2();
  1004. else if (y > this.getHeight() - controller.getScaleDiv2())
  1005. y = this.getHeight() - controller.getScaleDiv2();
  1006. cps.setPosition(x, y);
  1007. }
  1008. }
  1009. }
  1010. repaint();
  1011. } catch (Exception eex) {
  1012. }
  1013. }
  1014. // Mark Objects
  1015. if (doMark) {
  1016. tempSelected.clear();
  1017. for (AbstractCpsObject cps : upperNode.getNodes()) {
  1018. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  1019. if (sx >= x) {
  1020. x1 = x;
  1021. x2 = sx;
  1022. }
  1023. if (sy >= y) {
  1024. y1 = y;
  1025. y2 = sy;
  1026. }
  1027. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  1028. && x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
  1029. tempSelected.add(cps);
  1030. }
  1031. }
  1032. int count = 0;
  1033. for (CpsEdge ed : upperNode.getConnections()) {
  1034. AbstractCpsObject cps = null;
  1035. if (ed.getA().equals(upperNode)) {
  1036. cps = ed.getB();
  1037. } else {
  1038. cps = ed.getA();
  1039. }
  1040. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  1041. if (sx >= x) {
  1042. x1 = x;
  1043. x2 = sx;
  1044. }
  1045. if (sy >= y) {
  1046. y1 = y;
  1047. y2 = sy;
  1048. }
  1049. if (x1 <= borderPos >> 1
  1050. && y1 <= (int) (5 + (model.getScale() + scalediv20 + 10) * count) + model.getScaleDiv2()
  1051. && x2 >= borderPos >> 1 && y2 >= (int) (5 + (model.getScale() + scalediv20 + 10) * count)) {
  1052. tempSelected.add(cps);
  1053. }
  1054. count++;
  1055. }
  1056. }
  1057. repaint();
  1058. }
  1059. @Override
  1060. public void mouseMoved(MouseEvent e) {
  1061. x = e.getX();
  1062. y = e.getY();
  1063. // Everytghing for the tooltip :)
  1064. boolean on = false;
  1065. for (AbstractCpsObject cps : upperNode.getNodes()) {
  1066. cx = cps.getPosition().x - controller.getScaleDiv2();
  1067. cy = cps.getPosition().y - controller.getScaleDiv2();
  1068. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  1069. on = true;
  1070. toolTipPos.x = cps.getPosition().x - model.getScaleDiv2();
  1071. toolTipPos.y = cps.getPosition().y + model.getScaleDiv2();
  1072. toolTipText = cps.getName() + ", " + cps.getId();
  1073. }
  1074. }
  1075. int count = 0;
  1076. for (CpsEdge ed : upperNode.getConnections()) {
  1077. AbstractCpsObject cps;
  1078. if (ed.getA().equals(this.upperNode)) {
  1079. cps = ed.getB();
  1080. } else {
  1081. cps = ed.getA();
  1082. }
  1083. cx = borderPos >> 1;
  1084. cy = (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count);
  1085. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  1086. on = true;
  1087. toolTipPos.x = cx - model.getScaleDiv2();
  1088. toolTipPos.y = cy + controller.getScale();
  1089. toolTipText = cps.getName() + ", " + cps.getId();
  1090. }
  1091. count++;
  1092. }
  1093. if (on) {
  1094. toolTip = true;
  1095. } else {
  1096. toolTip = false;
  1097. }
  1098. repaint();
  1099. }
  1100. /**
  1101. * Draws or Deletes an Edge.
  1102. */
  1103. private void drawDeleteEdge() {
  1104. boolean node = true; // new node?
  1105. boolean newEdge = true;
  1106. boolean onEdge = true;
  1107. boolean deleteNode = false;
  1108. boolean outsideCon = !upperNode.getNodes().contains(tempCps); // Connection
  1109. // to
  1110. // the
  1111. // outside
  1112. boolean found = false; // dont search for outside connetion if inside
  1113. // connection is found
  1114. CpsEdge e = null;
  1115. for (AbstractCpsObject cps : upperNode.getNodes()) {
  1116. cx = cps.getPosition().x - controller.getScaleDiv2();
  1117. cy = cps.getPosition().y - controller.getScaleDiv2();
  1118. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  1119. && cps != tempCps) {
  1120. found = true;
  1121. node = false;
  1122. onEdge = false;
  1123. for (CpsEdge p : tempCps.getConnections()) {
  1124. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  1125. newEdge = false;
  1126. e = p;
  1127. }
  1128. }
  1129. if (!newEdge) {
  1130. if (outsideCon) {
  1131. controller.disconnectNodes(e, upperNode);
  1132. } else {
  1133. controller.delEdgeUpperNode(e, upperNode);
  1134. }
  1135. // Node ohne Edge?
  1136. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  1137. tempCps = e.getA();
  1138. deleteNode = true;
  1139. }
  1140. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  1141. deleteNode = true;
  1142. }
  1143. }
  1144. if (newEdge) {
  1145. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  1146. if (outsideCon) {
  1147. controller.connectNodes(e, upperNode);
  1148. } else {
  1149. controller.addEdgeUpperNode(e, upperNode);
  1150. }
  1151. }
  1152. }
  1153. }
  1154. if (!found && !outsideCon) {
  1155. int count = 0;
  1156. for (CpsEdge ed : upperNode.getConnections()) {
  1157. AbstractCpsObject cps = null;
  1158. if (ed.getA().equals(upperNode)) {
  1159. cps = ed.getB();
  1160. } else {
  1161. cps = ed.getA();
  1162. }
  1163. cx = borderPos >> 1;
  1164. cy = (int) (scalediv20 + 5 + (model.getScale() + scalediv20 + 10) * count);
  1165. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  1166. && cps != tempCps) {
  1167. outsideCon = true;
  1168. node = false;
  1169. onEdge = false;
  1170. for (CpsEdge p : tempCps.getConnections()) {
  1171. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  1172. newEdge = false;
  1173. e = p;
  1174. }
  1175. }
  1176. if (!newEdge) {
  1177. if (outsideCon) {
  1178. controller.disconnectNodes(e, upperNode);
  1179. } else {
  1180. controller.delEdgeUpperNode(e, upperNode);
  1181. }
  1182. // Node ohne Edge?
  1183. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  1184. tempCps = e.getA();
  1185. deleteNode = true;
  1186. }
  1187. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  1188. deleteNode = true;
  1189. }
  1190. }
  1191. if (newEdge) {
  1192. CpsEdge edge = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  1193. if (outsideCon) {
  1194. controller.connectNodes(edge, upperNode);
  1195. } else {
  1196. controller.addEdgeUpperNode(edge, upperNode);
  1197. }
  1198. }
  1199. }
  1200. count++;
  1201. }
  1202. }
  1203. // Edge auf eine Edge gezogen?
  1204. if (onEdge) {
  1205. CpsEdge p = mousePositionOnEdge(x, y);
  1206. if (p != null) {
  1207. CpsEdge e1 = null;
  1208. CpsEdge e2 = null;
  1209. node = false;
  1210. CpsNode n = new CpsNode("Node");
  1211. n.setPosition(x, y);
  1212. controller.addObjUpperNode(n, upperNode);
  1213. AbstractCpsObject r, k;
  1214. r = p.getA();
  1215. k = p.getB();
  1216. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  1217. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  1218. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  1219. controller.delEdgeUpperNode(p, upperNode);
  1220. if (outsideCon) {
  1221. controller.connectNodes(e, upperNode);
  1222. } else {
  1223. controller.addEdgeUpperNode(e, upperNode);
  1224. }
  1225. controller.addEdgeUpperNode(e1, upperNode);
  1226. controller.addEdgeUpperNode(e2, upperNode);
  1227. }
  1228. }
  1229. // ins leere Gedragged
  1230. if (node && x > borderPos) {
  1231. CpsNode n = new CpsNode("Node");
  1232. n.setPosition(x, y);
  1233. controller.addObjUpperNode(n, upperNode);
  1234. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  1235. if (outsideCon) {
  1236. controller.connectNodes(e, upperNode);
  1237. } else {
  1238. controller.addEdgeUpperNode(e, upperNode);
  1239. }
  1240. }
  1241. // Wenn ein Node ohne Connections da ist
  1242. if (deleteNode) {
  1243. controller.delCanvasObject(tempCps, true);
  1244. tempCps = null;
  1245. }
  1246. }
  1247. /**
  1248. * Checks if the mouse is on an Edge.
  1249. *
  1250. * @param x
  1251. * Position of the Mouse
  1252. * @param y
  1253. * Position of the Mouse
  1254. *
  1255. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  1256. */
  1257. public CpsEdge mousePositionOnEdge(int x, int y) {
  1258. x += controller.getScaleDiv2();
  1259. y += controller.getScaleDiv2();
  1260. int lx, ly, hx, hy;
  1261. for (CpsEdge p : upperNode.getNodeEdges()) {
  1262. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  1263. p.getB().getPosition().y);
  1264. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  1265. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  1266. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  1267. } else {
  1268. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  1269. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  1270. }
  1271. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  1272. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  1273. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  1274. } else {
  1275. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  1276. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  1277. }
  1278. // distance from a point to a line and between both Objects
  1279. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  1280. && y < hy) {
  1281. return p;
  1282. }
  1283. }
  1284. return null;
  1285. }
  1286. /**
  1287. * Checks if a double click was made.
  1288. *
  1289. * @return true if doublecklick, false if not
  1290. */
  1291. private boolean doubleClick() {
  1292. if (click) {
  1293. click = false;
  1294. return true;
  1295. } else {
  1296. click = true;
  1297. Timer t = new Timer("doubleclickTimer", false);
  1298. t.schedule(new TimerTask() {
  1299. @Override
  1300. public void run() {
  1301. click = false;
  1302. }
  1303. }, 500);
  1304. }
  1305. return false;
  1306. }
  1307. /**
  1308. * sets the Edge Capacity.
  1309. *
  1310. * @param cap
  1311. * capacity
  1312. */
  1313. public void setEdgeCapacity(float cap) {
  1314. controller.setMaxCapacity(cap);
  1315. }
  1316. /**
  1317. * Set if Information should be shown.
  1318. *
  1319. * @param connection
  1320. * boolean for conecction
  1321. * @param object
  1322. * boolean for objects
  1323. */
  1324. public void setShowedInformation(boolean connection, boolean object, boolean nodeOfnode) {
  1325. showedInformation[0] = connection;
  1326. showedInformation[1] = object;
  1327. showedInformation[4] = nodeOfnode;
  1328. }
  1329. /**
  1330. * copies a set of given informations
  1331. *
  1332. * @param informations
  1333. */
  1334. public void setShowedInformation(boolean[] informations) {
  1335. showedInformation = informations;
  1336. }
  1337. /**
  1338. * Returns if Information should be shown.
  1339. *
  1340. * @return Array of boolean [0] = connection, [1] = objects
  1341. */
  1342. public boolean[] getShowedInformation() {
  1343. return showedInformation;
  1344. }
  1345. /**
  1346. * Set the Background Image;
  1347. *
  1348. * @param imagePath
  1349. * Image Path
  1350. * @param mode
  1351. * Image Mode
  1352. * @param width
  1353. * Image custom width
  1354. * @param height
  1355. * Image custom height
  1356. */
  1357. public void setBackgroundImage(String imagePath, int mode, int width, int height) {
  1358. upperNode.setBackgroundImage(imagePath, mode, width, height);
  1359. }
  1360. }