MyCanvas.java 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. package ui.view;
  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Image;
  8. import java.awt.Point;
  9. import java.awt.RenderingHints;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.MouseEvent;
  13. import java.awt.event.MouseListener;
  14. import java.awt.event.MouseMotionListener;
  15. import java.awt.geom.Line2D;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.Timer;
  20. import java.util.TimerTask;
  21. import javax.swing.ImageIcon;
  22. import javax.swing.JMenuItem;
  23. import javax.swing.JPanel;
  24. import javax.swing.JPopupMenu;
  25. import javax.swing.JScrollPane;
  26. import javax.swing.JTabbedPane;
  27. import javax.swing.JToolTip;
  28. import classes.CpsEdge;
  29. import classes.CpsNode;
  30. import classes.CpsUpperNode;
  31. import classes.AbstractCpsObject;
  32. import classes.HolonElement;
  33. import classes.HolonObject;
  34. import classes.HolonSwitch;
  35. import classes.Position;
  36. import classes.SubNet;
  37. import ui.controller.Control;
  38. import ui.model.Model;
  39. /**
  40. * This Class is the Canvas. All Objects will be visualized here
  41. *
  42. * @author Gruppe14
  43. */
  44. public class MyCanvas extends JPanel implements MouseListener, MouseMotionListener {
  45. private static final long serialVersionUID = 1L;
  46. private Image img = null; // Contains the image to draw on MyCanvas
  47. private int x = 0;
  48. private int y = 0;
  49. // edge Object Start Point
  50. private Model model;
  51. private final Control controller;
  52. Graphics2D g2; // For Painting
  53. private int cx, cy;
  54. private int sx, sy; // Mark Coords
  55. private float scalediv20;
  56. private Position unPos;
  57. private ArrayList<Position> savePos;
  58. ArrayList<HolonElement> dataSelected = new ArrayList<HolonElement>();
  59. ArrayList<AbstractCpsObject> tempSelected = new ArrayList<AbstractCpsObject>();
  60. private boolean[] showedInformation = new boolean[4];
  61. private boolean dragging = false; // for dragging
  62. private boolean dragged = false; // if an object/objects was/were dragged
  63. private boolean drawEdge = false; // for drawing edges
  64. public boolean click = false; // for double click
  65. private boolean doMark = false; // for double click
  66. public AbstractCpsObject tempCps = null;
  67. private CpsEdge edgeHighlight = null;
  68. // PopUpMenu
  69. private JPopupMenu popmenu = new JPopupMenu();
  70. private JMenuItem itemDelete = new JMenuItem("Delete");
  71. private JMenuItem itemCut = new JMenuItem("Cut");
  72. private JMenuItem itemCopy = new JMenuItem("Copy");
  73. public JMenuItem itemPaste = new JMenuItem("Paste");
  74. public JMenuItem itemGroup = new JMenuItem("Group");
  75. public JMenuItem itemUngroup = new JMenuItem("Ungroup");
  76. public JMenuItem itemTrack = new JMenuItem("Track");
  77. public JMenuItem itemUntrack = new JMenuItem("Untrack");
  78. private JToolTip objectTT = new JToolTip();
  79. private Point mousePosition = new Point(); // Mouse Position when
  80. // rightclicked
  81. javax.swing.Timer animT; // animation Timer
  82. private final int ANIMTIME = 500; // animation Time
  83. private ArrayList<AbstractCpsObject> animCps = null;
  84. private int animFPS = 60;
  85. private int animDuration = ANIMTIME; // animation Duration
  86. private int animDelay = 1000 / animFPS; // animation Delay
  87. private int animSteps = animDuration / animDelay; // animation Steps;
  88. private long start = 0; // for time
  89. private long elapsedTime = 0; // outprint
  90. // contains the value of the Capacity for new created Edges
  91. /**
  92. * Constructor.
  93. *
  94. * @param mod
  95. * the Model
  96. * @param control
  97. * the Controller
  98. */
  99. public MyCanvas(Model mod, Control control) {
  100. this.add(objectTT);
  101. this.controller = control;
  102. this.model = mod;
  103. scalediv20 = model.getScale() / 20;
  104. showedInformation[0] = true;
  105. showedInformation[1] = true;
  106. showedInformation[2] = false;
  107. control.setMaxCapacity(10000);
  108. popmenu.add(itemCut);
  109. popmenu.add(itemCopy);
  110. popmenu.add(itemPaste);
  111. popmenu.add(itemDelete);
  112. popmenu.addSeparator();
  113. popmenu.add(itemGroup);
  114. popmenu.add(itemUngroup);
  115. popmenu.add(itemTrack);
  116. popmenu.add(itemUntrack);
  117. itemDelete.setEnabled(false);
  118. itemCut.setEnabled(false);
  119. itemCopy.setEnabled(false);
  120. itemPaste.setEnabled(false);
  121. itemGroup.setEnabled(false);
  122. itemUngroup.setEnabled(false);
  123. itemTrack.setEnabled(false);
  124. itemUntrack.setEnabled(false);
  125. itemGroup.addActionListener(new ActionListener() {
  126. @Override
  127. public void actionPerformed(ActionEvent e) {
  128. // calculate uppernode pos (taken from the controller)
  129. unPos = new Position(0, 0);
  130. animCps = new ArrayList<>();
  131. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  132. animCps.add(cps); // add to animation Cps ArrayList
  133. unPos.x += cps.getPosition().x;
  134. unPos.y += cps.getPosition().y;
  135. }
  136. unPos.x /= animCps.size();
  137. unPos.y /= animCps.size();
  138. // save old Position
  139. savePos = new ArrayList<>();
  140. for (int i = 0; i < animCps.size(); i++) {
  141. savePos.add(new Position(0, 0));
  142. savePos.get(i).x = animCps.get(i).getPosition().x;
  143. savePos.get(i).y = animCps.get(i).getPosition().y;
  144. }
  145. animT = new javax.swing.Timer(animDelay, new ActionListener() {
  146. @Override
  147. public void actionPerformed(ActionEvent e) {
  148. if (animDuration - animDelay > 0 && animCps.size() > 1) {
  149. for (int i = 0; i < animCps.size(); i++) {
  150. double x1 = animCps.get(i).getPosition().x - unPos.x;
  151. double y1 = animCps.get(i).getPosition().y - unPos.y;
  152. animCps.get(i).getPosition().x -= x1 / animSteps;
  153. animCps.get(i).getPosition().y -= y1 / animSteps;
  154. }
  155. repaint();
  156. animDuration -= animDelay;
  157. animSteps--;
  158. } else {
  159. animDuration = ANIMTIME;
  160. animSteps = animDuration / animDelay;
  161. animT.stop();
  162. for (int i = 0; i < animCps.size(); i++) {
  163. animCps.get(i).getPosition().x = savePos.get(i).x;
  164. animCps.get(i).getPosition().y = savePos.get(i).y;
  165. }
  166. controller.addUpperNode("NodeOfNode", null, animCps);
  167. controller.calculateStateForCurrentTimeStep();
  168. repaint();
  169. elapsedTime = System.currentTimeMillis() - start;
  170. controller.addTextToConsole(elapsedTime + "");
  171. }
  172. }
  173. });
  174. start = System.currentTimeMillis();
  175. animT.start();
  176. }
  177. });
  178. itemUngroup.addActionListener(new ActionListener() {
  179. @Override
  180. public void actionPerformed(ActionEvent e) {
  181. // save old Position
  182. JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
  183. for (int i = 3; i < tabbedPane.getTabCount(); i++) {
  184. if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  185. .getComponent(0)).upperNode.getID() == ((CpsUpperNode) tempCps).getID()) {
  186. tabbedPane.remove(i);
  187. break;
  188. }
  189. }
  190. savePos = new ArrayList<>();
  191. animCps = ((CpsUpperNode) tempCps).getNodes();
  192. controller.delUpperNode((CpsUpperNode) tempCps, null);
  193. for (int i = 0; i < animCps.size(); i++) {
  194. savePos.add(new Position(0, 0));
  195. savePos.get(i).x = animCps.get(i).getPosition().x;
  196. savePos.get(i).y = animCps.get(i).getPosition().y;
  197. }
  198. for (AbstractCpsObject cps : animCps) {
  199. int x = ((CpsUpperNode) tempCps).getPosition().x;
  200. int y = ((CpsUpperNode) tempCps).getPosition().y;
  201. cps.setPosition(new Position(x, y));
  202. }
  203. animT = new javax.swing.Timer(animDelay, new ActionListener() {
  204. @Override
  205. public void actionPerformed(ActionEvent e) {
  206. if (animDuration - animDelay >= 0) {
  207. for (int i = 0; i < animCps.size(); i++) {
  208. double x1 = animCps.get(i).getPosition().x - savePos.get(i).x;
  209. double y1 = animCps.get(i).getPosition().y - savePos.get(i).y;
  210. animCps.get(i).getPosition().x -= x1 / animSteps;
  211. animCps.get(i).getPosition().y -= y1 / animSteps;
  212. }
  213. repaint();
  214. animDuration -= animDelay;
  215. animSteps--;
  216. } else {
  217. animDuration = ANIMTIME;
  218. animSteps = animDuration / animDelay;
  219. animT.stop();
  220. for (int i = 0; i < animCps.size(); i++) {
  221. animCps.get(i).getPosition().x = savePos.get(i).x;
  222. animCps.get(i).getPosition().y = savePos.get(i).y;
  223. }
  224. controller.calculateStateForCurrentTimeStep();
  225. repaint();
  226. elapsedTime = System.currentTimeMillis() - start;
  227. controller.addTextToConsole(elapsedTime + "");
  228. }
  229. }
  230. });
  231. start = System.currentTimeMillis();
  232. animT.start();
  233. }
  234. });
  235. //adds the selected object(s) to the statistic panel
  236. itemTrack.addActionListener(new ActionListener() {
  237. @Override
  238. public void actionPerformed(ActionEvent e) {
  239. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  240. boolean found = false;
  241. if (controller.getTrackingObj() != null) {
  242. if(controller.getTrackingObj().contains(o)){
  243. found = true;
  244. }
  245. }
  246. if (!found) {
  247. controller.addTrackingObj(o);
  248. if(o instanceof HolonObject){
  249. ((HolonObject) o).updateTrackingInfo();
  250. }
  251. }
  252. controller.addTextToConsole("Tracking: ", Color.BLACK, 12, false, false, false);
  253. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  254. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  255. controller.addTextToConsole("" + o.getID(), Color.RED, 12, true, false, true);
  256. }
  257. }
  258. });
  259. itemUntrack.addActionListener(new ActionListener() {
  260. @Override
  261. public void actionPerformed(ActionEvent e) {
  262. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  263. if (o instanceof HolonObject) {
  264. boolean found = false;
  265. if (controller.getTrackingObj() != null) {
  266. for (AbstractCpsObject obj : controller.getTrackingObj()) {
  267. if(obj instanceof HolonObject){
  268. if (obj.getID() == o.getID()) {
  269. found = true;
  270. }
  271. }
  272. }
  273. }
  274. if (found) {
  275. // Removed from tracking array and tracking
  276. // information reseted
  277. controller.removeTrackingObj((HolonObject) o);
  278. ((HolonObject) o).setTrackingProd(new float[100]);
  279. ((HolonObject) o).setTrackingCons(new float[100]);
  280. }
  281. controller.addTextToConsole("Untracking: ", Color.BLACK, 12, false, false, false);
  282. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  283. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  284. controller.addTextToConsole("" + o.getID(), Color.RED, 12, true, false, true);
  285. }
  286. }
  287. }
  288. });
  289. itemDelete.addActionListener(new ActionListener() {
  290. @Override
  291. public void actionPerformed(ActionEvent e) {
  292. // Remove the selected Object objects
  293. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  294. controller.delCanvasObject(cps);
  295. controller.removeTrackingObj(cps);
  296. // Remove UpperNodeTab if UpperNode deleted
  297. if (cps instanceof CpsUpperNode) {
  298. JTabbedPane tabbedPane = (JTabbedPane) getParent().getParent().getParent();
  299. for (int i = 3; i < tabbedPane.getTabCount(); i++) {
  300. if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  301. .getComponent(0)).upperNode.getID() == cps.getID()) {
  302. tabbedPane.remove(i);
  303. i = tabbedPane.getTabCount();
  304. }
  305. }
  306. }
  307. }
  308. model.getSelectedCpsObjects().clear();
  309. tempCps = null;
  310. repaint();
  311. }
  312. });
  313. itemCut.addActionListener(new ActionListener() {
  314. @Override
  315. public void actionPerformed(ActionEvent e) {
  316. controller.cutObjects();
  317. itemPaste.setEnabled(true);
  318. repaint();
  319. }
  320. });
  321. itemCopy.addActionListener(new ActionListener() {
  322. @Override
  323. public void actionPerformed(ActionEvent e) {
  324. controller.copyObjects();
  325. itemPaste.setEnabled(true);
  326. repaint();
  327. }
  328. });
  329. itemPaste.addActionListener(new ActionListener() {
  330. @Override
  331. public void actionPerformed(ActionEvent e) {
  332. controller.pasteObjects(mousePosition);
  333. repaint();
  334. }
  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. public void paintComponent(Graphics g) {
  346. String maxCap;
  347. super.paintComponent(g);
  348. // Rendering
  349. g2 = (Graphics2D) g;
  350. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  351. g2.setRenderingHints(rh);
  352. // Test SubNet Coloring
  353. int i = 0;
  354. for (SubNet s : controller.getSimManager().getSubNets()) {
  355. if (model.getSubNetColors().size() - 1 < i) {
  356. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  357. (int) (Math.random() * 255)));
  358. }
  359. if(showedInformation[2]){
  360. for (HolonObject cps : s.getObjects()) {
  361. cps.setBorderColor(model.getSubNetColors().get(i));
  362. }
  363. }
  364. i++;
  365. }
  366. // drawEdges that is being dragged
  367. if (drawEdge) {
  368. g2.setColor(Color.BLACK);
  369. g2.setStroke(new BasicStroke(2));
  370. g2.drawLine(tempCps.getPosition().x + controller.getScaleDiv2(),
  371. tempCps.getPosition().y + controller.getScaleDiv2(), x, y);
  372. }
  373. for (CpsEdge con : model.getEdgesOnCanvas()) {
  374. if (con.getA().getID() != model.getSelectedObjectID() && con.getB().getID() != model.getSelectedObjectID()
  375. && con != edgeHighlight) {
  376. if (con.getState()) {
  377. g2.setColor(Color.GREEN);
  378. if (con.getCapacity() != -1) {
  379. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  380. }
  381. } else {
  382. g2.setColor(Color.RED);
  383. g2.setStroke(new BasicStroke(2));
  384. }
  385. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  386. con.getA().getPosition().y + controller.getScaleDiv2(),
  387. con.getB().getPosition().x + controller.getScaleDiv2(),
  388. con.getB().getPosition().y + controller.getScaleDiv2());
  389. if (con.getCapacity() == -1) {
  390. maxCap = Character.toString('\u221e');
  391. } else {
  392. maxCap = String.valueOf(con.getCapacity());
  393. }
  394. if (showedInformation[0]) {
  395. g2.drawString(con.getFlow() + "/" + maxCap,
  396. (con.getA().getPosition().x + con.getB().getPosition().x) / 2 + controller.getScaleDiv2(),
  397. (con.getA().getPosition().y + con.getB().getPosition().y) / 2 + controller.getScaleDiv2());
  398. }
  399. }
  400. }
  401. // Highlighted Edge
  402. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  403. g2.setColor(Color.BLUE);
  404. for (CpsEdge con : model.getEdgesOnCanvas()) {
  405. if (con.getFlow() <= con.getCapacity()) {
  406. g2.setStroke(new BasicStroke(Math.min((con.getFlow() / con.getCapacity() * 4), 4)));
  407. } else {
  408. g2.setStroke(new BasicStroke(2));
  409. }
  410. if (con.getA().getID() == model.getSelectedObjectID()
  411. || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
  412. || con.getB().getID() == model.getSelectedObjectID()
  413. || model.getSelectedCpsObjects().contains(con.getB())
  414. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  415. g2.drawLine(con.getA().getPosition().x + controller.getScaleDiv2(),
  416. con.getA().getPosition().y + controller.getScaleDiv2(),
  417. con.getB().getPosition().x + controller.getScaleDiv2(),
  418. con.getB().getPosition().y + controller.getScaleDiv2());
  419. if (con.getCapacity() == -1) {
  420. maxCap = Character.toString('\u221e');
  421. } else {
  422. maxCap = String.valueOf(con.getCapacity());
  423. }
  424. if (showedInformation[0]) {
  425. g2.drawString(con.getFlow() + "/" + maxCap,
  426. (con.getA().getPosition().x + con.getB().getPosition().x) / 2
  427. + controller.getScaleDiv2(),
  428. (con.getA().getPosition().y + con.getB().getPosition().y) / 2
  429. + controller.getScaleDiv2());
  430. }
  431. }
  432. }
  433. } else if (edgeHighlight != null) {
  434. g2.setColor(Color.BLUE);
  435. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  436. g2.setStroke(new BasicStroke(Math.min((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 4), 4)));
  437. } else {
  438. g2.setStroke(new BasicStroke(2));
  439. }
  440. g2.drawLine(edgeHighlight.getA().getPosition().x + controller.getScaleDiv2(),
  441. edgeHighlight.getA().getPosition().y + controller.getScaleDiv2(),
  442. edgeHighlight.getB().getPosition().x + controller.getScaleDiv2(),
  443. edgeHighlight.getB().getPosition().y + controller.getScaleDiv2());
  444. if (edgeHighlight.getCapacity() == -1) {
  445. maxCap = Character.toString('\u221e');
  446. } else {
  447. maxCap = String.valueOf(edgeHighlight.getCapacity());
  448. }
  449. if (showedInformation[0]) {
  450. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  451. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2
  452. + controller.getScaleDiv2(),
  453. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2
  454. + controller.getScaleDiv2());
  455. }
  456. }
  457. // Objects
  458. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  459. // Border Highlighting
  460. if(showedInformation[2]){
  461. g2.setColor(cps.getBorderColor());
  462. if (g2.getColor() != Color.WHITE) {
  463. g2.fillRect((int) (cps.getPosition().x - scalediv20 - 3), (int) (cps.getPosition().y - scalediv20 - 3),
  464. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  465. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  466. }
  467. }
  468. // node image
  469. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  470. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  471. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  472. } else {
  473. if (cps instanceof HolonSwitch) {
  474. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  475. ((HolonSwitch) cps).setAutoState(true);
  476. } else {
  477. ((HolonSwitch) cps).setAutoState(false);
  478. }
  479. }
  480. // Highlighting
  481. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  482. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  483. g2.setColor(Color.BLUE);
  484. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  485. (int) (controller.getScale() + (scalediv20 * 2)),
  486. (int) (controller.getScale() + (scalediv20 * 2)));
  487. if (showedInformation[1] && cps instanceof HolonObject) {
  488. g2.setColor(Color.BLACK);
  489. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  490. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  491. }
  492. } else if (cps instanceof HolonObject) {
  493. g2.setColor(((HolonObject) cps).getColor());
  494. g2.fillRect((int) (cps.getPosition().x - scalediv20), (int) (cps.getPosition().y - scalediv20),
  495. (int) (controller.getScale() + (scalediv20 * 2)),
  496. (int) (controller.getScale() + (scalediv20 * 2)));
  497. if (showedInformation[1]) {
  498. g2.setColor(Color.BLACK);
  499. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  500. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x, cps.getPosition().y - 10);
  501. }
  502. }
  503. // draw image
  504. File checkPath = new File(cps.getImage());
  505. if (checkPath.exists()) {
  506. img = new ImageIcon(cps.getImage()).getImage();
  507. } else {
  508. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  509. }
  510. }
  511. g2.drawImage(img, cps.getPosition().x, cps.getPosition().y, controller.getScale(), controller.getScale(),
  512. null);
  513. }
  514. // Dragg Highlighting
  515. if (doMark) {
  516. g2.setColor(Color.BLACK);
  517. g2.setStroke(new BasicStroke(1));
  518. if (sx > x && sy > y) {
  519. g2.drawRect(x, y, sx - x, sy - y);
  520. } else if (sx < x && sy < y) {
  521. g2.drawRect(sx, sy, x - sx, y - sy);
  522. } else if (sx >= x) {
  523. g2.drawRect(x, sy, sx - x, y - sy);
  524. } else if (sy >= y) {
  525. g2.drawRect(sx, y, x - sx, sy - y);
  526. }
  527. }
  528. }
  529. @Override
  530. public void mouseClicked(MouseEvent e) {
  531. }
  532. @Override
  533. public void mouseEntered(MouseEvent e) {
  534. }
  535. @Override
  536. public void mouseExited(MouseEvent e) {
  537. }
  538. @Override
  539. public void mousePressed(MouseEvent e) {
  540. tempCps = null;
  541. dataSelected = null;
  542. edgeHighlight = null;
  543. controller.setSelecteEdge(null);
  544. // Object Selection
  545. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  546. cx = cps.getPosition().x;
  547. cy = cps.getPosition().y;
  548. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  549. tempCps = cps;
  550. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  551. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  552. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  553. controller.addTextToConsole("" + cps.getID(), Color.RED, 12, true, false, true);
  554. dragging = true;
  555. controller.setSelectedObjectID(tempCps.getID());
  556. // If drawing an Edge (CTRL down)
  557. if (tempCps.getClass() == HolonObject.class) {
  558. HolonObject tempObj = ((HolonObject) tempCps);
  559. dataSelected = tempObj.getElements();
  560. }
  561. if (e.isShiftDown()) {
  562. drawEdge = true;
  563. dragging = false;
  564. }
  565. }
  566. }
  567. // Edge Selection
  568. if (tempCps == null) {
  569. edgeHighlight = mousePositionOnEdge(x, y);
  570. controller.setSelecteEdge(edgeHighlight);
  571. controller.setSelectedObjectID(0);
  572. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  573. model.getSelectedCpsObjects().clear();
  574. }
  575. }
  576. if (edgeHighlight == null && tempCps == null) {
  577. sx = e.getX();
  578. sy = e.getY();
  579. doMark = true;
  580. }
  581. repaint();
  582. }
  583. @Override
  584. public void mouseReleased(MouseEvent e) {
  585. dragging = false;
  586. if (drawEdge) {
  587. drawEdge = false;
  588. drawDeleteEdge();
  589. }
  590. if (dragged == true) {
  591. try {
  592. controller.autoSave();
  593. } catch (IOException ex) {
  594. // TODO Auto-generated catch block
  595. ex.printStackTrace();
  596. }
  597. }
  598. if (!e.isControlDown() && dragged == false && tempCps != null && e.BUTTON3 != e.getButton()) {
  599. model.getSelectedCpsObjects().clear();
  600. controller.addSelectedObject(tempCps);
  601. }
  602. dragged = false;
  603. // Rightclick List
  604. if (e.getButton() == MouseEvent.BUTTON3) {
  605. if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
  606. itemDelete.setEnabled(true);
  607. itemCut.setEnabled(true);
  608. itemCopy.setEnabled(true);
  609. if (tempCps != null)
  610. itemGroup.setEnabled(true);
  611. if (tempCps instanceof CpsUpperNode)
  612. itemUngroup.setEnabled(true);
  613. else
  614. itemUngroup.setEnabled(false);
  615. /*
  616. if (!(tempCps instanceof HolonSwitch)) {
  617. itemTrack.setEnabled(true);
  618. itemUntrack.setEnabled(true);
  619. } else {
  620. */
  621. itemTrack.setEnabled(true);
  622. itemUntrack.setEnabled(true);
  623. /*
  624. }
  625. */
  626. if (model.getSelectedCpsObjects().size() == 0) {
  627. controller.addSelectedObject(tempCps);
  628. }
  629. } else {
  630. itemCut.setEnabled(false);
  631. itemCopy.setEnabled(false);
  632. itemDelete.setEnabled(false);
  633. itemGroup.setEnabled(false);
  634. itemUngroup.setEnabled(false);
  635. itemTrack.setEnabled(false);
  636. itemUntrack.setEnabled(false);
  637. }
  638. mousePosition = this.getMousePosition();
  639. popmenu.show(e.getComponent(), e.getX(), e.getY());
  640. }
  641. if (doMark) {
  642. doMark = false;
  643. for (AbstractCpsObject cps : tempSelected) {
  644. if (!model.getSelectedCpsObjects().contains(cps)) {
  645. controller.addSelectedObject(cps);
  646. }
  647. }
  648. tempSelected.clear();
  649. }
  650. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch && MouseEvent.BUTTON3 != e.getButton()) {
  651. ((HolonSwitch) tempCps).switchState();
  652. }
  653. controller.calculateStateForTimeStep(model.getCurIteration());
  654. repaint();
  655. }
  656. @Override
  657. public void mouseDragged(MouseEvent e) {
  658. // If Edge is drawn
  659. x = e.getX();
  660. y = e.getY();
  661. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  662. model.getSelectedCpsObjects().clear();
  663. if (tempCps != null) {
  664. controller.addSelectedObject(tempCps);
  665. }
  666. }
  667. if (dragging) {
  668. try {
  669. dragged = true;
  670. float xDist, yDist; // Distance
  671. x = e.getX() - controller.getScaleDiv2();
  672. y = e.getY() - controller.getScaleDiv2();
  673. // Make sure its in bounds
  674. if (e.getX() < controller.getScaleDiv2())
  675. x = 0;
  676. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  677. x = this.getWidth() - controller.getScale();
  678. if (e.getY() < controller.getScaleDiv2())
  679. y = 0;
  680. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  681. y = this.getHeight() - controller.getScale();
  682. // Distance
  683. xDist = x - tempCps.getPosition().x;
  684. yDist = y - tempCps.getPosition().y;
  685. tempCps.setPosition(x, y); // Drag Position
  686. // TipText Position and name
  687. objectTT.setTipText(tempCps.getName() + ", " + tempCps.getID());
  688. objectTT.setLocation(x, y + controller.getScale());
  689. // All Selected Objects
  690. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  691. if (cps != tempCps) {
  692. x = (int) (cps.getPosition().x + xDist);
  693. y = (int) (cps.getPosition().y + yDist);
  694. // Make sure its in bounds
  695. if (x <= 0)
  696. x = 0;
  697. else if (x > this.getWidth() - controller.getScale())
  698. x = this.getWidth() - controller.getScale();
  699. if (y <= 0)
  700. y = 0;
  701. else if (y > this.getHeight() - controller.getScale())
  702. y = this.getHeight() - controller.getScale();
  703. cps.setPosition(x, y);
  704. }
  705. }
  706. repaint();
  707. } catch (Exception eex) {
  708. }
  709. }
  710. // Mark Objects
  711. if (doMark) {
  712. tempSelected.clear();
  713. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  714. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  715. if (sx >= x) {
  716. x1 = x;
  717. x2 = sx;
  718. }
  719. if (sy >= y) {
  720. y1 = y;
  721. y2 = sy;
  722. }
  723. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  724. && x2 >= cps.getPosition().x + model.getScaleDiv2()
  725. && y2 >= cps.getPosition().y + model.getScaleDiv2()) {
  726. tempSelected.add(cps);
  727. }
  728. }
  729. }
  730. repaint();
  731. }
  732. @Override
  733. public void mouseMoved(MouseEvent e) {
  734. x = e.getX();
  735. y = e.getY();
  736. // Everytghing for the tooltip :)
  737. boolean on = false;
  738. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  739. cx = cps.getPosition().x;
  740. cy = cps.getPosition().y;
  741. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  742. objectTT.setLocation(cx, cy + controller.getScale());
  743. objectTT.setTipText(cps.getName() + ", " + cps.getID());
  744. on = true;
  745. }
  746. }
  747. if (!on) {
  748. objectTT.setTipText("");
  749. objectTT.setLocation(-200, -200);
  750. }
  751. }
  752. /**
  753. * Draws or Deletes an Edge.
  754. */
  755. private void drawDeleteEdge() {
  756. boolean node = true;
  757. boolean newEdge = true;
  758. boolean onEdge = true;
  759. boolean deleteNode = false;
  760. CpsEdge e = null;
  761. AbstractCpsObject tempCPS = null;
  762. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  763. cx = cps.getPosition().x;
  764. cy = cps.getPosition().y;
  765. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  766. && cps != tempCps) {
  767. node = false;
  768. onEdge = false;
  769. for (CpsEdge p : tempCps.getConnections()) {
  770. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  771. newEdge = false;
  772. e = p;
  773. }
  774. }
  775. if (!newEdge) {
  776. controller.removeEdgesOnCanvas(e);
  777. // Node ohne Edge?
  778. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  779. tempCps = e.getA();
  780. deleteNode = true;
  781. }
  782. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  783. tempCPS = e.getB();
  784. deleteNode = true;
  785. }
  786. }
  787. if (newEdge) {
  788. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  789. controller.addEdgeOnCanvas(e);
  790. }
  791. }
  792. }
  793. // Edge auf eine Edge gezogen?
  794. if (onEdge) {
  795. CpsEdge p = mousePositionOnEdge(x, y);
  796. if (p != null) {
  797. CpsEdge e1 = null;
  798. CpsEdge e2 = null;
  799. node = false;
  800. CpsNode n = new CpsNode("Node");
  801. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  802. controller.addObjectCanvas(n);
  803. AbstractCpsObject r, k;
  804. r = p.getA();
  805. k = p.getB();
  806. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  807. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  808. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  809. controller.removeEdgesOnCanvas(p);
  810. controller.addEdgeOnCanvas(e);
  811. controller.addEdgeOnCanvas(e1);
  812. controller.addEdgeOnCanvas(e2);
  813. }
  814. }
  815. // ins leere Gedragged
  816. if (node) {
  817. CpsNode n = new CpsNode("Node");
  818. n.setPosition(x - model.getScaleDiv2(), y - model.getScaleDiv2());
  819. controller.addObjectCanvas(n);
  820. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  821. controller.addEdgeOnCanvas(e);
  822. }
  823. // Wenn ein Node ohne Connections da ist
  824. if (deleteNode) {
  825. controller.delCanvasObject(tempCps);
  826. tempCps = null;
  827. }
  828. }
  829. /**
  830. * Checks if the mouse is on an Edge.
  831. *
  832. * @param x
  833. * Position of the Mouse
  834. * @param y
  835. * Position of the Mouse
  836. *
  837. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  838. */
  839. public CpsEdge mousePositionOnEdge(int x, int y) {
  840. int lx, ly, hx, hy;
  841. for (CpsEdge p : model.getEdgesOnCanvas()) {
  842. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  843. p.getB().getPosition().y);
  844. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  845. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  846. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  847. } else {
  848. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  849. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  850. }
  851. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  852. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  853. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  854. } else {
  855. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  856. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  857. }
  858. // distance from a point to a line and between both Objects
  859. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  860. && y < hy) {
  861. return p;
  862. }
  863. }
  864. return null;
  865. }
  866. /**
  867. * Checks if a double click was made.
  868. *
  869. * @return
  870. *
  871. * @return true if doublecklick, false if not
  872. */
  873. private boolean doubleClick() {
  874. if (click) {
  875. click = false;
  876. return true;
  877. } else {
  878. click = true;
  879. Timer t = new Timer("doubleclickTimer", false);
  880. t.schedule(new TimerTask() {
  881. @Override
  882. public void run() {
  883. click = false;
  884. }
  885. }, 500);
  886. }
  887. return false;
  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. */
  897. public void setShowedInformation(boolean connection, boolean object, boolean border) {
  898. showedInformation[0] = connection;
  899. showedInformation[1] = object;
  900. showedInformation[2] = border;
  901. }
  902. /**
  903. * Returns if Information should be shown.
  904. *
  905. * @return Array of boolean [0] = connection, [1] = objects
  906. */
  907. public boolean[] getShowedInformation() {
  908. return showedInformation;
  909. }
  910. }