MyCanvas.java 36 KB

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