GroupNodeCanvas.java 53 KB

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