MyCanvas.java 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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. controller.addTextToConsole("Tracking: ", Color.BLACK, 12, false, false, false);
  260. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  261. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  262. controller.addTextToConsole("" + o.getId(), Color.RED, 12, true, false, true);
  263. }
  264. }
  265. });
  266. itemUntrack.addActionListener(new ActionListener() {
  267. @Override
  268. public void actionPerformed(ActionEvent e) {
  269. for (AbstractCpsObject o : model.getSelectedCpsObjects()) {
  270. if (o instanceof HolonObject) {
  271. boolean found = false;
  272. if (controller.getTrackingObj() != null) {
  273. for (AbstractCpsObject obj : controller.getTrackingObj()) {
  274. if (obj instanceof HolonObject) {
  275. if (obj.getId() == o.getId()) {
  276. found = true;
  277. }
  278. }
  279. }
  280. }
  281. if (found) {
  282. // Removed from tracking array and tracking
  283. // information reseted
  284. controller.removeTrackingObj((HolonObject) o);
  285. ((HolonObject) o).setTrackingProd(new float[100]);
  286. ((HolonObject) o).setTrackingCons(new float[100]);
  287. }
  288. controller.addTextToConsole("Untracking: ", Color.BLACK, 12, false, false, false);
  289. controller.addTextToConsole("" + o.getName(), Color.BLUE, 12, true, false, false);
  290. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  291. controller.addTextToConsole("" + o.getId(), Color.RED, 12, true, false, true);
  292. }
  293. }
  294. }
  295. });
  296. itemDelete.addActionListener(new ActionListener() {
  297. @Override
  298. public void actionPerformed(ActionEvent e) {
  299. // Remove the selected Object objects
  300. boolean save = false;
  301. for (int j = 0; j < model.getSelectedCpsObjects().size(); j++) {
  302. AbstractCpsObject cps = model.getSelectedCpsObjects().get(j);
  303. if (j == model.getSelectedCpsObjects().size() - 1)
  304. save = true;
  305. controller.delCanvasObject(cps, save);
  306. controller.removeTrackingObj(cps);
  307. // Remove UpperNodeTab if UpperNode deleted
  308. if (cps instanceof CpsUpperNode) {
  309. boolean splitView = false;
  310. JSplitPane tempSplit = (JSplitPane) getParent().getParent().getParent().getParent();
  311. JTabbedPane tabbedPane;
  312. JTabbedPane tabbedPane2;
  313. // if SplitView is activated
  314. if (tempSplit.getLeftComponent() instanceof JTabbedPane
  315. && tempSplit.getRightComponent() instanceof JTabbedPane) {
  316. splitView = true;
  317. tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
  318. tabbedPane2 = (JTabbedPane) tempSplit.getRightComponent();
  319. } else {
  320. tabbedPane = (JTabbedPane) tempSplit.getLeftComponent();
  321. tabbedPane2 = null;
  322. }
  323. //Look if the uppernode is open in a Tab
  324. for (int i = 4; i < tabbedPane.getTabCount(); i++) {
  325. if (tabbedPane.getComponentAt(i) == null) {
  326. } else if (((UpperNodeCanvas) ((JScrollPane) tabbedPane.getComponentAt(i)).getViewport()
  327. .getComponent(0)).upperNode.getId() == cps.getId()) {
  328. ((ButtonTabComponent) tabbedPane.getTabComponentAt(i)).removeTabs();
  329. break;
  330. }
  331. }
  332. // If SplitView is on and the view on
  333. // tabbedPane2 is the deleted upperNode
  334. try {
  335. if (tabbedPane2 != null
  336. && ((UpperNodeCanvas) ((JScrollPane) tabbedPane2.getSelectedComponent())
  337. .getViewport().getComponent(0)).upperNode.getId() == cps.getId()) {
  338. ((ButtonTabComponent) tabbedPane
  339. .getTabComponentAt(tabbedPane2.getSelectedIndex())).removeTabs();
  340. }
  341. } catch (Exception e2) {
  342. }
  343. }
  344. toolTip = false;
  345. }
  346. model.getSelectedCpsObjects().clear();
  347. tempCps = null;
  348. repaint();
  349. }
  350. });
  351. itemCut.addActionListener(new ActionListener() {
  352. @Override
  353. public void actionPerformed(ActionEvent e) {
  354. controller.cut(null);
  355. itemPaste.setEnabled(true);
  356. repaint();
  357. }
  358. });
  359. itemCopy.addActionListener(new ActionListener() {
  360. @Override
  361. public void actionPerformed(ActionEvent e) {
  362. controller.copy(null);
  363. itemPaste.setEnabled(true);
  364. repaint();
  365. }
  366. });
  367. itemPaste.addActionListener(new ActionListener() {
  368. @Override
  369. public void actionPerformed(ActionEvent e) {
  370. try {
  371. controller.paste(null, mousePosition);
  372. unitGraph.update(model.getSelectedCpsObjects());
  373. } catch (JsonParseException | UnsupportedFlavorException | IOException e1) {
  374. // TODO Auto-generated catch block
  375. JLabel message = new JLabel("The Clipboard information cannot be pastet into Application.");
  376. JOptionPane.showMessageDialog(null, message, "", JOptionPane.ERROR_MESSAGE);
  377. }
  378. repaint();
  379. }
  380. });
  381. this.addMouseListener(this);
  382. this.addMouseMotionListener(this);
  383. }
  384. /**
  385. * Paints all Components on the Canvas.
  386. *
  387. * @param g
  388. * Graphics
  389. */
  390. public void paintComponent(Graphics g) {
  391. String maxCap;
  392. super.paintComponent(g);
  393. // Rendering
  394. g2 = (Graphics2D) g;
  395. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  396. g2.setRenderingHints(rh);
  397. // Paint the Background
  398. if (!model.getCanvasImagePath().isEmpty()) {
  399. img = new ImageIcon(model.getCanvasImagePath()).getImage();
  400. switch (model.getCanvasImageMode()) {
  401. case BackgroundPopUp.IMAGE_PIXELS:
  402. g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
  403. break;
  404. case BackgroundPopUp.STRETCHED:
  405. g2.drawImage(img, 0, 0, model.getCanvasX(), model.getCanvasY(), null);
  406. break;
  407. case BackgroundPopUp.CUSTOM:
  408. g2.drawImage(img, 0, 0, model.getCanvasImageWidth(), model.getCanvasImageHeight(), null);
  409. break;
  410. default:
  411. break;
  412. }
  413. }
  414. // Test SubNet Coloring
  415. int i = 0;
  416. for (SubNet s : controller.getSimManager().getSubNets()) {
  417. if (model.getSubNetColors().size() - 1 < i) {
  418. controller.addSubNetColor(new Color((int) (Math.random() * 255), (int) (Math.random() * 255),
  419. (int) (Math.random() * 255)));
  420. }
  421. if (showedInformation[3]) {
  422. for (HolonObject cps : s.getObjects()) {
  423. cps.setBorderColor(model.getSubNetColors().get(i));
  424. }
  425. }
  426. i++;
  427. }
  428. // drawEdges that is being dragged
  429. if (drawEdge) {
  430. g2.setColor(Color.BLACK);
  431. g2.setStroke(new BasicStroke(2));
  432. g2.drawLine(tempCps.getPosition().x, tempCps.getPosition().y, x, y);
  433. }
  434. for (CpsEdge con : model.getEdgesOnCanvas()) {
  435. if (con.getA().getId() != model.getSelectedObjectID() && con.getB().getId() != model.getSelectedObjectID()
  436. && con != edgeHighlight) {
  437. if (con.getConnected() == 0) {
  438. if (con.getState()) {
  439. g2.setColor(Color.GREEN);
  440. if (con.getCapacity() != -1) {
  441. g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  442. }
  443. } else {
  444. g2.setColor(Color.RED);
  445. g2.setStroke(new BasicStroke(2));
  446. }
  447. } else {
  448. g2.setColor(Color.DARK_GRAY);
  449. g2.setStroke(new BasicStroke(2));
  450. }
  451. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  452. con.getB().getPosition().y);
  453. if (con.getCapacity() == -1) {
  454. maxCap = Character.toString('\u221e');
  455. } else if (con.getCapacity() == -2) {
  456. maxCap = "???";
  457. } else {
  458. maxCap = String.valueOf(con.getCapacity());
  459. }
  460. if (showedInformation[0]) {
  461. if (con.getConnected() == 0 || con.getConnected() == 1) {
  462. g2.drawString(con.getFlow() + "/" + maxCap,
  463. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  464. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  465. } else {
  466. g2.drawString("not connected", (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  467. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  468. }
  469. }
  470. }
  471. }
  472. // Highlighted Edge
  473. if (model.getSelectedObjectID() > 0 || !model.getSelectedCpsObjects().isEmpty() || !tempSelected.isEmpty()) {
  474. g2.setColor(Color.BLUE);
  475. for (CpsEdge con : model.getEdgesOnCanvas()) {
  476. if (con.getFlow() <= con.getCapacity()) {
  477. g2.setStroke(new BasicStroke(Math.min(((con.getFlow() / con.getCapacity() * 3) + 1), 4)));
  478. } else {
  479. g2.setStroke(new BasicStroke(2));
  480. }
  481. if (con.getA().getId() == model.getSelectedObjectID()
  482. || model.getSelectedCpsObjects().contains(con.getA()) || tempSelected.contains(con.getA())
  483. || con.getB().getId() == model.getSelectedObjectID()
  484. || model.getSelectedCpsObjects().contains(con.getB())
  485. || tempSelected.contains(con.getB()) && con != edgeHighlight) {
  486. g2.drawLine(con.getA().getPosition().x, con.getA().getPosition().y, con.getB().getPosition().x,
  487. con.getB().getPosition().y);
  488. if (con.getCapacity() == -1) {
  489. maxCap = Character.toString('\u221e');
  490. } else if (con.getCapacity() == -2) {
  491. maxCap = "???";
  492. } else {
  493. maxCap = String.valueOf(con.getCapacity());
  494. }
  495. if (showedInformation[0]) {
  496. if (con.getConnected() == 0 || con.getConnected() == 1) {
  497. g2.drawString(con.getFlow() + "/" + maxCap,
  498. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  499. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  500. } else {
  501. g2.drawString("not connected",
  502. (con.getA().getPosition().x + con.getB().getPosition().x) / 2,
  503. (con.getA().getPosition().y + con.getB().getPosition().y) / 2);
  504. }
  505. }
  506. }
  507. }
  508. } else if (edgeHighlight != null) {
  509. g2.setColor(Color.BLUE);
  510. if (edgeHighlight.getFlow() <= edgeHighlight.getCapacity()) {
  511. g2.setStroke(new BasicStroke(
  512. Math.min(((edgeHighlight.getFlow() / edgeHighlight.getCapacity() * 3) + 1), 4)));
  513. } else {
  514. g2.setStroke(new BasicStroke(2));
  515. }
  516. g2.drawLine(edgeHighlight.getA().getPosition().x, edgeHighlight.getA().getPosition().y,
  517. edgeHighlight.getB().getPosition().x, edgeHighlight.getB().getPosition().y);
  518. if (edgeHighlight.getCapacity() == -1) {
  519. maxCap = Character.toString('\u221e');
  520. } else if (edgeHighlight.getCapacity() == -2) {
  521. maxCap = "???";
  522. } else {
  523. maxCap = String.valueOf(edgeHighlight.getCapacity());
  524. }
  525. if (showedInformation[0]) {
  526. g2.drawString(edgeHighlight.getFlow() + "/" + maxCap,
  527. (edgeHighlight.getA().getPosition().x + edgeHighlight.getB().getPosition().x) / 2,
  528. (edgeHighlight.getA().getPosition().y + edgeHighlight.getB().getPosition().y) / 2);
  529. }
  530. }
  531. // Objects
  532. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  533. // Border Highlighting
  534. if (showedInformation[3]) {
  535. g2.setColor(cps.getBorderColor());
  536. if (g2.getColor() != Color.WHITE && !(cps instanceof CpsNode)) {
  537. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20 - 3),
  538. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20 - 3),
  539. (int) (controller.getScale() + ((scalediv20 + 3) * 2)),
  540. (int) (controller.getScale() + ((scalediv20 + 3) * 2)));
  541. }
  542. }
  543. // node image
  544. if (cps instanceof CpsNode && (cps == tempCps || model.getSelectedCpsObject() == cps
  545. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps))) {
  546. img = new ImageIcon(this.getClass().getResource("/Images/node_selected.png")).getImage();
  547. } else {
  548. if (cps instanceof HolonSwitch) {
  549. if (((HolonSwitch) cps).getActiveAt()[model.getCurIteration()]) {
  550. ((HolonSwitch) cps).setAutoState(true);
  551. } else {
  552. ((HolonSwitch) cps).setAutoState(false);
  553. }
  554. }
  555. // Highlighting
  556. if ((cps == tempCps && model.getSelectedCpsObjects().size() == 0 && tempSelected.size() == 0)
  557. || model.getSelectedCpsObjects().contains(cps) || tempSelected.contains(cps)) {
  558. g2.setColor(Color.BLUE);
  559. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
  560. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
  561. (int) (controller.getScale() + (scalediv20 * 2)),
  562. (int) (controller.getScale() + (scalediv20 * 2)));
  563. if (showedInformation[1] && cps instanceof HolonObject) {
  564. g2.setColor(Color.BLACK);
  565. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  566. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
  567. cps.getPosition().y - controller.getScaleDiv2() - 10);
  568. }
  569. } else if (cps instanceof HolonObject) {
  570. g2.setColor(((HolonObject) cps).getColor());
  571. g2.fillRect((int) (cps.getPosition().x - controller.getScaleDiv2() - scalediv20),
  572. (int) (cps.getPosition().y - controller.getScaleDiv2() - scalediv20),
  573. (int) (controller.getScale() + (scalediv20 * 2)),
  574. (int) (controller.getScale() + (scalediv20 * 2)));
  575. if (showedInformation[1]) {
  576. g2.setColor(Color.BLACK);
  577. float totalEnergy = ((HolonObject) cps).getCurrentEnergyAtTimeStep(model.getCurIteration());
  578. g2.drawString(Float.toString(totalEnergy), cps.getPosition().x - controller.getScaleDiv2(),
  579. cps.getPosition().y - controller.getScaleDiv2() - 10);
  580. }
  581. }
  582. // draw image
  583. File checkPath = new File(cps.getImage());
  584. if (checkPath.exists()) {
  585. img = new ImageIcon(cps.getImage()).getImage();
  586. } else {
  587. img = new ImageIcon(this.getClass().getResource(cps.getImage())).getImage();
  588. }
  589. }
  590. g2.drawImage(img, cps.getPosition().x - controller.getScaleDiv2(),
  591. cps.getPosition().y - controller.getScaleDiv2(), controller.getScale(), controller.getScale(),
  592. null);
  593. }
  594. // Dragged marker Highlighting
  595. if (doMark) {
  596. g2.setColor(Color.BLACK);
  597. g2.setStroke(new BasicStroke(1));
  598. if (sx > x && sy > y) {
  599. g2.drawRect(x, y, sx - x, sy - y);
  600. } else if (sx < x && sy < y) {
  601. g2.drawRect(sx, sy, x - sx, y - sy);
  602. } else if (sx >= x) {
  603. g2.drawRect(x, sy, sx - x, y - sy);
  604. } else if (sy >= y) {
  605. g2.drawRect(sx, y, x - sx, sy - y);
  606. }
  607. }
  608. // Tooltip
  609. if (toolTip) {
  610. g2.setColor(new Color(255, 225, 150));
  611. g2.setStroke(new BasicStroke(1));
  612. int textWidth = g.getFontMetrics().stringWidth(toolTipText) + 2; // Text
  613. // width
  614. // fixed x and y Position to the screen
  615. int fixXPos = toolTipPos.x - (textWidth >> 1) + model.getScaleDiv2();
  616. int fixYPos = toolTipPos.y;
  617. if (fixXPos < 0) {
  618. fixXPos = 0;
  619. } else if (fixXPos + textWidth + 1 > this.getWidth()) {
  620. fixXPos -= (fixXPos + textWidth + 1) - this.getWidth();
  621. }
  622. if (fixYPos + 16 > this.getHeight()) {
  623. fixYPos -= (fixYPos + 16) - this.getHeight();
  624. }
  625. g2.fillRect(fixXPos, fixYPos, textWidth, 15);
  626. g2.setColor(Color.BLACK);
  627. g2.drawRect(fixXPos, fixYPos, textWidth, 15);
  628. g2.drawString(toolTipText, fixXPos + 2, fixYPos + 12);
  629. }
  630. }
  631. @Override
  632. public void mouseClicked(MouseEvent e) {
  633. if (e.getButton() == e.BUTTON1) {
  634. if (model.getPropertyTable().getRowCount() > 0) {
  635. for (int i = model.getPropertyTable().getRowCount() - 1; i > -1; i--) {
  636. model.getPropertyTable().removeRow(i);
  637. }
  638. }
  639. updCon.paintProperties(tempCps);
  640. updCon.refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
  641. updCon.refreshTableProperties(model.getPropertyTable());
  642. }
  643. }
  644. @Override
  645. public void mouseEntered(MouseEvent e) {
  646. }
  647. @Override
  648. public void mouseExited(MouseEvent e) {
  649. }
  650. @Override
  651. public void mousePressed(MouseEvent e) {
  652. tempCps = null;
  653. dataSelected = null;
  654. edgeHighlight = null;
  655. controller.setSelecteEdge(null);
  656. // Object Selection
  657. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  658. cx = cps.getPosition().x - controller.getScaleDiv2();
  659. cy = cps.getPosition().y - controller.getScaleDiv2();
  660. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  661. tempCps = cps;
  662. controller.addTextToConsole("Selected: ", Color.BLACK, 12, false, false, false);
  663. controller.addTextToConsole("" + cps.getName(), Color.BLUE, 12, true, false, false);
  664. controller.addTextToConsole(", ID:", Color.BLACK, 12, false, false, false);
  665. controller.addTextToConsole("" + cps.getId(), Color.RED, 12, true, false, true);
  666. dragging = true;
  667. if (e.isControlDown() && tempCps != null) {
  668. if (model.getSelectedCpsObjects().contains(tempCps)) {
  669. controller.deleteSelectedObject(tempCps);
  670. } else {
  671. controller.addSelectedObject(tempCps);
  672. }
  673. }
  674. // If drawing an Edge (CTRL down)
  675. if (tempCps.getClass() == HolonObject.class) {
  676. HolonObject tempObj = ((HolonObject) tempCps);
  677. dataSelected = tempObj.getElements();
  678. }
  679. if (e.isShiftDown()) {
  680. drawEdge = true;
  681. dragging = false;
  682. }
  683. }
  684. }
  685. // Edge Selection
  686. if (tempCps == null) {
  687. edgeHighlight = mousePositionOnEdge(x, y);
  688. controller.setSelecteEdge(edgeHighlight);
  689. controller.setSelectedObjectID(0);
  690. if (!e.isControlDown() && e.getButton() != MouseEvent.BUTTON3) {
  691. model.getSelectedCpsObjects().clear();
  692. }
  693. }
  694. if (edgeHighlight == null && tempCps == null) {
  695. sx = e.getX();
  696. sy = e.getY();
  697. doMark = true;
  698. }
  699. repaint();
  700. }
  701. @Override
  702. public void mouseReleased(MouseEvent e) {
  703. x = e.getX();
  704. y = e.getY();
  705. dragging = false;
  706. if (drawEdge) {
  707. drawEdge = false;
  708. drawDeleteEdge();
  709. }
  710. if (dragged == true) {
  711. try {
  712. controller.autoSave();
  713. } catch (IOException ex) {
  714. // TODO Auto-generated catch block
  715. ex.printStackTrace();
  716. }
  717. }
  718. if (!e.isControlDown() && dragged == false && tempCps != null && e.BUTTON3 != e.getButton()) {
  719. model.getSelectedCpsObjects().clear();
  720. controller.addSelectedObject(tempCps);
  721. }
  722. dragged = false;
  723. // Rightclick List
  724. if (e.getButton() == MouseEvent.BUTTON3) {
  725. if (e.getButton() == MouseEvent.BUTTON3 && tempCps != null) {
  726. itemDelete.setEnabled(true);
  727. itemCut.setEnabled(true);
  728. itemCopy.setEnabled(true);
  729. if (tempCps != null) {
  730. itemGroup.setEnabled(true);
  731. itemTrack.setEnabled(true);
  732. itemUntrack.setEnabled(true);
  733. }
  734. if (tempCps instanceof CpsUpperNode)
  735. itemUngroup.setEnabled(true);
  736. else
  737. itemUngroup.setEnabled(false);
  738. if (model.getSelectedCpsObjects().size() == 0) {
  739. controller.addSelectedObject(tempCps);
  740. }
  741. } else {
  742. itemCut.setEnabled(false);
  743. itemCopy.setEnabled(false);
  744. itemDelete.setEnabled(false);
  745. itemGroup.setEnabled(false);
  746. itemUngroup.setEnabled(false);
  747. itemTrack.setEnabled(false);
  748. itemUntrack.setEnabled(false);
  749. }
  750. mousePosition = this.getMousePosition();
  751. popmenu.show(e.getComponent(), e.getX(), e.getY());
  752. }
  753. if (doMark) {
  754. doMark = false;
  755. for (AbstractCpsObject cps : tempSelected) {
  756. if (!model.getSelectedCpsObjects().contains(cps)) {
  757. controller.addSelectedObject(cps);
  758. }
  759. }
  760. controller.getObjectsInDepth();
  761. tempSelected.clear();
  762. }
  763. if (doubleClick() && tempCps != null && tempCps instanceof HolonSwitch && MouseEvent.BUTTON3 != e.getButton()) {
  764. ((HolonSwitch) tempCps).switchState();
  765. }
  766. controller.calculateStateForTimeStep(model.getCurIteration());
  767. repaint();
  768. }
  769. @Override
  770. public void mouseDragged(MouseEvent e) {
  771. // If Edge is drawn
  772. x = e.getX();
  773. y = e.getY();
  774. if (!model.getSelectedCpsObjects().contains(tempCps) && doMark == false) {
  775. model.getSelectedCpsObjects().clear();
  776. if (tempCps != null) {
  777. controller.addSelectedObject(tempCps);
  778. }
  779. }
  780. if (dragging) {
  781. try {
  782. dragged = true;
  783. float xDist, yDist; // Distance
  784. x = e.getX();
  785. y = e.getY();
  786. // Make sure its in bounds
  787. if (e.getX() < controller.getScaleDiv2())
  788. x = controller.getScaleDiv2();
  789. else if (e.getX() > this.getWidth() - controller.getScaleDiv2())
  790. x = this.getWidth() - controller.getScaleDiv2();
  791. if (e.getY() < controller.getScaleDiv2())
  792. y = controller.getScaleDiv2();
  793. else if (e.getY() > this.getHeight() - controller.getScaleDiv2())
  794. y = this.getHeight() - controller.getScaleDiv2();
  795. // Distance
  796. xDist = x - tempCps.getPosition().x;
  797. yDist = y - tempCps.getPosition().y;
  798. tempCps.setPosition(x, y); // Drag Position
  799. // ToolTipText Position and name
  800. toolTip = true;
  801. toolTipText = tempCps.getName() + ", " + tempCps.getId();
  802. toolTipPos.x = tempCps.getPosition().x - controller.getScaleDiv2();
  803. toolTipPos.y = tempCps.getPosition().y + controller.getScaleDiv2();
  804. // All Selected Objects
  805. for (AbstractCpsObject cps : model.getSelectedCpsObjects()) {
  806. if (cps != tempCps) {
  807. x = (int) (cps.getPosition().x + xDist);
  808. y = (int) (cps.getPosition().y + yDist);
  809. // Make sure its in bounds
  810. if (x <= controller.getScaleDiv2())
  811. x = controller.getScaleDiv2();
  812. else if (x > this.getWidth() - controller.getScaleDiv2())
  813. x = this.getWidth() - controller.getScaleDiv2();
  814. if (y <= controller.getScaleDiv2())
  815. y = controller.getScaleDiv2();
  816. else if (y > this.getHeight() - controller.getScaleDiv2())
  817. y = this.getHeight() - controller.getScaleDiv2();
  818. cps.setPosition(x, y);
  819. }
  820. }
  821. repaint();
  822. } catch (Exception eex) {
  823. }
  824. }
  825. // Mark Objects
  826. if (doMark) {
  827. tempSelected.clear();
  828. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  829. int x1 = sx, x2 = x, y1 = sy, y2 = y;
  830. if (sx >= x) {
  831. x1 = x;
  832. x2 = sx;
  833. }
  834. if (sy >= y) {
  835. y1 = y;
  836. y2 = sy;
  837. }
  838. if (x1 <= cps.getPosition().x + model.getScaleDiv2() && y1 <= cps.getPosition().y + model.getScaleDiv2()
  839. && x2 >= cps.getPosition().x && y2 >= cps.getPosition().y) {
  840. tempSelected.add(cps);
  841. }
  842. }
  843. }
  844. repaint();
  845. }
  846. @Override
  847. public void mouseMoved(MouseEvent e) {
  848. x = e.getX();
  849. y = e.getY();
  850. // Everytghing for the tooltip :)
  851. boolean on = false;
  852. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  853. cx = cps.getPosition().x - controller.getScaleDiv2();
  854. cy = cps.getPosition().y - controller.getScaleDiv2();
  855. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy) {
  856. on = true;
  857. toolTipPos.x = cps.getPosition().x - controller.getScaleDiv2();
  858. toolTipPos.y = cps.getPosition().y + controller.getScaleDiv2();
  859. toolTipText = cps.getName() + ", " + cps.getId();
  860. }
  861. }
  862. if (on) {
  863. toolTip = true;
  864. } else {
  865. toolTip = false;
  866. }
  867. repaint();
  868. }
  869. /**
  870. * Draws or Deletes an Edge.
  871. */
  872. private void drawDeleteEdge() {
  873. if (getMousePosition() != null) {
  874. boolean node = true;
  875. boolean newEdge = true;
  876. boolean onEdge = true;
  877. boolean deleteNode = false;
  878. CpsEdge e = null;
  879. for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
  880. cx = cps.getPosition().x - controller.getScaleDiv2();
  881. cy = cps.getPosition().y - controller.getScaleDiv2();
  882. if (x - controller.getScale() <= cx && y - controller.getScale() <= cy && x >= cx && y >= cy
  883. && cps != tempCps) {
  884. node = false;
  885. onEdge = false;
  886. for (CpsEdge p : tempCps.getConnections()) {
  887. if ((p.getA() == tempCps && p.getB() == cps) || (p.getB() == tempCps && p.getA() == cps)) {
  888. newEdge = false;
  889. e = p;
  890. }
  891. }
  892. if (!newEdge) {
  893. controller.removeEdgesOnCanvas(e);
  894. // Node ohne Edge?
  895. if (e.getA().getClass() == CpsNode.class && e.getA().getConnections().isEmpty()) {
  896. tempCps = e.getA();
  897. deleteNode = true;
  898. }
  899. if (e.getB().getClass() == CpsNode.class && e.getB().getConnections().isEmpty()) {
  900. deleteNode = true;
  901. }
  902. }
  903. if (newEdge) {
  904. e = new CpsEdge(cps, tempCps, model.getMaxCapacity());
  905. controller.addEdgeOnCanvas(e);
  906. }
  907. }
  908. }
  909. // Edge auf eine Edge gezogen?
  910. if (onEdge) {
  911. CpsEdge p = mousePositionOnEdge(x, y);
  912. if (p != null) {
  913. CpsEdge e1 = null;
  914. CpsEdge e2 = null;
  915. node = false;
  916. CpsNode n = new CpsNode("Node");
  917. n.setPosition(x, y);
  918. controller.addObjectCanvas(n);
  919. AbstractCpsObject r, k;
  920. r = p.getA();
  921. k = p.getB();
  922. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  923. e1 = new CpsEdge(n, r, model.getMaxCapacity());
  924. e2 = new CpsEdge(n, k, model.getMaxCapacity());
  925. controller.removeEdgesOnCanvas(p);
  926. controller.addEdgeOnCanvas(e);
  927. controller.addEdgeOnCanvas(e1);
  928. controller.addEdgeOnCanvas(e2);
  929. }
  930. }
  931. // ins leere Gedragged
  932. if (node) {
  933. CpsNode n = new CpsNode("Node");
  934. n.setPosition(x, y);
  935. controller.addObjectCanvas(n);
  936. e = new CpsEdge(n, tempCps, model.getMaxCapacity());
  937. controller.addEdgeOnCanvas(e);
  938. }
  939. // Wenn ein Node ohne Connections da ist
  940. if (deleteNode) {
  941. controller.delCanvasObject(tempCps, true);
  942. tempCps = null;
  943. }
  944. }
  945. }
  946. /**
  947. * Checks if the mouse is on an Edge.
  948. *
  949. * @param x
  950. * Position of the Mouse
  951. * @param y
  952. * Position of the Mouse
  953. *
  954. * @return CpsEdge the Mouse is on, null if the mouse is not on an Edge
  955. */
  956. public CpsEdge mousePositionOnEdge(int x, int y) {
  957. x += controller.getScaleDiv2();
  958. y += controller.getScaleDiv2();
  959. int lx, ly, hx, hy;
  960. for (CpsEdge p : model.getEdgesOnCanvas()) {
  961. Line2D l = new Line2D.Float(p.getA().getPosition().x, p.getA().getPosition().y, p.getB().getPosition().x,
  962. p.getB().getPosition().y);
  963. if (p.getA().getPosition().x > p.getB().getPosition().x) {
  964. hx = p.getA().getPosition().x + model.getScaleDiv2() + 7;
  965. lx = p.getB().getPosition().x + model.getScaleDiv2() - 7;
  966. } else {
  967. lx = p.getA().getPosition().x + model.getScaleDiv2() - 7;
  968. hx = p.getB().getPosition().x + model.getScaleDiv2() + 7;
  969. }
  970. if (p.getA().getPosition().y > p.getB().getPosition().y) {
  971. hy = p.getA().getPosition().y + model.getScaleDiv2() + 7;
  972. ly = p.getB().getPosition().y + model.getScaleDiv2() - 7;
  973. } else {
  974. ly = p.getA().getPosition().y + model.getScaleDiv2() - 7;
  975. hy = p.getB().getPosition().y + model.getScaleDiv2() + 7;
  976. }
  977. // distance from a point to a line and between both Objects
  978. if (l.ptLineDistSq(x - model.getScaleDiv2(), y - model.getScaleDiv2()) < 20 && x > lx && x < hx && y > ly
  979. && y < hy) {
  980. return p;
  981. }
  982. }
  983. return null;
  984. }
  985. public void updateLanguages() {
  986. itemCut.setText(Languages.getLanguage()[95]);
  987. itemCopy.setText(Languages.getLanguage()[96]);
  988. itemPaste.setText(Languages.getLanguage()[97]);
  989. itemDelete.setText(Languages.getLanguage()[98]);
  990. itemGroup.setText(Languages.getLanguage()[99]);
  991. itemUngroup.setText(Languages.getLanguage()[100]);
  992. itemTrack.setText(Languages.getLanguage()[101]);
  993. itemUntrack.setText(Languages.getLanguage()[102]);
  994. }
  995. /**
  996. * Checks if a double click was made.
  997. *
  998. * @return
  999. *
  1000. * @return true if doublecklick, false if not
  1001. */
  1002. private boolean doubleClick() {
  1003. if (click) {
  1004. click = false;
  1005. return true;
  1006. } else {
  1007. click = true;
  1008. Timer t = new Timer("doubleclickTimer", false);
  1009. t.schedule(new TimerTask() {
  1010. @Override
  1011. public void run() {
  1012. click = false;
  1013. }
  1014. }, 500);
  1015. }
  1016. return false;
  1017. }
  1018. /**
  1019. * Set if Information should be shown.
  1020. *
  1021. * @param connection
  1022. * boolean for conecction
  1023. * @param object
  1024. * boolean for objects
  1025. * @param nodeOfnode
  1026. */
  1027. public void setShowedInformation(boolean connection, boolean object, boolean border, boolean nodeOfnode) {
  1028. showedInformation[0] = connection;
  1029. showedInformation[1] = object;
  1030. showedInformation[3] = border;
  1031. showedInformation[4] = nodeOfnode;
  1032. }
  1033. /**
  1034. * Returns if Information should be shown.
  1035. *
  1036. * @return Array of boolean [0] = connection, [1] = objects
  1037. */
  1038. public boolean[] getShowedInformation() {
  1039. return showedInformation;
  1040. }
  1041. }