GroupNodeCanvas.java 53 KB

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