MyCanvas.java 35 KB

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