GroupNodeCanvas.java 50 KB

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