MyCanvas.java 38 KB

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