MyCanvas.java 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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 utility.Util;
  16. import ui.model.Model;
  17. import ui.model.Passiv;
  18. import ui.model.Supplier;
  19. import ui.model.VisualRepresentationalState;
  20. import javax.swing.*;
  21. import java.awt.*;
  22. import java.awt.datatransfer.UnsupportedFlavorException;
  23. import java.awt.event.ActionEvent;
  24. import java.awt.event.KeyEvent;
  25. import java.awt.event.KeyListener;
  26. import java.awt.event.MouseEvent;
  27. import java.awt.event.MouseListener;
  28. import java.awt.event.MouseMotionListener;
  29. import java.awt.font.LineMetrics;
  30. import java.awt.geom.Line2D;
  31. import java.awt.image.BufferedImage;
  32. import java.io.IOException;
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35. import java.util.HashSet;
  36. import java.util.stream.Collectors;
  37. /**
  38. * This Class is the Canvas. All Objects will be visualized here
  39. *
  40. * @author Gruppe14
  41. */
  42. public class MyCanvas extends AbstractCanvas implements MouseListener,
  43. MouseMotionListener {
  44. private static final long serialVersionUID = 1L;
  45. public boolean disabled = false;
  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. showConnectionInformation = 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 (AbstractCanvasObject 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 (AbstractCanvasObject 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.calculateStateAndVisualForCurrentTimeStep();
  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 = ((GroupNode) tempCps).getNodes();
  140. controller.delUpperNode((GroupNode) 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 (AbstractCanvasObject 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.calculateStateAndVisualForCurrentTimeStep();
  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 (AbstractCanvasObject 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 (AbstractCanvasObject o : model.getSelectedCpsObjects()) {
  196. if (o instanceof HolonObject) {
  197. boolean found = false;
  198. if (controller.getTrackingObj() != null) {
  199. for (AbstractCanvasObject 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() == Node.class && edgeHighlight.getA().getConnections().size() == 0){
  225. controller.delCanvasObject(edgeHighlight.getA(), false);
  226. }
  227. if(edgeHighlight.getB().getClass() == Node.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. AbstractCanvasObject 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 GroupNode) {
  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. && ((GroupNodeCanvas) ((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. && ((GroupNodeCanvas) ((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 GroupNode)
  296. controller.getObjectsInDepth();
  297. controller.copy(null);
  298. itemPaste.setEnabled(true);
  299. repaint();
  300. });
  301. itemAlign.addActionListener(actionEvent ->
  302. {
  303. for(AbstractCanvasObject 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. if(showConnectionInformation) {
  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. }
  408. private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
  409. {
  410. drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage(): HolonSwitch.getSwitchClosedImage() , dSwitch.getModel().getPosition());
  411. }
  412. private void paintExitCable(Graphics2D g, ExitCable eCable) {
  413. Position start = eCable.getStart().getPosition();
  414. Position end = eCable.getFinish().getPosition();
  415. float currentEnergy = eCable.getCable().getFlowEnergy();
  416. float capacity = eCable.getCable().getModel().getCapacity();
  417. boolean unlimited = eCable.getCable().getModel().isUnlimitedCapacity();
  418. switch(eCable.getCable().getState()) {
  419. case Burned:
  420. g.setColor(Color.RED);
  421. g.setStroke(new BasicStroke(2));
  422. break;
  423. case Working:
  424. g.setColor(new Color(13, 175, 28));
  425. g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 1));
  426. break;
  427. }
  428. g.drawLine(start.x, start.y, end.x, end.y);
  429. Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
  430. g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  431. g.drawString(currentEnergy + "/" + (unlimited?"\u221E":capacity) , middle.x, middle.y);
  432. }
  433. private void paintGroupNode(Graphics2D g, DecoratedGroupNode dGroupNode) {
  434. Position pos = dGroupNode.getModel().getPosition();
  435. g.setColor(Color.lightGray);
  436. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
  437. drawCanvasObject(g, "/Images/upper_node.png" , pos);
  438. paintGroupNodeBar(g, dGroupNode, pos);
  439. }
  440. private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode , Position pos) {
  441. // +1, -2, -1 little Adjustment for pixel perfect alignment
  442. int barWidth = (int) (controller.getScale());
  443. int barHeight = (int) (controller.getScale() / 5);
  444. g.setColor(Color.WHITE);
  445. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  446. float[] percentages = getGroupNodeBarPercentages(dGroupNode);
  447. Color[] colors = new Color[6];
  448. colors[0] = getStateColor(HolonObjectState.PRODUCER);
  449. colors[1] = getStateColor(HolonObjectState.NOT_SUPPLIED);
  450. colors[2] = getStateColor(HolonObjectState.PARTIALLY_SUPPLIED);
  451. colors[3] = getStateColor(HolonObjectState.SUPPLIED);
  452. colors[4] = getStateColor(HolonObjectState.OVER_SUPPLIED);
  453. colors[5] = getStateColor(HolonObjectState.NO_ENERGY);
  454. for(int i = 5; i>=0; i--) {
  455. g.setColor(colors[i]);
  456. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * percentages[i] - 1), barHeight);
  457. }
  458. // g.setColor(color);
  459. // g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  460. g.setColor(Color.BLACK);
  461. g.setStroke(new BasicStroke(1));
  462. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  463. }
  464. /**
  465. * HardCoded Stuff dont try at Home ;)
  466. * @param dGroupNode
  467. * @return
  468. */
  469. public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
  470. int[] amountOfObjects = new int[6];
  471. amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
  472. amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  473. amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  474. amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  475. amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  476. amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
  477. int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3] + amountOfObjects[4] + amountOfObjects[5];
  478. float[] percentages = new float[6];
  479. int count = 0;
  480. for(int i = 0; i < 6; i++) {
  481. count += amountOfObjects[i];
  482. percentages[i] = (float)count / (float)countHolonObjects;
  483. }
  484. return percentages;
  485. }
  486. private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
  487. // +1, -2, -1 little Adjustment for pixel perfect alignment
  488. int barWidth = (int) (controller.getScale());
  489. int barHeight = (int) (controller.getScale() / 5);
  490. g.setColor(Color.WHITE);
  491. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  492. g.setColor(color);
  493. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  494. g.setColor(Color.BLACK);
  495. g.setStroke(new BasicStroke(1));
  496. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  497. g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  498. String percentageString = (Math.round((percentage * 100))) + "%";
  499. int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  500. if(percentage > 1.0f) g.setColor(Color.WHITE); //Just to see better on purple
  501. g.drawString(percentageString, pos.x + 1 - stringWidth / 2, pos.y + controller.getScaleDiv2() - 1+ barHeight);
  502. }
  503. //old code
  504. void drawMarker(Graphics2D g) {
  505. Color transparentGrey = new Color(128, 174, 247, 40);
  506. if (sx > x && sy > y) {
  507. g.drawRect(x, y, sx - x, sy - y);
  508. g.setColor(transparentGrey);
  509. g.fillRect(x, y, sx - x, sy - y);
  510. } else if (sx < x && sy < y) {
  511. g.drawRect(sx, sy, x - sx, y - sy);
  512. g.setColor(transparentGrey);
  513. g.fillRect(sx, sy, x - sx, y - sy);
  514. } else if (sx >= x) {
  515. g.drawRect(x, sy, sx - x, y - sy);
  516. g.setColor(transparentGrey);
  517. g.fillRect(x, sy, sx - x, y - sy);
  518. } else if (sy >= y) {
  519. g.drawRect(sx, y, x - sx, sy - y);
  520. g.setColor(transparentGrey);
  521. g.fillRect(sx, y, x - sx, sy - y);
  522. }
  523. }
  524. public void paintComponent(Graphics g) {
  525. super.paintComponent(g);
  526. Graphics2D g2d = (Graphics2D) g;
  527. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  528. RenderingHints.VALUE_ANTIALIAS_ON));
  529. //-->Old code
  530. if (drawEdge) {
  531. g2d.setColor(Color.BLACK);
  532. g2d.setStroke(new BasicStroke(1));
  533. g2d.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  534. }
  535. //<--
  536. //SelectedCable
  537. HashSet<Edge> selectedEdges = new HashSet<Edge>();
  538. for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
  539. for(Edge edge: aCps.getConnections()) {
  540. selectedEdges.add(edge);
  541. }
  542. }
  543. if(model.getSelectedEdge() != null) selectedEdges.add(model.getSelectedEdge());
  544. //timstep:
  545. g.setFont(new Font("TimesNewRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  546. g2d.setColor(Color.BLACK);
  547. VisualRepresentationalState visualState = controller.getSimManager().getActualVisualRepresentationalState();
  548. //VisualState Representation:
  549. if(visualState == null) System.out.println("AHHH");
  550. for(ExitCable cable : visualState.getExitCableList()) {
  551. paintExitCable(g2d, cable);
  552. }
  553. for(DecoratedCable cable : visualState.getCableList()) {
  554. paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
  555. }
  556. for(DecoratedGroupNode dGroupNode : visualState.getGroupNodeList()) {
  557. paintGroupNode(g2d, dGroupNode);
  558. }
  559. for(Consumer con: visualState.getConsumerList()) {
  560. paintConsumer(g2d, con);
  561. }
  562. for(Supplier sup: visualState.getSupplierList()) {
  563. paintSupplier(g2d, sup);
  564. }
  565. for(Passiv pas: visualState.getPassivList()) {
  566. paintCanvasObject(g2d, pas);
  567. }
  568. for(DecoratedSwitch dSwitch : visualState.getSwitchList()) {
  569. paintSwitch(g2d, dSwitch);
  570. }
  571. for(Node node : visualState.getNodeList()) {
  572. drawCanvasObject(g2d, "/Images/node.png" , node.getPosition());
  573. }
  574. //-->oldCode
  575. if (doMark) {
  576. g2d.setColor(Color.BLACK);
  577. g2d.setStroke(new BasicStroke(0));
  578. drawMarker(g2d);
  579. }
  580. //Test Selection
  581. //Objects:
  582. g2d.setColor(Color.BLUE);
  583. g2d.setStroke(new BasicStroke(1));
  584. Color transparentGrey = new Color(128, 174, 247, 40);
  585. for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
  586. if(aCps instanceof Node) {
  587. Position pos = aCps.getPosition();
  588. g2d.setColor(transparentGrey);
  589. g2d.fillOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  590. g2d.setColor(Color.LIGHT_GRAY);
  591. g2d.setStroke(new BasicStroke(2));
  592. g2d.drawOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  593. }
  594. else {
  595. Position pos = aCps.getPosition();
  596. g2d.setColor(transparentGrey);
  597. 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));
  598. g2d.setColor(Color.LIGHT_GRAY);
  599. g2d.setStroke(new BasicStroke(2));
  600. 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));
  601. }
  602. }
  603. //maybeReplace:
  604. if(mayBeReplaced != null){
  605. g2d.setColor(Color.RED);
  606. Position pos = mayBeReplaced.getPosition();
  607. g2d.drawImage(Util.loadImage("/Images/replace.png") ,
  608. pos.x + controller.getScaleDiv2(),
  609. pos.y - controller.getScale(),
  610. controller.getScaleDiv2(), controller.getScaleDiv2(), null);
  611. }
  612. //<-- OldCode
  613. }
  614. @Override
  615. public void mouseClicked(MouseEvent e) {
  616. if(!disabled){
  617. if (e.getButton() == MouseEvent.BUTTON1) {
  618. DefaulTable propertyTable = model.getPropertyTable();
  619. if (propertyTable.getRowCount() > 0) {
  620. for (int i = propertyTable.getRowCount() - 1; i > -1; i--) {
  621. propertyTable.removeRow(i);
  622. }
  623. }
  624. triggerUpdateController();
  625. }
  626. stopEditing();
  627. }
  628. }
  629. @Override
  630. public void mouseEntered(MouseEvent e) {
  631. }
  632. @Override
  633. public void mouseExited(MouseEvent e) {
  634. }
  635. @Override
  636. public void mousePressed(MouseEvent e) {
  637. if(!disabled){
  638. stopEditing();
  639. tempCps = null;
  640. edgeHighlight = null;
  641. controller.setSelecteEdge(null);
  642. // Object Selection
  643. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  644. cx = cps.getPosition().x - controller.getScaleDiv2();
  645. cy = cps.getPosition().y - controller.getScaleDiv2();
  646. if (x - controller.getScale() <= cx
  647. && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  648. tempCps = cps;
  649. dragging = true;
  650. if (e.isControlDown() && tempCps != null) {
  651. if (model.getSelectedCpsObjects().contains(tempCps)) {
  652. controller.deleteSelectedObject(tempCps);
  653. //TODO: RemoveDepth
  654. } else {
  655. controller.addSelectedObject(tempCps);
  656. if(tempCps instanceof GroupNode)
  657. controller.getObjectsInDepth();
  658. }
  659. }
  660. // If drawing an Edge (CTRL down)
  661. if (tempCps.getClass() == HolonObject.class) {
  662. HolonObject tempObj = ((HolonObject) tempCps);
  663. dataSelected = tempObj.getElements();
  664. }
  665. if (e.isShiftDown()) {
  666. drawEdge = true;
  667. dragging = false;
  668. }
  669. }
  670. }
  671. // Edge Selection
  672. if (tempCps == null) {
  673. edgeHighlight = mousePositionOnEdge(x, y);
  674. controller.setSelecteEdge(edgeHighlight);
  675. controller.setSelectedObjectID(0);
  676. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  677. model.getSelectedCpsObjects().clear();
  678. }
  679. }
  680. if (edgeHighlight == null && tempCps == null) {
  681. sx = e.getX();
  682. sy = e.getY();
  683. doMark = true;
  684. }
  685. repaint();
  686. }
  687. }
  688. @Override
  689. public void mouseReleased(MouseEvent e) {
  690. if(!disabled){
  691. x = e.getX();
  692. y = e.getY();
  693. dragging = false;
  694. if (drawEdge) {
  695. drawEdge = false;
  696. drawDeleteEdge();
  697. }
  698. if (dragged) {
  699. try {
  700. /**
  701. * Save before further Dragged interactions happen
  702. */
  703. controller.autoSave();
  704. } catch (IOException ex) {
  705. System.err.println("AutoSave error by dragging");
  706. ex.printStackTrace();
  707. }
  708. /**
  709. * check if a unique tempCps could replace an Object on the canvas
  710. */
  711. if(model.getSelectedCpsObjects().size()==1
  712. && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
  713. /**
  714. * if UpperNode would be replaced, close its tabs
  715. */
  716. if(mayBeReplaced instanceof GroupNode)
  717. closeUpperNodeTab(mayBeReplaced.getId());
  718. /**
  719. * replace on canvas (will save)
  720. */
  721. controller.replaceCanvasObject(mayBeReplaced, tempCps);
  722. mayBeReplaced=null;
  723. }
  724. }
  725. if (!e.isControlDown() && !dragged && tempCps != null
  726. && MouseEvent.BUTTON3 != e.getButton()) {
  727. model.getSelectedCpsObjects().clear();
  728. controller.addSelectedObject(tempCps);
  729. model.setSelectedCpsObject(tempCps);
  730. if(tempCps instanceof GroupNode)
  731. controller.getObjectsInDepth();
  732. }
  733. dragged = false;
  734. // Rightclick List
  735. setRightClickMenu(e);
  736. markObjects();
  737. if (doubleClick() && tempCps instanceof HolonSwitch
  738. && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
  739. ((HolonSwitch) tempCps).switchState();
  740. }
  741. controller.calculateStateAndVisualForTimeStep(model.getCurIteration());
  742. triggerUpdateController();
  743. repaint();
  744. }
  745. }
  746. @Override
  747. public void mouseDragged(MouseEvent e) {
  748. if(!disabled){
  749. // If Edge is drawn
  750. x = e.getX();
  751. y = e.getY();
  752. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  753. model.getSelectedCpsObjects().clear();
  754. if (tempCps != null) {
  755. controller.addSelectedObject(tempCps);
  756. }
  757. }
  758. if (dragging) {
  759. try {
  760. dragged = true;
  761. float xDist, yDist; // Distance
  762. x = e.getX();
  763. y = e.getY();
  764. // Make sure its in bounds
  765. int scaleDiv2 = controller.getScaleDiv2();
  766. if (e.getX() < scaleDiv2)
  767. x = scaleDiv2;
  768. else if (e.getX() > this.getWidth() - scaleDiv2)
  769. x = this.getWidth() - scaleDiv2;
  770. if (e.getY() < scaleDiv2)
  771. y = scaleDiv2;
  772. else if (e.getY() > this.getHeight()
  773. - scaleDiv2)
  774. y = this.getHeight() - scaleDiv2;
  775. // Distance
  776. xDist = x - tempCps.getPosition().x;
  777. yDist = y - tempCps.getPosition().y;
  778. tempCps.setPosition(x, y); // Drag Position
  779. // ToolTipText Position and name
  780. toolTip = true;
  781. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  782. toolTipPos.x = tempCps.getPosition().x
  783. - scaleDiv2;
  784. toolTipPos.y = tempCps.getPosition().y
  785. + scaleDiv2;
  786. // All Selected Objects
  787. for (AbstractCanvasObject cps : model.getSelectedCpsObjects()) {
  788. if (cps != tempCps) {
  789. x = (int) (cps.getPosition().x + xDist);
  790. y = (int) (cps.getPosition().y + yDist);
  791. // Make sure its in bounds
  792. if (x <= scaleDiv2)
  793. x = scaleDiv2;
  794. else if (x > this.getWidth()
  795. - scaleDiv2)
  796. x = this.getWidth() - scaleDiv2;
  797. if (y <= scaleDiv2)
  798. y = scaleDiv2;
  799. else if (y > this.getHeight()
  800. - scaleDiv2)
  801. y = this.getHeight() - scaleDiv2;
  802. cps.setPosition(x, y);
  803. }
  804. }
  805. /**
  806. * check if something might be replaced
  807. */
  808. if(model.getSelectedCpsObjects().size()==1)
  809. checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
  810. repaint();
  811. } catch (Exception eex) {
  812. }
  813. }
  814. // Mark Objects
  815. if (doMark) {
  816. tempSelected.clear();
  817. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  818. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  819. if (sx >= x) {
  820. x1 = x;
  821. x2 = sx;
  822. }
  823. if (sy >= y) {
  824. y1 = y;
  825. y2 = sy;
  826. }
  827. if (x1 <= cps.getPosition().x + model.getScaleDiv2()
  828. && y1 <= cps.getPosition().y + model.getScaleDiv2()
  829. && x2 >= cps.getPosition().x
  830. && y2 >= cps.getPosition().y) {
  831. tempSelected.add(cps);
  832. }
  833. }
  834. }
  835. repaint();
  836. }
  837. }
  838. @Override
  839. public void mouseMoved(MouseEvent e) {
  840. if(!disabled){
  841. x = e.getX();
  842. y = e.getY();
  843. // Everything for the tooltip :)
  844. boolean on = false;
  845. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  846. cx = cps.getPosition().x - controller.getScaleDiv2();
  847. cy = cps.getPosition().y - controller.getScaleDiv2();
  848. on = setToolTipInfoAndPosition(on, cps);
  849. }
  850. if(on||(!on && toolTip))
  851. repaint();
  852. toolTip = on;
  853. }
  854. }
  855. /**
  856. * Draws or Deletes an Edge.
  857. */
  858. void drawDeleteEdge() {
  859. if (getMousePosition() != null) {
  860. boolean node = true;
  861. boolean newEdge = true;
  862. boolean onEdge = true;
  863. boolean deleteNode = false;
  864. Edge e = null;
  865. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  866. cx = cps.getPosition().x - controller.getScaleDiv2();
  867. cy = cps.getPosition().y - controller.getScaleDiv2();
  868. if (x - controller.getScale() <= cx
  869. && y - controller.getScale() <= cy && x >= cx
  870. && y >= cy && cps != tempCps) {
  871. node = false;
  872. onEdge = false;
  873. for (Edge p : tempCps.getConnections()) {
  874. if ((p.getA() == tempCps && p.getB() == cps)
  875. || (p.getB() == tempCps && p.getA() == cps)) {
  876. newEdge = false;
  877. e = p;
  878. }
  879. }
  880. if (!newEdge) {
  881. controller.removeEdgesOnCanvas(e);
  882. // Node ohne Edge?
  883. if (e.getA().getClass() == Node.class
  884. && e.getA().getConnections().isEmpty()) {
  885. tempCps = e.getA();
  886. deleteNode = true;
  887. }
  888. if (e.getB().getClass() == Node.class
  889. && e.getB().getConnections().isEmpty()) {
  890. deleteNode = true;
  891. }
  892. } else {
  893. if(!(cps instanceof GroupNode || tempCps instanceof GroupNode)) {
  894. e = new Edge(cps, tempCps, model.getMaxCapacity());
  895. controller.addEdgeOnCanvas(e);
  896. }else if (cps instanceof GroupNode && !(tempCps instanceof GroupNode)){
  897. GroupNode thisUpperNode = (GroupNode)cps;
  898. Object[] possibilities = thisUpperNode.getNodes().stream().map(aCps -> new ACpsHandle(aCps)).filter(aCpsHandle -> !(aCpsHandle.object instanceof GroupNode)).toArray();
  899. if(possibilities.length != 0) {
  900. 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, "");
  901. if(selected != null) {
  902. e = new Edge(selected.object, tempCps, model.getMaxCapacity());
  903. controller.addEdgeOnCanvas(e);
  904. }
  905. }else {
  906. Node n = new Node("Node");
  907. n.setPosition(x, y);
  908. controller.addObjUpperNode(n, thisUpperNode);
  909. e = new Edge(n, tempCps, model.getMaxCapacity());
  910. controller.addEdgeOnCanvas(e);
  911. }
  912. }
  913. }
  914. }
  915. }
  916. // Edge auf eine Edge gezogen?
  917. if (onEdge && !checkForReplacement(x, y)) {
  918. Edge p = mousePositionOnEdge(x, y);
  919. if (p != null) {
  920. Edge e1;
  921. Edge e2;
  922. node = false;
  923. Node n = new Node("Node");
  924. n.setPosition(x, y);
  925. controller.addObjectCanvas(n);
  926. AbstractCanvasObject r, k;
  927. r = p.getA();
  928. k = p.getB();
  929. e = new Edge(n, tempCps, model.getMaxCapacity());
  930. e1 = new Edge(n, r, model.getMaxCapacity());
  931. e2 = new Edge(n, k, model.getMaxCapacity());
  932. controller.removeEdgesOnCanvas(p);
  933. controller.addEdgeOnCanvas(e);
  934. controller.addEdgeOnCanvas(e1);
  935. controller.addEdgeOnCanvas(e2);
  936. }
  937. }else{
  938. mayBeReplaced = null;
  939. }
  940. // ins leere Gedragged
  941. if (node && !checkForReplacement(x, y)) {
  942. Node n = new Node("Node");
  943. n.setPosition(x, y);
  944. controller.addObjectCanvas(n);
  945. e = new Edge(n, tempCps, model.getMaxCapacity());
  946. controller.addEdgeOnCanvas(e);
  947. }else{
  948. mayBeReplaced = null;
  949. }
  950. // Wenn ein Node ohne Connections da ist
  951. if (deleteNode) {
  952. controller.delCanvasObject(tempCps, true);
  953. tempCps = null;
  954. }
  955. }
  956. }
  957. /**
  958. * Checks if the mouse is on an Edge.
  959. *
  960. * @param x
  961. * Position of the Mouse
  962. * @param y
  963. * Position of the Mouse
  964. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  965. */
  966. private Edge mousePositionOnEdge(int x, int y) {
  967. x += controller.getScaleDiv2();
  968. y += controller.getScaleDiv2();
  969. for (Edge p : model.getEdgesOnCanvas()) {
  970. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA()
  971. .getPosition().y, p.getB().getPosition().x, p.getB()
  972. .getPosition().y);
  973. int[] positions = determineMousePositionOnEdge(p);
  974. int lx = positions[0];
  975. int ly = positions[1];
  976. int hx = positions[2];
  977. int hy = positions[3];
  978. // distance from a point to a line and between both Objects
  979. if (l.ptLineDistSq(x - model.getScaleDiv2(),
  980. y - model.getScaleDiv2()) < 20
  981. && x > lx && x < hx && y > ly && y < hy) {
  982. return p;
  983. }
  984. }
  985. return null;
  986. }
  987. void updateLanguages() {
  988. itemCut.setText(Languages.getLanguage()[95]);
  989. itemCopy.setText(Languages.getLanguage()[96]);
  990. itemPaste.setText(Languages.getLanguage()[97]);
  991. itemDelete.setText(Languages.getLanguage()[98]);
  992. itemGroup.setText(Languages.getLanguage()[99]);
  993. itemUngroup.setText(Languages.getLanguage()[100]);
  994. itemTrack.setText(Languages.getLanguage()[101]);
  995. itemUntrack.setText(Languages.getLanguage()[102]);
  996. }
  997. /**
  998. * Set if Information should be shown.
  999. *
  1000. * @param connection
  1001. * boolean for conecction
  1002. * @param object
  1003. * boolean for objects
  1004. * @param nodeOfnode
  1005. */
  1006. void setShowedInformation(boolean connection, boolean object,
  1007. boolean border, boolean nodeOfnode) {
  1008. showConnectionInformation = connection;
  1009. showedInformation[1] = object;
  1010. showedInformation[3] = border;
  1011. showedInformation[4] = nodeOfnode;
  1012. }
  1013. /**
  1014. * Returns if Information should be shown.
  1015. *
  1016. * @return Array of boolean [0] = connection, [1] = objects
  1017. */
  1018. boolean[] getShowedInformation() {
  1019. return showedInformation;
  1020. }
  1021. /**
  1022. * set toolTip
  1023. *
  1024. * @param bool
  1025. */
  1026. void setToolTip(boolean bool) {
  1027. this.toolTip = bool;
  1028. }
  1029. /**
  1030. * Set the Mouse
  1031. *
  1032. * @param x
  1033. * @param y
  1034. */
  1035. void setXY(int x, int y) {
  1036. this.x = x;
  1037. this.y = y;
  1038. }
  1039. @Override
  1040. public boolean checkForReplacement(int x, int y) {
  1041. return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
  1042. }
  1043. @Override
  1044. public void tryToAlignObjects(){
  1045. /**
  1046. * Align all Objects
  1047. */
  1048. for(AbstractCanvasObject cps: model.getObjectsOnCanvas())
  1049. align(cps,3*model.getScaleDiv2());
  1050. /**
  1051. * AutoSave new Positons
  1052. */
  1053. try{
  1054. controller.autoSave();
  1055. } catch (IOException ex) {
  1056. System.err.println("AutoSave error by aligning");
  1057. ex.printStackTrace();
  1058. }
  1059. }
  1060. @Override
  1061. public void closeUpperNodeTab(int upperNodeId) {
  1062. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
  1063. .getParent().getParent().getParent();
  1064. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  1065. if (((GroupNodeCanvas) ((JScrollPane) tabbedPaneInner
  1066. .getComponentAt(i)).getViewport().getComponent(
  1067. 0)).upperNode.getId() == upperNodeId) {
  1068. tabbedPaneInner.remove(i);
  1069. break;
  1070. }
  1071. }
  1072. }
  1073. }