MyCanvas.java 36 KB

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