MyCanvas.java 31 KB

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