UpperNodeCanvas.java 51 KB

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