MyCanvas.java 36 KB

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