MyCanvas.java 46 KB

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