MyCanvas.java 36 KB

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