MyCanvas.java 40 KB

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