GroupNodeCanvas.java 51 KB

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