GroupNodeCanvas.java 52 KB

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