MyCanvas.java 36 KB

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