MyCanvas.java 37 KB

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