MyCanvas.java 33 KB

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