MyCanvas.java 36 KB

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