MyCanvas.java 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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. showConnectionInformation = 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 (AbstractCanvasObject 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 (AbstractCanvasObject 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 = ((GroupNode) tempCps).getNodes();
  139. controller.delUpperNode((GroupNode) 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 (AbstractCanvasObject 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 (AbstractCanvasObject 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 (AbstractCanvasObject o : model.getSelectedCpsObjects()) {
  195. if (o instanceof HolonObject) {
  196. boolean found = false;
  197. if (controller.getTrackingObj() != null) {
  198. for (AbstractCanvasObject 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() == Node.class && edgeHighlight.getA().getConnections().size() == 0){
  224. controller.delCanvasObject(edgeHighlight.getA(), false);
  225. }
  226. if(edgeHighlight.getB().getClass() == Node.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. AbstractCanvasObject 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 GroupNode) {
  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. && ((GroupNodeCanvas) ((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. && ((GroupNodeCanvas) ((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 GroupNode)
  295. controller.getObjectsInDepth();
  296. controller.copy(null);
  297. itemPaste.setEnabled(true);
  298. repaint();
  299. });
  300. itemAlign.addActionListener(actionEvent ->
  301. {
  302. for(AbstractCanvasObject 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. if(showConnectionInformation) {
  402. Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
  403. g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  404. g.drawString(currentEnergy + "/" + (unlimited?"\u221E":capacity) , middle.x, middle.y);
  405. }
  406. }
  407. private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
  408. {
  409. drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage(): HolonSwitch.getSwitchClosedImage() , dSwitch.getModel().getPosition());
  410. }
  411. private void paintExitCable(Graphics2D g, ExitCable eCable) {
  412. Position start = eCable.getStart().getPosition();
  413. Position end = eCable.getFinish().getPosition();
  414. float currentEnergy = eCable.getCable().getFlowEnergy();
  415. float capacity = eCable.getCable().getModel().getCapacity();
  416. boolean unlimited = eCable.getCable().getModel().isUnlimitedCapacity();
  417. switch(eCable.getCable().getState()) {
  418. case Burned:
  419. g.setColor(Color.RED);
  420. g.setStroke(new BasicStroke(2));
  421. break;
  422. case Working:
  423. g.setColor(new Color(13, 175, 28));
  424. g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 1));
  425. break;
  426. }
  427. g.drawLine(start.x, start.y, end.x, end.y);
  428. Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
  429. g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  430. g.drawString(currentEnergy + "/" + (unlimited?"\u221E":capacity) , middle.x, middle.y);
  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. if(visualState == null) System.out.println("AHHH");
  549. for(ExitCable cable : visualState.getExitCableList()) {
  550. paintExitCable(g2d, cable);
  551. }
  552. for(DecoratedCable cable : visualState.getCableList()) {
  553. paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
  554. }
  555. for(DecoratedGroupNode dGroupNode : visualState.getGroupNodeList()) {
  556. paintGroupNode(g2d, dGroupNode);
  557. }
  558. for(Consumer con: visualState.getConsumerList()) {
  559. paintConsumer(g2d, con);
  560. }
  561. for(Supplier sup: visualState.getSupplierList()) {
  562. paintSupplier(g2d, sup);
  563. }
  564. for(Passiv pas: visualState.getPassivList()) {
  565. paintCanvasObject(g2d, pas);
  566. }
  567. for(DecoratedSwitch dSwitch : visualState.getSwitchList()) {
  568. paintSwitch(g2d, dSwitch);
  569. }
  570. for(Node node : visualState.getNodeList()) {
  571. drawCanvasObject(g2d, "/Images/node.png" , node.getPosition());
  572. }
  573. //-->oldCode
  574. if (doMark) {
  575. g2d.setColor(Color.BLACK);
  576. g2d.setStroke(new BasicStroke(0));
  577. drawMarker(g2d);
  578. }
  579. //Test Selection
  580. //Objects:
  581. g2d.setColor(Color.BLUE);
  582. g2d.setStroke(new BasicStroke(1));
  583. Color transparentGrey = new Color(128, 174, 247, 40);
  584. for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
  585. if(aCps instanceof Node) {
  586. Position pos = aCps.getPosition();
  587. g2d.setColor(transparentGrey);
  588. g2d.fillOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  589. g2d.setColor(Color.LIGHT_GRAY);
  590. g2d.setStroke(new BasicStroke(2));
  591. g2d.drawOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  592. }
  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(Util.loadImage("/Images/replace.png") ,
  607. pos.x + controller.getScaleDiv2(),
  608. pos.y - controller.getScale(),
  609. controller.getScaleDiv2(), controller.getScaleDiv2(), null);
  610. }
  611. //<-- OldCode
  612. }
  613. @Override
  614. public void mouseClicked(MouseEvent e) {
  615. if(!disabled){
  616. if (e.getButton() == MouseEvent.BUTTON1) {
  617. DefaulTable propertyTable = model.getPropertyTable();
  618. if (propertyTable.getRowCount() > 0) {
  619. for (int i = propertyTable.getRowCount() - 1; i > -1; i--) {
  620. propertyTable.removeRow(i);
  621. }
  622. }
  623. triggerUpdateController();
  624. }
  625. stopEditing();
  626. }
  627. }
  628. @Override
  629. public void mouseEntered(MouseEvent e) {
  630. }
  631. @Override
  632. public void mouseExited(MouseEvent e) {
  633. }
  634. @Override
  635. public void mousePressed(MouseEvent e) {
  636. if(!disabled){
  637. stopEditing();
  638. tempCps = null;
  639. edgeHighlight = null;
  640. controller.setSelecteEdge(null);
  641. // Object Selection
  642. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  643. cx = cps.getPosition().x - controller.getScaleDiv2();
  644. cy = cps.getPosition().y - controller.getScaleDiv2();
  645. if (x - controller.getScale() <= cx
  646. && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  647. tempCps = cps;
  648. dragging = true;
  649. if (e.isControlDown() && tempCps != null) {
  650. if (model.getSelectedCpsObjects().contains(tempCps)) {
  651. controller.deleteSelectedObject(tempCps);
  652. //TODO: RemoveDepth
  653. } else {
  654. controller.addSelectedObject(tempCps);
  655. if(tempCps instanceof GroupNode)
  656. controller.getObjectsInDepth();
  657. }
  658. }
  659. // If drawing an Edge (CTRL down)
  660. if (tempCps.getClass() == HolonObject.class) {
  661. HolonObject tempObj = ((HolonObject) tempCps);
  662. dataSelected = tempObj.getElements();
  663. }
  664. if (e.isShiftDown()) {
  665. drawEdge = true;
  666. dragging = false;
  667. }
  668. }
  669. }
  670. // Edge Selection
  671. if (tempCps == null) {
  672. edgeHighlight = mousePositionOnEdge(x, y);
  673. controller.setSelecteEdge(edgeHighlight);
  674. controller.setSelectedObjectID(0);
  675. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  676. model.getSelectedCpsObjects().clear();
  677. }
  678. }
  679. if (edgeHighlight == null && tempCps == null) {
  680. sx = e.getX();
  681. sy = e.getY();
  682. doMark = true;
  683. }
  684. repaint();
  685. }
  686. }
  687. @Override
  688. public void mouseReleased(MouseEvent e) {
  689. if(!disabled){
  690. x = e.getX();
  691. y = e.getY();
  692. dragging = false;
  693. if (drawEdge) {
  694. drawEdge = false;
  695. drawDeleteEdge();
  696. }
  697. if (dragged) {
  698. try {
  699. /**
  700. * Save before further Dragged interactions happen
  701. */
  702. controller.autoSave();
  703. } catch (IOException ex) {
  704. System.err.println("AutoSave error by dragging");
  705. ex.printStackTrace();
  706. }
  707. /**
  708. * check if a unique tempCps could replace an Object on the canvas
  709. */
  710. if(model.getSelectedCpsObjects().size()==1
  711. && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
  712. /**
  713. * if UpperNode would be replaced, close its tabs
  714. */
  715. if(mayBeReplaced instanceof GroupNode)
  716. closeUpperNodeTab(mayBeReplaced.getId());
  717. /**
  718. * replace on canvas (will save)
  719. */
  720. controller.replaceCanvasObject(mayBeReplaced, tempCps);
  721. mayBeReplaced=null;
  722. }
  723. }
  724. if (!e.isControlDown() && !dragged && tempCps != null
  725. && MouseEvent.BUTTON3 != e.getButton()) {
  726. model.getSelectedCpsObjects().clear();
  727. controller.addSelectedObject(tempCps);
  728. model.setSelectedCpsObject(tempCps);
  729. if(tempCps instanceof GroupNode)
  730. controller.getObjectsInDepth();
  731. }
  732. dragged = false;
  733. // Rightclick List
  734. setRightClickMenu(e);
  735. markObjects();
  736. if (doubleClick() && tempCps instanceof HolonSwitch
  737. && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
  738. ((HolonSwitch) tempCps).switchState();
  739. }
  740. controller.calculateStateAndVisualForTimeStep(model.getCurIteration());
  741. triggerUpdateController();
  742. repaint();
  743. }
  744. }
  745. @Override
  746. public void mouseDragged(MouseEvent e) {
  747. if(!disabled){
  748. // If Edge is drawn
  749. x = e.getX();
  750. y = e.getY();
  751. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  752. model.getSelectedCpsObjects().clear();
  753. if (tempCps != null) {
  754. controller.addSelectedObject(tempCps);
  755. }
  756. }
  757. if (dragging) {
  758. try {
  759. dragged = true;
  760. float xDist, yDist; // Distance
  761. x = e.getX();
  762. y = e.getY();
  763. // Make sure its in bounds
  764. int scaleDiv2 = controller.getScaleDiv2();
  765. if (e.getX() < scaleDiv2)
  766. x = scaleDiv2;
  767. else if (e.getX() > this.getWidth() - scaleDiv2)
  768. x = this.getWidth() - scaleDiv2;
  769. if (e.getY() < scaleDiv2)
  770. y = scaleDiv2;
  771. else if (e.getY() > this.getHeight()
  772. - scaleDiv2)
  773. y = this.getHeight() - scaleDiv2;
  774. // Distance
  775. xDist = x - tempCps.getPosition().x;
  776. yDist = y - tempCps.getPosition().y;
  777. tempCps.setPosition(x, y); // Drag Position
  778. // ToolTipText Position and name
  779. toolTip = true;
  780. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  781. toolTipPos.x = tempCps.getPosition().x
  782. - scaleDiv2;
  783. toolTipPos.y = tempCps.getPosition().y
  784. + scaleDiv2;
  785. // All Selected Objects
  786. for (AbstractCanvasObject cps : model.getSelectedCpsObjects()) {
  787. if (cps != tempCps) {
  788. x = (int) (cps.getPosition().x + xDist);
  789. y = (int) (cps.getPosition().y + yDist);
  790. // Make sure its in bounds
  791. if (x <= scaleDiv2)
  792. x = scaleDiv2;
  793. else if (x > this.getWidth()
  794. - scaleDiv2)
  795. x = this.getWidth() - scaleDiv2;
  796. if (y <= scaleDiv2)
  797. y = scaleDiv2;
  798. else if (y > this.getHeight()
  799. - scaleDiv2)
  800. y = this.getHeight() - scaleDiv2;
  801. cps.setPosition(x, y);
  802. }
  803. }
  804. /**
  805. * check if something might be replaced
  806. */
  807. if(model.getSelectedCpsObjects().size()==1)
  808. checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
  809. repaint();
  810. } catch (Exception eex) {
  811. }
  812. }
  813. // Mark Objects
  814. if (doMark) {
  815. tempSelected.clear();
  816. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  817. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  818. if (sx >= x) {
  819. x1 = x;
  820. x2 = sx;
  821. }
  822. if (sy >= y) {
  823. y1 = y;
  824. y2 = sy;
  825. }
  826. if (x1 <= cps.getPosition().x + model.getScaleDiv2()
  827. && y1 <= cps.getPosition().y + model.getScaleDiv2()
  828. && x2 >= cps.getPosition().x
  829. && y2 >= cps.getPosition().y) {
  830. tempSelected.add(cps);
  831. }
  832. }
  833. }
  834. repaint();
  835. }
  836. }
  837. @Override
  838. public void mouseMoved(MouseEvent e) {
  839. if(!disabled){
  840. x = e.getX();
  841. y = e.getY();
  842. // Everything for the tooltip :)
  843. boolean on = false;
  844. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  845. cx = cps.getPosition().x - controller.getScaleDiv2();
  846. cy = cps.getPosition().y - controller.getScaleDiv2();
  847. on = setToolTipInfoAndPosition(on, cps);
  848. }
  849. if(on||(!on && toolTip))
  850. repaint();
  851. toolTip = on;
  852. }
  853. }
  854. /**
  855. * Draws or Deletes an Edge.
  856. */
  857. void drawDeleteEdge() {
  858. if (getMousePosition() != null) {
  859. boolean node = true;
  860. boolean newEdge = true;
  861. boolean onEdge = true;
  862. boolean deleteNode = false;
  863. Edge e = null;
  864. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  865. cx = cps.getPosition().x - controller.getScaleDiv2();
  866. cy = cps.getPosition().y - controller.getScaleDiv2();
  867. if (x - controller.getScale() <= cx
  868. && y - controller.getScale() <= cy && x >= cx
  869. && y >= cy && cps != tempCps) {
  870. node = false;
  871. onEdge = false;
  872. for (Edge p : tempCps.getConnections()) {
  873. if ((p.getA() == tempCps && p.getB() == cps)
  874. || (p.getB() == tempCps && p.getA() == cps)) {
  875. newEdge = false;
  876. e = p;
  877. }
  878. }
  879. if (!newEdge) {
  880. controller.removeEdgesOnCanvas(e);
  881. // Node ohne Edge?
  882. if (e.getA().getClass() == Node.class
  883. && e.getA().getConnections().isEmpty()) {
  884. tempCps = e.getA();
  885. deleteNode = true;
  886. }
  887. if (e.getB().getClass() == Node.class
  888. && e.getB().getConnections().isEmpty()) {
  889. deleteNode = true;
  890. }
  891. } else {
  892. if(!(cps instanceof GroupNode || tempCps instanceof GroupNode)) {
  893. e = new Edge(cps, tempCps, model.getMaxCapacity());
  894. controller.addEdgeOnCanvas(e);
  895. }else if (cps instanceof GroupNode && !(tempCps instanceof GroupNode)){
  896. GroupNode thisUpperNode = (GroupNode)cps;
  897. Object[] possibilities = thisUpperNode.getNodes().stream().map(aCps -> new ACpsHandle(aCps)).filter(aCpsHandle -> !(aCpsHandle.object instanceof GroupNode)).toArray();
  898. if(possibilities.length != 0) {
  899. 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, "");
  900. if(selected != null) {
  901. e = new Edge(selected.object, tempCps, model.getMaxCapacity());
  902. controller.addEdgeOnCanvas(e);
  903. }
  904. }else {
  905. Node n = new Node("Node");
  906. n.setPosition(x, y);
  907. controller.addObjUpperNode(n, thisUpperNode);
  908. e = new Edge(n, tempCps, model.getMaxCapacity());
  909. controller.addEdgeOnCanvas(e);
  910. }
  911. }
  912. }
  913. }
  914. }
  915. // Edge auf eine Edge gezogen?
  916. if (onEdge && !checkForReplacement(x, y)) {
  917. Edge p = mousePositionOnEdge(x, y);
  918. if (p != null) {
  919. Edge e1;
  920. Edge e2;
  921. node = false;
  922. Node n = new Node("Node");
  923. n.setPosition(x, y);
  924. controller.addObjectCanvas(n);
  925. AbstractCanvasObject r, k;
  926. r = p.getA();
  927. k = p.getB();
  928. e = new Edge(n, tempCps, model.getMaxCapacity());
  929. e1 = new Edge(n, r, model.getMaxCapacity());
  930. e2 = new Edge(n, k, model.getMaxCapacity());
  931. controller.removeEdgesOnCanvas(p);
  932. controller.addEdgeOnCanvas(e);
  933. controller.addEdgeOnCanvas(e1);
  934. controller.addEdgeOnCanvas(e2);
  935. }
  936. }else{
  937. mayBeReplaced = null;
  938. }
  939. // ins leere Gedragged
  940. if (node && !checkForReplacement(x, y)) {
  941. Node n = new Node("Node");
  942. n.setPosition(x, y);
  943. controller.addObjectCanvas(n);
  944. e = new Edge(n, tempCps, model.getMaxCapacity());
  945. controller.addEdgeOnCanvas(e);
  946. }else{
  947. mayBeReplaced = null;
  948. }
  949. // Wenn ein Node ohne Connections da ist
  950. if (deleteNode) {
  951. controller.delCanvasObject(tempCps, true);
  952. tempCps = null;
  953. }
  954. }
  955. }
  956. /**
  957. * Checks if the mouse is on an Edge.
  958. *
  959. * @param x
  960. * Position of the Mouse
  961. * @param y
  962. * Position of the Mouse
  963. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  964. */
  965. private Edge mousePositionOnEdge(int x, int y) {
  966. x += controller.getScaleDiv2();
  967. y += controller.getScaleDiv2();
  968. for (Edge p : model.getEdgesOnCanvas()) {
  969. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA()
  970. .getPosition().y, p.getB().getPosition().x, p.getB()
  971. .getPosition().y);
  972. int[] positions = determineMousePositionOnEdge(p);
  973. int lx = positions[0];
  974. int ly = positions[1];
  975. int hx = positions[2];
  976. int hy = positions[3];
  977. // distance from a point to a line and between both Objects
  978. if (l.ptLineDistSq(x - model.getScaleDiv2(),
  979. y - model.getScaleDiv2()) < 20
  980. && x > lx && x < hx && y > ly && y < hy) {
  981. return p;
  982. }
  983. }
  984. return null;
  985. }
  986. void updateLanguages() {
  987. itemCut.setText(Languages.getLanguage()[95]);
  988. itemCopy.setText(Languages.getLanguage()[96]);
  989. itemPaste.setText(Languages.getLanguage()[97]);
  990. itemDelete.setText(Languages.getLanguage()[98]);
  991. itemGroup.setText(Languages.getLanguage()[99]);
  992. itemUngroup.setText(Languages.getLanguage()[100]);
  993. itemTrack.setText(Languages.getLanguage()[101]);
  994. itemUntrack.setText(Languages.getLanguage()[102]);
  995. }
  996. /**
  997. * Set if Information should be shown.
  998. *
  999. * @param connection
  1000. * boolean for conecction
  1001. * @param object
  1002. * boolean for objects
  1003. * @param nodeOfnode
  1004. */
  1005. void setShowedInformation(boolean connection, boolean object,
  1006. boolean border, boolean nodeOfnode) {
  1007. showConnectionInformation = connection;
  1008. showedInformation[1] = object;
  1009. showedInformation[3] = border;
  1010. showedInformation[4] = nodeOfnode;
  1011. }
  1012. /**
  1013. * Returns if Information should be shown.
  1014. *
  1015. * @return Array of boolean [0] = connection, [1] = objects
  1016. */
  1017. boolean[] getShowedInformation() {
  1018. return showedInformation;
  1019. }
  1020. /**
  1021. * set toolTip
  1022. *
  1023. * @param bool
  1024. */
  1025. void setToolTip(boolean bool) {
  1026. this.toolTip = bool;
  1027. }
  1028. /**
  1029. * Set the Mouse
  1030. *
  1031. * @param x
  1032. * @param y
  1033. */
  1034. void setXY(int x, int y) {
  1035. this.x = x;
  1036. this.y = y;
  1037. }
  1038. @Override
  1039. public boolean checkForReplacement(int x, int y) {
  1040. return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
  1041. }
  1042. @Override
  1043. public void tryToAlignObjects(){
  1044. /**
  1045. * Align all Objects
  1046. */
  1047. for(AbstractCanvasObject cps: model.getObjectsOnCanvas())
  1048. align(cps,3*model.getScaleDiv2());
  1049. /**
  1050. * AutoSave new Positons
  1051. */
  1052. try{
  1053. controller.autoSave();
  1054. } catch (IOException ex) {
  1055. System.err.println("AutoSave error by aligning");
  1056. ex.printStackTrace();
  1057. }
  1058. }
  1059. @Override
  1060. public void closeUpperNodeTab(int upperNodeId) {
  1061. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
  1062. .getParent().getParent().getParent();
  1063. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  1064. if (((GroupNodeCanvas) ((JScrollPane) tabbedPaneInner
  1065. .getComponentAt(i)).getViewport().getComponent(
  1066. 0)).upperNode.getId() == upperNodeId) {
  1067. tabbedPaneInner.remove(i);
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. }