MyCanvas.java 34 KB

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