GroupNodeCanvas.java 49 KB

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