MyCanvas.java 36 KB

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