MyCanvas.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  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.ImageImport;
  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.ungroupGroupNode((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. JTabbedPane tabbedPane = (JTabbedPane)getParent().getParent()
  243. .getParent().getParent();
  244. // Look if the uppernode is open in a Tab
  245. for (int i = 4; i < tabbedPane.getTabCount(); i++) {
  246. if (tabbedPane.getComponentAt(i) != null
  247. && ((GroupNodeCanvas) ((JScrollPane) tabbedPane
  248. .getComponentAt(i)).getViewport()
  249. .getComponent(0)).upperNode.getId() == cps
  250. .getId()) {
  251. ((ButtonTabComponent) tabbedPane
  252. .getTabComponentAt(i)).removeTabs();
  253. break;
  254. }
  255. }
  256. }
  257. toolTip = false;
  258. }
  259. model.getSelectedCpsObjects().clear();
  260. tempCps = null;
  261. repaint();
  262. });
  263. itemCut.addActionListener(actionEvent -> {
  264. controller.cut(null);
  265. itemPaste.setEnabled(true);
  266. repaint();
  267. });
  268. itemCopy.addActionListener(actionEvent -> {
  269. if(tempCps instanceof GroupNode)
  270. controller.getObjectsInDepth();
  271. controller.copy(null);
  272. itemPaste.setEnabled(true);
  273. repaint();
  274. });
  275. itemAlign.addActionListener(actionEvent ->
  276. {
  277. for(AbstractCanvasObject cps: model.getSelectedCpsObjects())
  278. align(cps, model.getScaleDiv2());
  279. repaint();
  280. });
  281. itemPaste
  282. .addActionListener(actionEvent -> {
  283. try {
  284. controller.paste(null, mousePosition);
  285. unitGraph.update(model.getSelectedCpsObjects());
  286. } catch (JsonParseException | UnsupportedFlavorException
  287. | IOException e1) {
  288. JLabel message = new JLabel(
  289. "The Clipboard information cannot be pastet into Application.");
  290. JOptionPane.showMessageDialog(null, message, "",
  291. JOptionPane.ERROR_MESSAGE);
  292. }
  293. repaint();
  294. });
  295. /*
  296. * create Template
  297. */
  298. itemCreateTemplate.addActionListener(actionEvent -> {
  299. controller.createTemplate((HolonObject)tempCps,(JFrame)SwingUtilities.getRoot(this));
  300. });
  301. this.addMouseListener(this);
  302. this.addMouseMotionListener(this);
  303. }
  304. /**
  305. * Paints all Components on the Canvas.
  306. *
  307. * @param g
  308. * Graphics
  309. */
  310. private Color getStateColor(HolonObjectState state) {
  311. switch(state) {
  312. case NOT_SUPPLIED:
  313. return new Color(230, 120, 100);
  314. case NO_ENERGY:
  315. return Color.white;
  316. case OVER_SUPPLIED:
  317. return new Color(166, 78, 229);
  318. case PARTIALLY_SUPPLIED:
  319. return Color.yellow;
  320. case PRODUCER:
  321. return Color.lightGray;
  322. case SUPPLIED:
  323. return new Color(13, 175, 28);
  324. default:
  325. return Color.BLACK;
  326. }
  327. }
  328. private void paintCanvasObject(Graphics2D g, DecoratedHolonObject decoratedHolonObject){
  329. Position pos = decoratedHolonObject.getModel().getPosition();
  330. Color statecolor = getStateColor(decoratedHolonObject.getState());
  331. g.setColor(statecolor);
  332. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
  333. drawCanvasObject(g, decoratedHolonObject.getModel().getImage(), pos);
  334. }
  335. private void drawCanvasObjectString(Graphics2D g, Position posOfCanvasObject, float energy) {
  336. g.setColor(Color.BLACK);
  337. g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (controller.getScale() / 4f) ));
  338. g.drawString((energy > 0)? "+" + Float.toString(energy): Float.toString(energy), posOfCanvasObject.x - controller.getScaleDiv2(), posOfCanvasObject.y - controller.getScaleDiv2() - 1);
  339. }
  340. private void paintConsumer(Graphics2D g, Consumer con){
  341. paintCanvasObject(g, con);
  342. paintSupplyBar(g,con.getSupplyBarPercentage(), getStateColor(con.getState()), con.getModel().getPosition());
  343. drawCanvasObjectString(g, con.getModel().getPosition(), -con.getEnergyNeededFromNetwork());
  344. }
  345. private void paintSupplier(Graphics2D g, Supplier sup){
  346. paintCanvasObject(g, sup);
  347. drawCanvasObjectString(g, sup.getModel().getPosition(), sup.getEnergyToSupplyNetwork());
  348. }
  349. private void drawCanvasObject(Graphics2D g, String Image, Position pos) {
  350. g.drawImage(ImageImport.loadImage(Image, controller.getScale(), controller.getScale()) ,
  351. pos.x - controller.getScaleDiv2(),
  352. pos.y - controller.getScaleDiv2(),
  353. controller.getScale(), controller.getScale() , null);
  354. }
  355. private void paintCable(Graphics2D g, DecoratedCable cable, boolean isSelected)
  356. {
  357. Position start = cable.getModel().getA().getPosition();
  358. Position end = cable.getModel().getB().getPosition();
  359. float currentEnergy = cable.getFlowEnergy();
  360. float capacity = cable.getModel().getCapacity();
  361. boolean unlimited = cable.getModel().isUnlimitedCapacity();
  362. switch(cable.getState()) {
  363. case Burned:
  364. g.setColor(Color.RED);
  365. g.setStroke(new BasicStroke(2));
  366. break;
  367. case Working:
  368. g.setColor(new Color(13, 175, 28));
  369. g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 1));
  370. break;
  371. }
  372. if(isSelected){
  373. g.setColor(Color.lightGray);
  374. }
  375. g.drawLine(start.x, start.y, end.x, end.y);
  376. if(showConnectionInformation) {
  377. Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
  378. g.setFont(new Font("TimesRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  379. g.drawString(currentEnergy + "/" + (unlimited?"\u221E":capacity) , middle.x, middle.y);
  380. }
  381. }
  382. private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
  383. {
  384. drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage(): HolonSwitch.getSwitchClosedImage() , dSwitch.getModel().getPosition());
  385. }
  386. private void paintExitCable(Graphics2D g, ExitCable eCable) {
  387. Position start = eCable.getStart().getPosition();
  388. Position end = eCable.getFinish().getPosition();
  389. float currentEnergy = eCable.getCable().getFlowEnergy();
  390. float capacity = eCable.getCable().getModel().getCapacity();
  391. boolean unlimited = eCable.getCable().getModel().isUnlimitedCapacity();
  392. switch(eCable.getCable().getState()) {
  393. case Burned:
  394. g.setColor(Color.RED);
  395. g.setStroke(new BasicStroke(2));
  396. break;
  397. case Working:
  398. g.setColor(new Color(13, 175, 28));
  399. g.setStroke(new BasicStroke(unlimited?2f:(currentEnergy / capacity * 2f) + 1));
  400. break;
  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 paintGroupNode(Graphics2D g, DecoratedGroupNode dGroupNode) {
  408. Position pos = dGroupNode.getModel().getPosition();
  409. g.setColor(Color.lightGray);
  410. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
  411. drawCanvasObject(g, "/Images/upper_node.png" , pos);
  412. paintGroupNodeBar(g, dGroupNode, pos);
  413. }
  414. private void paintGroupNodeBar(Graphics2D g, DecoratedGroupNode dGroupNode , Position pos) {
  415. // +1, -2, -1 little Adjustment for pixel perfect alignment
  416. int barWidth = (int) (controller.getScale());
  417. int barHeight = (int) (controller.getScale() / 5);
  418. g.setColor(Color.WHITE);
  419. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  420. float[] percentages = getGroupNodeBarPercentages(dGroupNode);
  421. Color[] colors = new Color[6];
  422. colors[0] = getStateColor(HolonObjectState.PRODUCER);
  423. colors[1] = getStateColor(HolonObjectState.NOT_SUPPLIED);
  424. colors[2] = getStateColor(HolonObjectState.PARTIALLY_SUPPLIED);
  425. colors[3] = getStateColor(HolonObjectState.SUPPLIED);
  426. colors[4] = getStateColor(HolonObjectState.OVER_SUPPLIED);
  427. colors[5] = getStateColor(HolonObjectState.NO_ENERGY);
  428. for(int i = 5; i>=0; i--) {
  429. g.setColor(colors[i]);
  430. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * percentages[i] - 1), barHeight);
  431. }
  432. // g.setColor(color);
  433. // g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  434. g.setColor(Color.BLACK);
  435. g.setStroke(new BasicStroke(1));
  436. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  437. }
  438. /**
  439. * HardCoded Stuff dont try at Home ;)
  440. * @param dGroupNode
  441. * @return
  442. */
  443. public float[] getGroupNodeBarPercentages(DecoratedGroupNode dGroupNode) {
  444. int[] amountOfObjects = new int[6];
  445. amountOfObjects[0] = dGroupNode.getAmountOfSupplier();
  446. amountOfObjects[1] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.NOT_SUPPLIED);
  447. amountOfObjects[2] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.PARTIALLY_SUPPLIED);
  448. amountOfObjects[3] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.SUPPLIED);
  449. amountOfObjects[4] = dGroupNode.getAmountOfConsumerWithState(HolonObjectState.OVER_SUPPLIED);
  450. amountOfObjects[5] = dGroupNode.getAmountOfPassiv();
  451. int countHolonObjects = amountOfObjects[0] + amountOfObjects[1] + amountOfObjects[2] + amountOfObjects[3] + amountOfObjects[4] + amountOfObjects[5];
  452. float[] percentages = new float[6];
  453. int count = 0;
  454. for(int i = 0; i < 6; i++) {
  455. count += amountOfObjects[i];
  456. percentages[i] = (float)count / (float)countHolonObjects;
  457. }
  458. return percentages;
  459. }
  460. private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
  461. // +1, -2, -1 little Adjustment for pixel perfect alignment
  462. int barWidth = (int) (controller.getScale());
  463. int barHeight = (int) (controller.getScale() / 5);
  464. g.setColor(Color.WHITE);
  465. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  466. g.setColor(color);
  467. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  468. g.setColor(Color.BLACK);
  469. g.setStroke(new BasicStroke(1));
  470. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  471. g.setFont(new Font("TimesNewRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  472. String percentageString = (Math.round((percentage * 100))) + "%";
  473. int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  474. if(percentage > 1.0f) g.setColor(Color.WHITE); //Just to see better on purple
  475. g.drawString(percentageString, pos.x + 1 - stringWidth / 2, pos.y + controller.getScaleDiv2() - 1+ barHeight);
  476. }
  477. //old code
  478. void drawMarker(Graphics2D g) {
  479. Color transparentGrey = new Color(128, 174, 247, 40);
  480. if (sx > x && sy > y) {
  481. g.drawRect(x, y, sx - x, sy - y);
  482. g.setColor(transparentGrey);
  483. g.fillRect(x, y, sx - x, sy - y);
  484. } else if (sx < x && sy < y) {
  485. g.drawRect(sx, sy, x - sx, y - sy);
  486. g.setColor(transparentGrey);
  487. g.fillRect(sx, sy, x - sx, y - sy);
  488. } else if (sx >= x) {
  489. g.drawRect(x, sy, sx - x, y - sy);
  490. g.setColor(transparentGrey);
  491. g.fillRect(x, sy, sx - x, y - sy);
  492. } else if (sy >= y) {
  493. g.drawRect(sx, y, x - sx, sy - y);
  494. g.setColor(transparentGrey);
  495. g.fillRect(sx, y, x - sx, sy - y);
  496. }
  497. }
  498. public void paintComponent(Graphics g) {
  499. super.paintComponent(g);
  500. Graphics2D g2d = (Graphics2D) g;
  501. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  502. RenderingHints.VALUE_ANTIALIAS_ON));
  503. //-->Old code
  504. if (drawEdge) {
  505. g2d.setColor(Color.BLACK);
  506. g2d.setStroke(new BasicStroke(1));
  507. g2d.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  508. }
  509. //<--
  510. //SelectedCable
  511. HashSet<Edge> selectedEdges = new HashSet<Edge>();
  512. for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
  513. for(Edge edge: aCps.getConnections()) {
  514. selectedEdges.add(edge);
  515. }
  516. }
  517. if(model.getSelectedEdge() != null) selectedEdges.add(model.getSelectedEdge());
  518. //timstep:
  519. g.setFont(new Font("TimesNewRoman", Font.PLAIN, Math.max((int) (controller.getScale() / 3.5f), 10) ));
  520. g2d.setColor(Color.BLACK);
  521. VisualRepresentationalState visualState = controller.getSimManager().getActualVisualRepresentationalState();
  522. //VisualState Representation:
  523. if(visualState == null) System.out.println("AHHH");
  524. for(ExitCable cable : visualState.getExitCableList()) {
  525. paintExitCable(g2d, cable);
  526. }
  527. for(DecoratedCable cable : visualState.getCableList()) {
  528. paintCable(g2d, cable, selectedEdges.contains(cable.getModel()));
  529. }
  530. for(DecoratedGroupNode dGroupNode : visualState.getGroupNodeList()) {
  531. paintGroupNode(g2d, dGroupNode);
  532. }
  533. for(Consumer con: visualState.getConsumerList()) {
  534. paintConsumer(g2d, con);
  535. }
  536. for(Supplier sup: visualState.getSupplierList()) {
  537. paintSupplier(g2d, sup);
  538. }
  539. for(Passiv pas: visualState.getPassivList()) {
  540. paintCanvasObject(g2d, pas);
  541. }
  542. for(DecoratedSwitch dSwitch : visualState.getSwitchList()) {
  543. paintSwitch(g2d, dSwitch);
  544. }
  545. for(Node node : visualState.getNodeList()) {
  546. drawCanvasObject(g2d, "/Images/node.png" , node.getPosition());
  547. }
  548. //-->oldCode
  549. if (doMark) {
  550. g2d.setColor(Color.BLACK);
  551. g2d.setStroke(new BasicStroke(0));
  552. drawMarker(g2d);
  553. }
  554. //Test Selection
  555. //Objects:
  556. g2d.setColor(Color.BLUE);
  557. g2d.setStroke(new BasicStroke(1));
  558. Color transparentGrey = new Color(128, 174, 247, 40);
  559. for(AbstractCanvasObject aCps: model.getSelectedCpsObjects()) {
  560. if(aCps instanceof Node) {
  561. Position pos = aCps.getPosition();
  562. g2d.setColor(transparentGrey);
  563. g2d.fillOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  564. g2d.setColor(Color.LIGHT_GRAY);
  565. g2d.setStroke(new BasicStroke(2));
  566. g2d.drawOval(pos.x - (int) (controller.getScaleDiv2()), pos.y - (int) (controller.getScaleDiv2()), controller.getScale(), controller.getScale());
  567. }
  568. else {
  569. Position pos = aCps.getPosition();
  570. g2d.setColor(transparentGrey);
  571. 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));
  572. g2d.setColor(Color.LIGHT_GRAY);
  573. g2d.setStroke(new BasicStroke(2));
  574. 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));
  575. }
  576. }
  577. //maybeReplace:
  578. if(mayBeReplaced != null){
  579. g2d.setColor(Color.RED);
  580. Position pos = mayBeReplaced.getPosition();
  581. g2d.drawImage(ImageImport.loadImage("/Images/replace.png") ,
  582. pos.x + controller.getScaleDiv2(),
  583. pos.y - controller.getScale(),
  584. controller.getScaleDiv2(), controller.getScaleDiv2(), null);
  585. }
  586. //<-- OldCode
  587. }
  588. @Override
  589. public void mouseClicked(MouseEvent e) {
  590. if(!disabled){
  591. if (e.getButton() == MouseEvent.BUTTON1) {
  592. DefaulTable propertyTable = model.getPropertyTable();
  593. if (propertyTable.getRowCount() > 0) {
  594. for (int i = propertyTable.getRowCount() - 1; i > -1; i--) {
  595. propertyTable.removeRow(i);
  596. }
  597. }
  598. triggerUpdateController();
  599. }
  600. stopEditing();
  601. }
  602. }
  603. @Override
  604. public void mouseEntered(MouseEvent e) {
  605. }
  606. @Override
  607. public void mouseExited(MouseEvent e) {
  608. }
  609. @Override
  610. public void mousePressed(MouseEvent e) {
  611. if(!disabled){
  612. stopEditing();
  613. tempCps = null;
  614. edgeHighlight = null;
  615. controller.setSelecteEdge(null);
  616. // Object Selection
  617. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  618. cx = cps.getPosition().x - controller.getScaleDiv2();
  619. cy = cps.getPosition().y - controller.getScaleDiv2();
  620. if (x - controller.getScale() <= cx
  621. && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  622. tempCps = cps;
  623. dragging = true;
  624. if (e.isControlDown() && tempCps != null) {
  625. if (model.getSelectedCpsObjects().contains(tempCps)) {
  626. controller.deleteSelectedObject(tempCps);
  627. //TODO: RemoveDepth
  628. } else {
  629. controller.addSelectedObject(tempCps);
  630. if(tempCps instanceof GroupNode)
  631. controller.getObjectsInDepth();
  632. }
  633. }
  634. // If drawing an Edge (CTRL down)
  635. if (tempCps.getClass() == HolonObject.class) {
  636. HolonObject tempObj = ((HolonObject) tempCps);
  637. dataSelected = tempObj.getElements();
  638. }
  639. if (e.isShiftDown()) {
  640. drawEdge = true;
  641. dragging = false;
  642. }
  643. }
  644. }
  645. // Edge Selection
  646. if (tempCps == null) {
  647. edgeHighlight = mousePositionOnEdge(x, y);
  648. controller.setSelecteEdge(edgeHighlight);
  649. controller.setSelectedObjectID(0);
  650. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  651. model.getSelectedCpsObjects().clear();
  652. }
  653. }
  654. if (edgeHighlight == null && tempCps == null) {
  655. sx = e.getX();
  656. sy = e.getY();
  657. doMark = true;
  658. }
  659. repaint();
  660. }
  661. }
  662. @Override
  663. public void mouseReleased(MouseEvent e) {
  664. if(!disabled){
  665. x = e.getX();
  666. y = e.getY();
  667. dragging = false;
  668. if (drawEdge) {
  669. drawEdge = false;
  670. drawDeleteEdge();
  671. }
  672. if (dragged) {
  673. try {
  674. /**
  675. * Save before further Dragged interactions happen
  676. */
  677. controller.autoSave();
  678. } catch (IOException ex) {
  679. System.err.println("AutoSave error by dragging");
  680. ex.printStackTrace();
  681. }
  682. /**
  683. * check if a unique tempCps could replace an Object on the canvas
  684. */
  685. if(model.getSelectedCpsObjects().size()==1
  686. && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
  687. /**
  688. * if UpperNode would be replaced, close its tabs
  689. */
  690. if(mayBeReplaced instanceof GroupNode)
  691. closeUpperNodeTab(mayBeReplaced.getId());
  692. /**
  693. * replace on canvas (will save)
  694. */
  695. controller.replaceCanvasObject(mayBeReplaced, tempCps);
  696. mayBeReplaced=null;
  697. }
  698. }
  699. if (!e.isControlDown() && !dragged && tempCps != null
  700. && MouseEvent.BUTTON3 != e.getButton()) {
  701. model.getSelectedCpsObjects().clear();
  702. controller.addSelectedObject(tempCps);
  703. model.setSelectedCpsObject(tempCps);
  704. if(tempCps instanceof GroupNode)
  705. controller.getObjectsInDepth();
  706. }
  707. dragged = false;
  708. // Rightclick List
  709. setRightClickMenu(e);
  710. markObjects();
  711. if (doubleClick() && tempCps instanceof HolonSwitch
  712. && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
  713. ((HolonSwitch) tempCps).switchState();
  714. }
  715. controller.calculateStateAndVisualForTimeStep(model.getCurIteration());
  716. triggerUpdateController();
  717. repaint();
  718. }
  719. }
  720. @Override
  721. public void mouseDragged(MouseEvent e) {
  722. if(!disabled){
  723. // If Edge is drawn
  724. x = e.getX();
  725. y = e.getY();
  726. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  727. model.getSelectedCpsObjects().clear();
  728. if (tempCps != null) {
  729. controller.addSelectedObject(tempCps);
  730. }
  731. }
  732. if (dragging) {
  733. try {
  734. dragged = true;
  735. float xDist, yDist; // Distance
  736. x = e.getX();
  737. y = e.getY();
  738. // Make sure its in bounds
  739. int scaleDiv2 = controller.getScaleDiv2();
  740. if (e.getX() < scaleDiv2)
  741. x = scaleDiv2;
  742. else if (e.getX() > this.getWidth() - scaleDiv2)
  743. x = this.getWidth() - scaleDiv2;
  744. if (e.getY() < scaleDiv2)
  745. y = scaleDiv2;
  746. else if (e.getY() > this.getHeight()
  747. - scaleDiv2)
  748. y = this.getHeight() - scaleDiv2;
  749. // Distance
  750. xDist = x - tempCps.getPosition().x;
  751. yDist = y - tempCps.getPosition().y;
  752. tempCps.setPosition(x, y); // Drag Position
  753. // ToolTipText Position and name
  754. toolTip = true;
  755. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  756. toolTipPos.x = tempCps.getPosition().x
  757. - scaleDiv2;
  758. toolTipPos.y = tempCps.getPosition().y
  759. + scaleDiv2;
  760. // All Selected Objects
  761. for (AbstractCanvasObject cps : model.getSelectedCpsObjects()) {
  762. if (cps != tempCps) {
  763. x = (int) (cps.getPosition().x + xDist);
  764. y = (int) (cps.getPosition().y + yDist);
  765. // Make sure its in bounds
  766. if (x <= scaleDiv2)
  767. x = scaleDiv2;
  768. else if (x > this.getWidth()
  769. - scaleDiv2)
  770. x = this.getWidth() - scaleDiv2;
  771. if (y <= scaleDiv2)
  772. y = scaleDiv2;
  773. else if (y > this.getHeight()
  774. - scaleDiv2)
  775. y = this.getHeight() - scaleDiv2;
  776. cps.setPosition(x, y);
  777. }
  778. }
  779. /**
  780. * check if something might be replaced
  781. */
  782. if(model.getSelectedCpsObjects().size()==1)
  783. checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
  784. repaint();
  785. } catch (Exception eex) {
  786. }
  787. }
  788. // Mark Objects
  789. if (doMark) {
  790. tempSelected.clear();
  791. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  792. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  793. if (sx >= x) {
  794. x1 = x;
  795. x2 = sx;
  796. }
  797. if (sy >= y) {
  798. y1 = y;
  799. y2 = sy;
  800. }
  801. if (x1 <= cps.getPosition().x + model.getScaleDiv2()
  802. && y1 <= cps.getPosition().y + model.getScaleDiv2()
  803. && x2 >= cps.getPosition().x
  804. && y2 >= cps.getPosition().y) {
  805. tempSelected.add(cps);
  806. }
  807. }
  808. }
  809. repaint();
  810. }
  811. }
  812. @Override
  813. public void mouseMoved(MouseEvent e) {
  814. if(!disabled){
  815. x = e.getX();
  816. y = e.getY();
  817. // Everything for the tooltip :)
  818. boolean on = false;
  819. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  820. cx = cps.getPosition().x - controller.getScaleDiv2();
  821. cy = cps.getPosition().y - controller.getScaleDiv2();
  822. on = setToolTipInfoAndPosition(on, cps);
  823. }
  824. if(on||(!on && toolTip))
  825. repaint();
  826. toolTip = on;
  827. }
  828. }
  829. /**
  830. * Draws or Deletes an Edge.
  831. */
  832. void drawDeleteEdge() {
  833. if (getMousePosition() != null) {
  834. boolean node = true;
  835. boolean newEdge = true;
  836. boolean onEdge = true;
  837. boolean deleteNode = false;
  838. Edge e = null;
  839. for (AbstractCanvasObject cps : model.getObjectsOnCanvas()) {
  840. cx = cps.getPosition().x - controller.getScaleDiv2();
  841. cy = cps.getPosition().y - controller.getScaleDiv2();
  842. if (x - controller.getScale() <= cx
  843. && y - controller.getScale() <= cy && x >= cx
  844. && y >= cy && cps != tempCps) {
  845. node = false;
  846. onEdge = false;
  847. for (Edge p : tempCps.getConnections()) {
  848. if ((p.getA() == tempCps && p.getB() == cps)
  849. || (p.getB() == tempCps && p.getA() == cps)) {
  850. newEdge = false;
  851. e = p;
  852. }
  853. }
  854. if (!newEdge) {
  855. controller.removeEdgesOnCanvas(e);
  856. // Node ohne Edge?
  857. if (e.getA().getClass() == Node.class
  858. && e.getA().getConnections().isEmpty()) {
  859. tempCps = e.getA();
  860. deleteNode = true;
  861. }
  862. if (e.getB().getClass() == Node.class
  863. && e.getB().getConnections().isEmpty()) {
  864. deleteNode = true;
  865. }
  866. } else {
  867. if(!(cps instanceof GroupNode || tempCps instanceof GroupNode)) {
  868. e = new Edge(cps, tempCps, model.getMaxCapacity());
  869. controller.addEdgeOnCanvas(e);
  870. }else if (cps instanceof GroupNode && !(tempCps instanceof GroupNode)){
  871. GroupNode thisUpperNode = (GroupNode)cps;
  872. Object[] possibilities = thisUpperNode.getNodes().stream().map(aCps -> new ACpsHandle(aCps)).filter(aCpsHandle -> !(aCpsHandle.object instanceof GroupNode)).toArray();
  873. if(possibilities.length != 0) {
  874. 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, "");
  875. if(selected != null) {
  876. e = new Edge(selected.object, tempCps, model.getMaxCapacity());
  877. controller.addEdgeOnCanvas(e);
  878. }
  879. }else {
  880. Node n = new Node("Node");
  881. n.setPosition(x, y);
  882. controller.addObjUpperNode(n, thisUpperNode);
  883. e = new Edge(n, tempCps, model.getMaxCapacity());
  884. controller.addEdgeOnCanvas(e);
  885. }
  886. }
  887. }
  888. }
  889. }
  890. // Edge auf eine Edge gezogen?
  891. if (onEdge && !checkForReplacement(x, y)) {
  892. Edge p = mousePositionOnEdge(x, y);
  893. if (p != null) {
  894. Edge e1;
  895. Edge e2;
  896. node = false;
  897. Node n = new Node("Node");
  898. n.setPosition(x, y);
  899. controller.addObjectCanvas(n);
  900. AbstractCanvasObject r, k;
  901. r = p.getA();
  902. k = p.getB();
  903. e = new Edge(n, tempCps, model.getMaxCapacity());
  904. e1 = new Edge(n, r, model.getMaxCapacity());
  905. e2 = new Edge(n, k, model.getMaxCapacity());
  906. controller.removeEdgesOnCanvas(p);
  907. controller.addEdgeOnCanvas(e);
  908. controller.addEdgeOnCanvas(e1);
  909. controller.addEdgeOnCanvas(e2);
  910. }
  911. }else{
  912. mayBeReplaced = null;
  913. }
  914. // ins leere Gedragged
  915. if (node && !checkForReplacement(x, y)) {
  916. Node n = new Node("Node");
  917. n.setPosition(x, y);
  918. controller.addObjectCanvas(n);
  919. e = new Edge(n, tempCps, model.getMaxCapacity());
  920. controller.addEdgeOnCanvas(e);
  921. }else{
  922. mayBeReplaced = null;
  923. }
  924. // Wenn ein Node ohne Connections da ist
  925. if (deleteNode) {
  926. controller.delCanvasObject(tempCps, true);
  927. tempCps = null;
  928. }
  929. }
  930. }
  931. /**
  932. * Checks if the mouse is on an Edge.
  933. *
  934. * @param x
  935. * Position of the Mouse
  936. * @param y
  937. * Position of the Mouse
  938. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  939. */
  940. private Edge mousePositionOnEdge(int x, int y) {
  941. x += controller.getScaleDiv2();
  942. y += controller.getScaleDiv2();
  943. for (Edge p : model.getEdgesOnCanvas()) {
  944. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA()
  945. .getPosition().y, p.getB().getPosition().x, p.getB()
  946. .getPosition().y);
  947. int[] positions = determineMousePositionOnEdge(p);
  948. int lx = positions[0];
  949. int ly = positions[1];
  950. int hx = positions[2];
  951. int hy = positions[3];
  952. // distance from a point to a line and between both Objects
  953. if (l.ptLineDistSq(x - model.getScaleDiv2(),
  954. y - model.getScaleDiv2()) < 20
  955. && x > lx && x < hx && y > ly && y < hy) {
  956. return p;
  957. }
  958. }
  959. return null;
  960. }
  961. void updateLanguages() {
  962. itemCut.setText(Languages.getLanguage()[95]);
  963. itemCopy.setText(Languages.getLanguage()[96]);
  964. itemPaste.setText(Languages.getLanguage()[97]);
  965. itemDelete.setText(Languages.getLanguage()[98]);
  966. itemGroup.setText(Languages.getLanguage()[99]);
  967. itemUngroup.setText(Languages.getLanguage()[100]);
  968. itemTrack.setText(Languages.getLanguage()[101]);
  969. itemUntrack.setText(Languages.getLanguage()[102]);
  970. }
  971. /**
  972. * Set if Information should be shown.
  973. *
  974. * @param connection
  975. * boolean for conecction
  976. * @param object
  977. * boolean for objects
  978. * @param nodeOfnode
  979. */
  980. void setShowedInformation(boolean connection, boolean object,
  981. boolean border, boolean nodeOfnode) {
  982. showConnectionInformation = connection;
  983. showedInformation[1] = object;
  984. showedInformation[3] = border;
  985. showedInformation[4] = nodeOfnode;
  986. }
  987. /**
  988. * Returns if Information should be shown.
  989. *
  990. * @return Array of boolean [0] = connection, [1] = objects
  991. */
  992. boolean[] getShowedInformation() {
  993. return showedInformation;
  994. }
  995. /**
  996. * set toolTip
  997. *
  998. * @param bool
  999. */
  1000. void setToolTip(boolean bool) {
  1001. this.toolTip = bool;
  1002. }
  1003. /**
  1004. * Set the Mouse
  1005. *
  1006. * @param x
  1007. * @param y
  1008. */
  1009. void setXY(int x, int y) {
  1010. this.x = x;
  1011. this.y = y;
  1012. }
  1013. @Override
  1014. public boolean checkForReplacement(int x, int y) {
  1015. return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
  1016. }
  1017. @Override
  1018. public void tryToAlignObjects(){
  1019. /**
  1020. * Align all Objects
  1021. */
  1022. for(AbstractCanvasObject cps: model.getObjectsOnCanvas())
  1023. align(cps,3*model.getScaleDiv2());
  1024. /**
  1025. * AutoSave new Positons
  1026. */
  1027. try{
  1028. controller.autoSave();
  1029. } catch (IOException ex) {
  1030. System.err.println("AutoSave error by aligning");
  1031. ex.printStackTrace();
  1032. }
  1033. }
  1034. @Override
  1035. public void closeUpperNodeTab(int upperNodeId) {
  1036. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
  1037. .getParent().getParent().getParent();
  1038. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  1039. if (((GroupNodeCanvas) ((JScrollPane) tabbedPaneInner
  1040. .getComponentAt(i)).getViewport().getComponent(
  1041. 0)).upperNode.getId() == upperNodeId) {
  1042. tabbedPaneInner.remove(i);
  1043. break;
  1044. }
  1045. }
  1046. }
  1047. }