MyCanvas.java 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. //System.out.println(decoratedHolonObject.getModel().getImage());
  365. Position pos = decoratedHolonObject.getModel().getPosition();
  366. Color statecolor = getStateColor(decoratedHolonObject.getState());
  367. g.setColor(statecolor);
  368. //System.out.println(statecolor);
  369. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y - controller.getScaleDiv2(), controller.getScale(), controller.getScale());
  370. drawCanvasObject(g, decoratedHolonObject.getModel().getImage(), pos);
  371. }
  372. //if(decoratedHolonObject.getState() != HolonObjectState.PRODUCER && decoratedHolonObject.getState() != HolonObjectState.NO_ENERGY)paintSupplyBar(g, decoratedHolonObject.get, statecolor, pos);
  373. private void paintConsumer(Graphics2D g, Consumer con){
  374. paintCanvasObject(g, con);
  375. paintSupplyBar(g,con.getSupplyBarPercentage(), getStateColor(con.getState()), con.getModel().getPosition());
  376. }
  377. private void drawCanvasObject(Graphics2D g, String Image, Position pos) {
  378. g.drawImage(Util.loadImage(Image) ,
  379. pos.x - controller.getScaleDiv2(),
  380. pos.y - controller.getScaleDiv2(),
  381. controller.getScale(), controller.getScale(), null);
  382. }
  383. private void paintCable(Graphics2D g, DecoratedCable cable)
  384. {
  385. Position start = cable.getModel().getA().getPosition();
  386. Position end = cable.getModel().getB().getPosition();
  387. float currentEnergy = cable.getFlowEnergy();
  388. float capacity = cable.getModel().getCapacity();
  389. g.setColor(Color.BLACK);
  390. g.setStroke(new BasicStroke(2));
  391. switch(cable.getState()) {
  392. case Burned:
  393. g.setColor(Color.RED);
  394. g.setStroke(new BasicStroke(2));
  395. break;
  396. case Working:
  397. g.setColor(Color.GREEN);
  398. g.setStroke(new BasicStroke((currentEnergy / capacity* 3) + 1));
  399. break;
  400. }
  401. g.drawLine(start.x, start.y, end.x, end.y);
  402. Position middle = new Position((start.x + end.x) / 2, (start.y + end.y) / 2);
  403. g.drawString(currentEnergy + "/" + capacity , middle.x, middle.y);
  404. }
  405. private void paintSwitch(Graphics2D g, DecoratedSwitch dSwitch)
  406. {
  407. drawCanvasObject(g, dSwitch.getState() == SwitchState.Open ? HolonSwitch.getSwitchOpenImage(): HolonSwitch.getSwitchClosedImage() , dSwitch.getModel().getPosition());
  408. }
  409. private void paintSupplyBar(Graphics2D g, float percentage, Color color, Position pos) {
  410. // +1, -2, -1 little Ajustment for pixelperfect allignment
  411. int barWidth = (int) (controller.getScale());
  412. int barHeight = (int) (controller.getScale() / 5);
  413. g.setColor(Color.WHITE);
  414. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) barWidth, barHeight);
  415. g.setColor(color);
  416. g.fillRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, (int) (barWidth * (percentage < 1 ? percentage : 1.0f) - 1), barHeight);
  417. g.setColor(Color.BLACK);
  418. g.setStroke(new BasicStroke(1));
  419. g.drawRect(pos.x - controller.getScaleDiv2(), pos.y + controller.getScaleDiv2() - 1, barWidth - 1 , barHeight);
  420. g.setFont(new Font("TimesRoman", Font.PLAIN, (int) (barHeight * 1.5) - 2));
  421. String percentageString = (Math.round((percentage * 100))) + "%";
  422. int stringWidth = (int) g.getFontMetrics().getStringBounds(percentageString, g).getWidth();
  423. if(percentage > 1.0f) g.setColor(Color.WHITE); //Just to see better on purple
  424. g.drawString(percentageString, pos.x + 1 - stringWidth / 2, pos.y + controller.getScaleDiv2() - 1+ barHeight);
  425. }
  426. //old code
  427. void drawMarker(Graphics2D g) {
  428. if (sx > x && sy > y) {
  429. g.drawRect(x, y, sx - x, sy - y);
  430. } else if (sx < x && sy < y) {
  431. g.drawRect(sx, sy, x - sx, y - sy);
  432. } else if (sx >= x) {
  433. g.drawRect(x, sy, sx - x, y - sy);
  434. } else if (sy >= y) {
  435. g.drawRect(sx, y, x - sx, sy - y);
  436. }
  437. }
  438. public void paintComponent(Graphics g) {
  439. super.paintComponent(g);
  440. Graphics2D g2d = (Graphics2D) g;
  441. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  442. RenderingHints.VALUE_ANTIALIAS_ON));
  443. //-->Old code
  444. if (drawEdge) {
  445. g2d.setColor(Color.BLACK);
  446. g2d.setStroke(new BasicStroke(1));
  447. g2d.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  448. }
  449. //<--
  450. g2d.setColor(Color.BLACK);
  451. for(DecoratedCable cable : controller.getSimManager().getDecorState().getLeftOverEdges()) {
  452. paintCable(g2d, cable);
  453. }
  454. for(DecoratedNetwork network : controller.getSimManager().getDecorState().getNetworkList()) {
  455. for(DecoratedCable cable : network.getDecoratedCableList()) {
  456. paintCable(g2d, cable);
  457. }
  458. for(Consumer con: network.getConsumerList()) {
  459. paintConsumer(g2d, con);
  460. }
  461. for(Consumer con: network.getConsumerSelfSuppliedList()) {
  462. paintConsumer(g2d, con);
  463. }
  464. for(Supplier sup: network.getSupplierList()) {
  465. paintCanvasObject(g2d, sup);
  466. }
  467. for(Passiv pas: network.getPassivNoEnergyList()) {
  468. paintCanvasObject(g2d, pas);
  469. }
  470. }
  471. for(DecoratedSwitch dSwitch : controller.getSimManager().getDecorState().getDecoratedSwitches()) {
  472. paintSwitch(g2d, dSwitch);
  473. }
  474. for(CpsNode node : controller.getSimManager().getMinimumModel().getNodeList()) {
  475. drawCanvasObject(g2d, "/Images/node_selected.png" , node.getPosition());
  476. }
  477. //oldCode
  478. if (doMark) {
  479. g2d.setColor(Color.BLACK);
  480. g2d.setStroke(new BasicStroke(0));
  481. drawMarker(g2d);
  482. }
  483. /*
  484. *
  485. for(HolonSwitch switch2 : controller.getSimManager().getMinimumModel().getSwitchList()) {
  486. drawCanvasObject(g2d, switch2.getSwitchClosedImage() , switch2.getPosition());
  487. }
  488. */
  489. // String maxCap = null;
  490. // // Rendering
  491. // g2 = (Graphics2D) g;
  492. // RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  493. // RenderingHints.VALUE_ANTIALIAS_ON);
  494. // g2.setRenderingHints(rh);
  495. //
  496. // // Paint the Background
  497. // if (!model.getCanvasImagePath().isEmpty()) {
  498. // img = new ImageIcon(model.getCanvasImagePath()).getImage();
  499. // switch (model.getCanvasImageMode()) {
  500. // case BackgroundPopUp.IMAGE_PIXELS:
  501. // g2.drawImage(img, 0, 0, img.getWidth(null),
  502. // img.getHeight(null), null);
  503. // break;
  504. // case BackgroundPopUp.STRETCHED:
  505. // g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(),
  506. // null);
  507. // break;
  508. // case BackgroundPopUp.CUSTOM:
  509. // g2.drawImage(img, 0, 0, model.getCanvasImageWidth(),
  510. // model.getCanvasImageHeight(), null);
  511. // break;
  512. // default:
  513. // break;
  514. // }
  515. // }
  516. //
  517. // // SubNet Coloring
  518. // int i = 0;
  519. // for (SubNet s : controller.getSimManager().getSubNets()) {
  520. //
  521. // if (model.getSubNetColors().size() - 1 < i) {
  522. // controller.addSubNetColor(new Color(
  523. // (int) (Math.random() * 255),
  524. // (int) (Math.random() * 255),
  525. // (int) (Math.random() * 255)));
  526. // }
  527. // if (showedInformation[3]) {
  528. // for (HolonObject cps : s.getObjects()) {
  529. // cps.setBorderColor(model.getSubNetColors().get(i));
  530. // }
  531. // }
  532. // i++;
  533. // }
  534. //
  535. // // drawEdges that is being dragged
  536. // if (drawEdge) {
  537. // g2.setColor(Color.BLACK);
  538. // g2.setStroke(new BasicStroke(2));
  539. // g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  540. // }
  541. //
  542. // if(model.getEdgesOnCanvas().isEmpty() && !model.getObjectsOnCanvas().isEmpty()
  543. // && !model.getObjectsOnCanvas().get(0).getConnections().isEmpty()){
  544. // ArrayList<CpsEdge> edgesOnCanvas= model.getEdgesOnCanvas();
  545. // for(AbstractCpsObject cps :model.getObjectsOnCanvas())
  546. // for(CpsEdge e: cps.getConnections()){
  547. // if(!edgesOnCanvas.contains(e))
  548. // model.addEdgeOnCanvas(e);
  549. // }
  550. // }
  551. //
  552. // for (CpsEdge con : model.getEdgesOnCanvas()) {
  553. // maxCap = paintEdge(con, maxCap);
  554. // }
  555. //
  556. // // Highlighted Edge
  557. // if (!model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty() || model.getSelectedObjectID() > 0) {
  558. // g2.setColor(Color.BLUE);
  559. // for (CpsEdge con : model.getEdgesOnCanvas()) {
  560. // if (con.getFlow() <= con.getCapacity()) {
  561. // g2.setStroke(new BasicStroke(Math.min(
  562. // ((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  563. // } else {
  564. // g2.setStroke(new BasicStroke(2));
  565. // }
  566. //
  567. // maxCap = drawEdgeLine(con, maxCap);
  568. // }
  569. // } else if (edgeHighlight != null) {
  570. // g2.setColor(Color.BLUE);
  571. // if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  572. // g2.setStroke(new BasicStroke(Math.min(((edgeHighlight.getFlow()
  573. // / edgeHighlight.getCapacity() * 3) + 1), 4)));
  574. // } else {
  575. // g2.setStroke(new BasicStroke(2));
  576. // }
  577. // g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight
  578. // .getA().getPosition().y,
  579. // edgeHighlight.getB().getPosition().x, edgeHighlight.getB()
  580. // .getPosition().y);
  581. //
  582. // maxCap = setCapacityString(edgeHighlight, maxCap);
  583. //
  584. // if (showedInformation[0]) {
  585. // g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  586. // (edgeHighlight.getA().getPosition().x + edgeHighlight
  587. // .getB().getPosition().x) / 2, (edgeHighlight
  588. // .getA().getPosition().y + edgeHighlight.getB()
  589. // .getPosition().y) / 2);
  590. // }
  591. // }
  592. //
  593. //
  594. // /**
  595. // * highlight the Object that would be replaced
  596. // */
  597. // highlightMayBeReplaced(g2);
  598. //
  599. // // Objects
  600. // for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  601. // // Border Highlighting
  602. // if (showedInformation[3]) {
  603. // g2.setColor(cps.getBorderColor());
  604. // if (g2.getColor() != Color.WHITE && !(cps instanceof CpsNode)) {
  605. // g2.fillRect(
  606. // (int) (cps.getPosition().x
  607. // - controller.getScaleDiv2() - scalediv20 - 3),
  608. // (int) (cps.getPosition().y
  609. // - controller.getScaleDiv2() - scalediv20 - 3),
  610. // (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  611. // (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  612. // }
  613. // }
  614. //
  615. // setEdgePictureAndHighlighting(cps);
  616. //
  617. // g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
  618. // cps.getPosition().y - controller.getScaleDiv2(),
  619. // controller.getScale(), controller.getScale(), null);
  620. //
  621. // paintSupplyBar(g, cps);
  622. // }
  623. //
  624. // // Dragged marker Highlighting
  625. // if (doMark) {
  626. // g2.setColor(Color.BLACK);
  627. // g2.setStroke(new BasicStroke(0));
  628. // drawMarker();
  629. // }
  630. // // Tooltip
  631. // showTooltip(g);
  632. }
  633. @Override
  634. public void mouseClicked(MouseEvent e) {
  635. if (e.getButton() == MouseEvent.BUTTON1) {
  636. DefaulTable propertyTable = model.getPropertyTable();
  637. if (propertyTable.getRowCount() > 0) {
  638. for (int i = propertyTable.getRowCount() - 1; i > -1; i--) {
  639. propertyTable.removeRow(i);
  640. }
  641. }
  642. triggerUpdateController();
  643. }
  644. stopEditing();
  645. }
  646. @Override
  647. public void mouseEntered(MouseEvent e) {
  648. }
  649. @Override
  650. public void mouseExited(MouseEvent e) {
  651. }
  652. @Override
  653. public void mousePressed(MouseEvent e) {
  654. stopEditing();
  655. tempCps = null;
  656. edgeHighlight = null;
  657. controller.setSelecteEdge(null);
  658. // Object Selection
  659. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  660. cx = cps.getPosition().x - controller.getScaleDiv2();
  661. cy = cps.getPosition().y - controller.getScaleDiv2();
  662. if (x - controller.getScale() <= cx
  663. && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  664. tempCps = cps;
  665. setConsoleTextAfterSelect(cps);
  666. dragging = true;
  667. if (e.isControlDown() && tempCps != null) {
  668. if (model.getSelectedCpsObjects().contains(tempCps)) {
  669. controller.deleteSelectedObject(tempCps);
  670. //TODO: RemoveDepth
  671. } else {
  672. controller.addSelectedObject(tempCps);
  673. if(tempCps instanceof CpsUpperNode)
  674. controller.getObjectsInDepth();
  675. }
  676. }
  677. // If drawing an Edge (CTRL down)
  678. if (tempCps.getClass() == HolonObject.class) {
  679. HolonObject tempObj = ((HolonObject) tempCps);
  680. dataSelected = tempObj.getElements();
  681. }
  682. if (e.isShiftDown()) {
  683. drawEdge = true;
  684. dragging = false;
  685. }
  686. }
  687. }
  688. // Edge Selection
  689. if (tempCps == null) {
  690. edgeHighlight = mousePositionOnEdge(x, y);
  691. controller.setSelecteEdge(edgeHighlight);
  692. controller.setSelectedObjectID(0);
  693. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  694. model.getSelectedCpsObjects().clear();
  695. }
  696. }
  697. if (edgeHighlight == null && tempCps == null) {
  698. sx = e.getX();
  699. sy = e.getY();
  700. doMark = true;
  701. }
  702. repaint();
  703. }
  704. @Override
  705. public void mouseReleased(MouseEvent e) {
  706. x = e.getX();
  707. y = e.getY();
  708. dragging = false;
  709. if (drawEdge) {
  710. drawEdge = false;
  711. drawDeleteEdge();
  712. }
  713. if (dragged) {
  714. try {
  715. /**
  716. * Save before further Dragged interactions happen
  717. */
  718. controller.autoSave();
  719. } catch (IOException ex) {
  720. System.err.println("AutoSave error by dragging");
  721. ex.printStackTrace();
  722. }
  723. /**
  724. * check if a unique tempCps could replace an Object on the canvas
  725. */
  726. if(model.getSelectedCpsObjects().size()==1
  727. && checkForReplacement(model.getObjectsOnCanvas(), tempCps, tempCps.getPosition().x, tempCps.getPosition().y)){
  728. /**
  729. * if UpperNode would be replaced, close its tabs
  730. */
  731. if(mayBeReplaced instanceof CpsUpperNode)
  732. closeUpperNodeTab(mayBeReplaced.getId());
  733. /**
  734. * replace on canvas (will save)
  735. */
  736. controller.replaceCanvasObject(mayBeReplaced, tempCps);
  737. mayBeReplaced=null;
  738. }
  739. }
  740. if (!e.isControlDown() && !dragged && tempCps != null
  741. && MouseEvent.BUTTON3 != e.getButton()) {
  742. model.getSelectedCpsObjects().clear();
  743. controller.addSelectedObject(tempCps);
  744. model.setSelectedCpsObject(tempCps);
  745. if(tempCps instanceof CpsUpperNode)
  746. controller.getObjectsInDepth();
  747. }
  748. dragged = false;
  749. // Rightclick List
  750. setRightClickMenu(e);
  751. markObjects();
  752. if (doubleClick() && tempCps instanceof HolonSwitch
  753. && MouseEvent.BUTTON3 != e.getButton()&& tempCps != null) {
  754. ((HolonSwitch) tempCps).switchState();
  755. }
  756. controller.calculateStateForTimeStep(model.getCurIteration());
  757. triggerUpdateController();
  758. repaint();
  759. }
  760. @Override
  761. public void mouseDragged(MouseEvent e) {
  762. // If Edge is drawn
  763. x = e.getX();
  764. y = e.getY();
  765. if (!model.getSelectedCpsObjects().contains(tempCps) && !doMark) {
  766. model.getSelectedCpsObjects().clear();
  767. if (tempCps != null) {
  768. controller.addSelectedObject(tempCps);
  769. }
  770. }
  771. if (dragging) {
  772. try {
  773. dragged = true;
  774. float xDist, yDist; // Distance
  775. x = e.getX();
  776. y = e.getY();
  777. // Make sure its in bounds
  778. int scaleDiv2 = controller.getScaleDiv2();
  779. if (e.getX() < scaleDiv2)
  780. x = scaleDiv2;
  781. else if (e.getX() > this.getWidth() - scaleDiv2)
  782. x = this.getWidth() - scaleDiv2;
  783. if (e.getY() < scaleDiv2)
  784. y = scaleDiv2;
  785. else if (e.getY() > this.getHeight()
  786. - scaleDiv2)
  787. y = this.getHeight() - scaleDiv2;
  788. // Distance
  789. xDist = x - tempCps.getPosition().x;
  790. yDist = y - tempCps.getPosition().y;
  791. tempCps.setPosition(x, y); // Drag Position
  792. // ToolTipText Position and name
  793. toolTip = true;
  794. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  795. toolTipPos.x = tempCps.getPosition().x
  796. - scaleDiv2;
  797. toolTipPos.y = tempCps.getPosition().y
  798. + scaleDiv2;
  799. // All Selected Objects
  800. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  801. if (cps != tempCps) {
  802. x = (int) (cps.getPosition().x + xDist);
  803. y = (int) (cps.getPosition().y + yDist);
  804. // Make sure its in bounds
  805. if (x <= scaleDiv2)
  806. x = scaleDiv2;
  807. else if (x > this.getWidth()
  808. - scaleDiv2)
  809. x = this.getWidth() - scaleDiv2;
  810. if (y <= scaleDiv2)
  811. y = scaleDiv2;
  812. else if (y > this.getHeight()
  813. - scaleDiv2)
  814. y = this.getHeight() - scaleDiv2;
  815. cps.setPosition(x, y);
  816. }
  817. }
  818. /**
  819. * check if something might be replaced
  820. */
  821. if(model.getSelectedCpsObjects().size()==1)
  822. checkForReplacement(model.getObjectsOnCanvas(), tempCps, x, y);
  823. repaint();
  824. } catch (Exception eex) {
  825. }
  826. }
  827. // Mark Objects
  828. if (doMark) {
  829. tempSelected.clear();
  830. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  831. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  832. if (sx >= x) {
  833. x1 = x;
  834. x2 = sx;
  835. }
  836. if (sy >= y) {
  837. y1 = y;
  838. y2 = sy;
  839. }
  840. if (x1 <= cps.getPosition().x + model.getScaleDiv2()
  841. && y1 <= cps.getPosition().y + model.getScaleDiv2()
  842. && x2 >= cps.getPosition().x
  843. && y2 >= cps.getPosition().y) {
  844. tempSelected.add(cps);
  845. }
  846. }
  847. }
  848. repaint();
  849. }
  850. @Override
  851. public void mouseMoved(MouseEvent e) {
  852. x = e.getX();
  853. y = e.getY();
  854. // Everything for the tooltip :)
  855. boolean on = false;
  856. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  857. cx = cps.getPosition().x - controller.getScaleDiv2();
  858. cy = cps.getPosition().y - controller.getScaleDiv2();
  859. on = setToolTipInfoAndPosition(on, cps);
  860. }
  861. if(on||(!on && toolTip))
  862. repaint();
  863. toolTip = on;
  864. }
  865. /**
  866. * Draws or Deletes an Edge.
  867. */
  868. void drawDeleteEdge() {
  869. if (getMousePosition() != null) {
  870. boolean node = true;
  871. boolean newEdge = true;
  872. boolean onEdge = true;
  873. boolean deleteNode = false;
  874. CpsEdge e = null;
  875. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  876. cx = cps.getPosition().x - controller.getScaleDiv2();
  877. cy = cps.getPosition().y - controller.getScaleDiv2();
  878. if (x - controller.getScale() <= cx
  879. && y - controller.getScale() <= cy && x >= cx
  880. && y >= cy && cps != tempCps) {
  881. node = false;
  882. onEdge = false;
  883. for (CpsEdge p : tempCps.getConnections()) {
  884. if ((p.getA() == tempCps && p.getB() == cps)
  885. || (p.getB() == tempCps && p.getA() == cps)) {
  886. newEdge = false;
  887. e = p;
  888. }
  889. }
  890. if (!newEdge) {
  891. controller.removeEdgesOnCanvas(e);
  892. // Node ohne Edge?
  893. if (e.getA().getClass() == CpsNode.class
  894. && e.getA().getConnections().isEmpty()) {
  895. tempCps = e.getA();
  896. deleteNode = true;
  897. }
  898. if (e.getB().getClass() == CpsNode.class
  899. && e.getB().getConnections().isEmpty()) {
  900. deleteNode = true;
  901. }
  902. } else {
  903. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  904. controller.addEdgeOnCanvas(e);
  905. }
  906. }
  907. }
  908. // Edge auf eine Edge gezogen?
  909. if (onEdge && !checkForReplacement(x, y)) {
  910. CpsEdge p = mousePositionOnEdge(x, y);
  911. if (p != null) {
  912. CpsEdge e1;
  913. CpsEdge e2;
  914. node = false;
  915. CpsNode n = new CpsNode("Node");
  916. n.setPosition(x, y);
  917. controller.addObjectCanvas(n);
  918. AbstractCpsObject r, k;
  919. r = p.getA();
  920. k = p.getB();
  921. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  922. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  923. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  924. controller.removeEdgesOnCanvas(p);
  925. controller.addEdgeOnCanvas(e);
  926. controller.addEdgeOnCanvas(e1);
  927. controller.addEdgeOnCanvas(e2);
  928. }
  929. }else{
  930. mayBeReplaced = null;
  931. }
  932. // ins leere Gedragged
  933. if (node && !checkForReplacement(x, y)) {
  934. CpsNode n = new CpsNode("Node");
  935. n.setPosition(x, y);
  936. controller.addObjectCanvas(n);
  937. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  938. controller.addEdgeOnCanvas(e);
  939. }else{
  940. mayBeReplaced = null;
  941. }
  942. // Wenn ein Node ohne Connections da ist
  943. if (deleteNode) {
  944. controller.delCanvasObject(tempCps, true);
  945. tempCps = null;
  946. }
  947. }
  948. }
  949. /**
  950. * Checks if the mouse is on an Edge.
  951. *
  952. * @param x
  953. * Position of the Mouse
  954. * @param y
  955. * Position of the Mouse
  956. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  957. */
  958. private CpsEdge mousePositionOnEdge(int x, int y) {
  959. x += controller.getScaleDiv2();
  960. y += controller.getScaleDiv2();
  961. for (CpsEdge p : model.getEdgesOnCanvas()) {
  962. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA()
  963. .getPosition().y, p.getB().getPosition().x, p.getB()
  964. .getPosition().y);
  965. int[] positions = determineMousePositionOnEdge(p);
  966. int lx = positions[0];
  967. int ly = positions[1];
  968. int hx = positions[2];
  969. int hy = positions[3];
  970. // distance from a point to a line and between both Objects
  971. if (l.ptLineDistSq(x - model.getScaleDiv2(),
  972. y - model.getScaleDiv2()) < 20
  973. && x > lx && x < hx && y > ly && y < hy) {
  974. return p;
  975. }
  976. }
  977. return null;
  978. }
  979. void updateLanguages() {
  980. itemCut.setText(Languages.getLanguage()[95]);
  981. itemCopy.setText(Languages.getLanguage()[96]);
  982. itemPaste.setText(Languages.getLanguage()[97]);
  983. itemDelete.setText(Languages.getLanguage()[98]);
  984. itemGroup.setText(Languages.getLanguage()[99]);
  985. itemUngroup.setText(Languages.getLanguage()[100]);
  986. itemTrack.setText(Languages.getLanguage()[101]);
  987. itemUntrack.setText(Languages.getLanguage()[102]);
  988. }
  989. /**
  990. * Set if Information should be shown.
  991. *
  992. * @param connection
  993. * boolean for conecction
  994. * @param object
  995. * boolean for objects
  996. * @param nodeOfnode
  997. */
  998. void setShowedInformation(boolean connection, boolean object,
  999. boolean border, boolean nodeOfnode) {
  1000. showedInformation[0] = connection;
  1001. showedInformation[1] = object;
  1002. showedInformation[3] = border;
  1003. showedInformation[4] = nodeOfnode;
  1004. }
  1005. /**
  1006. * Returns if Information should be shown.
  1007. *
  1008. * @return Array of boolean [0] = connection, [1] = objects
  1009. */
  1010. boolean[] getShowedInformation() {
  1011. return showedInformation;
  1012. }
  1013. /**
  1014. * set toolTip
  1015. *
  1016. * @param bool
  1017. */
  1018. void setToolTip(boolean bool) {
  1019. this.toolTip = bool;
  1020. }
  1021. /**
  1022. * Set the Mouse
  1023. *
  1024. * @param x
  1025. * @param y
  1026. */
  1027. void setXY(int x, int y) {
  1028. this.x = x;
  1029. this.y = y;
  1030. }
  1031. @Override
  1032. public boolean checkForReplacement(int x, int y) {
  1033. return checkForReplacement(model.getObjectsOnCanvas(), null, x, y);
  1034. }
  1035. @Override
  1036. public void tryToAlignObjects(){
  1037. /**
  1038. * Align all Objects
  1039. */
  1040. for(AbstractCpsObject cps: model.getObjectsOnCanvas())
  1041. align(cps,3*model.getScaleDiv2());
  1042. /**
  1043. * AutoSave new Positons
  1044. */
  1045. try{
  1046. controller.autoSave();
  1047. } catch (IOException ex) {
  1048. System.err.println("AutoSave error by aligning");
  1049. ex.printStackTrace();
  1050. }
  1051. }
  1052. @Override
  1053. public void closeUpperNodeTab(int upperNodeId) {
  1054. JTabbedPane tabbedPaneInner = (JTabbedPane) getParent()
  1055. .getParent().getParent().getParent();
  1056. for (int i = 1; i < tabbedPaneInner.getTabCount(); i++) {
  1057. if (((UpperNodeCanvas) ((JScrollPane) tabbedPaneInner
  1058. .getComponentAt(i)).getViewport().getComponent(
  1059. 0)).upperNode.getId() == upperNodeId) {
  1060. tabbedPaneInner.remove(i);
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. }