GroupNodeCanvas.java 49 KB

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