MyCanvas.java 37 KB

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