MyCanvas.java 35 KB

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