GroupNodeCanvas.java 49 KB

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