MyCanvas.java 38 KB

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