GroupNodeCanvas.java 49 KB

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