MyCanvas.java 38 KB

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