MyCanvas.java 40 KB

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