MyCanvas.java 40 KB

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