UpperNodeCanvas.java 52 KB

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