UnitGraph.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. package ui.view;
  2. import classes.*;
  3. import classes.comparator.UnitGraphPointComperator;
  4. import interfaces.GraphEditable;
  5. import interfaces.GraphEditable.Graphtype;
  6. import interfaces.LocalMode;
  7. import sun.reflect.generics.reflectiveObjects.NotImplementedException;
  8. import ui.controller.Control;
  9. import ui.controller.SingletonControl;
  10. import ui.model.Model;
  11. import javax.swing.*;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. import java.awt.geom.Path2D;
  15. import java.awt.geom.Point2D;
  16. import java.util.ArrayList;
  17. import java.util.LinkedList;
  18. import java.util.ListIterator;
  19. /**
  20. * This Class represents a Graph where the User can model the behavior of
  21. * elements and switches over time.
  22. *
  23. * @author Tom Troppmann
  24. */
  25. public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
  26. private static final long serialVersionUID = 1L;
  27. // Normal Settings
  28. private int border = 4;
  29. private int clickThreshholdSquared = 25;
  30. // Display Settings
  31. /**
  32. * The size of a dot in the graph.
  33. * It should be at least 1.
  34. * */
  35. int dotSize = 8;
  36. /** The Color of a dot in the graph. */
  37. Color dotColor = Color.blue;
  38. Color editDotColor = new Color(255, 119, 0);
  39. //Intern Variables
  40. //TODO: JavaDoc
  41. private LinkedList<UnitGraphPoint> actualGraphPoints = new LinkedList<UnitGraphPoint>();
  42. private Graphtype actualGraphType;
  43. private GraphEditable actualElement;
  44. Position editPosition;
  45. boolean editMode = false;
  46. private enum pointType {Normal, StartPoint, EndPoint};
  47. pointType editPoint = pointType.Normal;
  48. //Maybe Needed
  49. private Model model;
  50. private Control controller;
  51. private int widthWithBorder, heightWithBorder;
  52. /**
  53. * Constructor.
  54. *
  55. * @param model the Model
  56. * @param control the Controller
  57. */
  58. public UnitGraph(final Model model, Control control) {
  59. setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  60. this.controller = control;
  61. this.model = model;
  62. this.setBackground(Color.WHITE);
  63. this.addMouseListener(this);
  64. this.addMouseMotionListener(this);
  65. this.addComponentListener(this);
  66. }
  67. /**
  68. * When the UnitGraph should represent a new GraphEditable Element.
  69. * Its Updates the Graph and give access to the Element.
  70. * @param element
  71. */
  72. public void initNewElement(GraphEditable element)
  73. {
  74. overrideUnitGraph(element.getStateGraph());
  75. actualGraphType = element.getGraphType();
  76. actualElement = element;
  77. repaint();
  78. }
  79. /**
  80. * Paints the Graph, the Grid, the actual Line fro the currentIteration
  81. * @param g Graphics
  82. */
  83. public void paintComponent(Graphics g) {
  84. super.paintComponent(g);
  85. Graphics2D g2D = (Graphics2D) g;
  86. drawGrid(g2D);
  87. g2D.setColor(Color.BLACK);
  88. g2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  89. g2D.setStroke(new BasicStroke(2));
  90. drawUnitGraph(g2D);
  91. g2D.setColor(dotColor);
  92. if(editMode)
  93. {
  94. drawUnitGraphPointsReleased(g2D);
  95. }else
  96. {
  97. drawUnitGraphPoints(g2D);
  98. }
  99. g2D.setColor(dotColor);
  100. g2D.setStroke(new BasicStroke(1));
  101. drawCurrentIterartionLine(g2D);
  102. }
  103. // Draw Methods only to let the User see the changes. Nothing its saved here or changed.
  104. /**
  105. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  106. * <p>
  107. * This Methods draws the UnitGraph whether its a boolGraph or a doubleGraph.
  108. * @param g to draw.
  109. */
  110. private void drawUnitGraph(Graphics2D g) {
  111. switch(actualGraphType) {
  112. case boolGraph:
  113. if(editMode)
  114. drawBoolGraphInEditMode(g);
  115. else
  116. drawBoolGraph(g);
  117. break;
  118. case doubleGraph:
  119. if(editMode)
  120. drawDoubleGraphInEditMode(g);
  121. else
  122. drawDoubleGraph(g);
  123. break;
  124. default:
  125. throw new UnsupportedOperationException();
  126. }
  127. }
  128. /**
  129. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  130. * <p>
  131. * This Methods draws the UnitGraphPoints of the UnitGraph.
  132. * @param g to draw.
  133. */
  134. private void drawUnitGraphPoints(Graphics2D g) {
  135. g.setColor(dotColor);
  136. for(UnitGraphPoint p : actualGraphPoints){
  137. drawDot(g, p.displayedPosition);
  138. }
  139. }
  140. /**
  141. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  142. * <p>
  143. * This Methods draws the UnitGraphPoints of the UnitGraph when its in EditMode.
  144. * @param g to draw.
  145. */
  146. private void drawUnitGraphPointsReleased(Graphics2D g) {
  147. drawUnitGraphPoints(g);
  148. g.setColor(editDotColor);
  149. drawDot(g, editPosition);
  150. }
  151. /**
  152. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  153. * <p>
  154. * This Methods draws the Grid on the Canvas.
  155. * @param g2D to draw.
  156. */
  157. private void drawGrid(Graphics2D g2D) {
  158. g2D.setStroke(new BasicStroke(1));
  159. g2D.setColor(Color.lightGray);
  160. int amountOfLines = 10;
  161. int width = widthWithBorder + 2 * border;
  162. int height = heightWithBorder;
  163. for(int i = 0; i<=amountOfLines; i++)
  164. {
  165. int linehieght = (int) (((double)i/ (double) amountOfLines) * (double) height) + border;
  166. g2D.drawLine(0, linehieght, width, linehieght);
  167. }
  168. }
  169. /**
  170. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  171. * <p>
  172. * This Method draws the CurrentIterationLine.
  173. * @param g2D to draw.
  174. */
  175. private void drawCurrentIterartionLine(Graphics2D g)
  176. {
  177. int cur = model.getCurIteration();
  178. int max = model.getIterations();
  179. double where;
  180. if(!this.isUsingLocalPeriod()) {
  181. where = ((double) cur)/((double) max);
  182. }
  183. else
  184. {
  185. int lPeriod = this.getLocalPeriod();
  186. where = ((double) cur%lPeriod)/((double) lPeriod);
  187. }
  188. Position oben = new Position(border + (int)(where * widthWithBorder), 0);
  189. Position unten = new Position(border + (int)(where * widthWithBorder), 2 * border + heightWithBorder);
  190. drawLine(g,oben,unten);
  191. }
  192. /**
  193. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  194. * <p>
  195. * This Method draws a line between two Positions on the Canvas.
  196. * @param g2D to draw.
  197. * @param start the Position of one end of the line to draw.
  198. * @param end the other Ends Position of the Line to draw.
  199. */
  200. private void drawLine(Graphics2D g, Position start, Position end)
  201. {
  202. Path2D.Double path = new Path2D.Double();
  203. path.moveTo(start.x, start.y);
  204. path.lineTo(end.x, end.y);
  205. g.draw(path);
  206. }
  207. /**
  208. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  209. * <p>
  210. * Initialize a Cubic BezierCurve.
  211. * @param start The Position to start the Curve.
  212. */
  213. private Path2D.Double initBezier(Position start) {
  214. //Good Source for basic understanding for Bezier Curves
  215. //http://www.theappguruz.com/blog/bezier-curve-in-games
  216. Path2D.Double path = new Path2D.Double();
  217. path.moveTo(start.x, start.y);
  218. return path;
  219. }
  220. /**
  221. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  222. * <p>
  223. * Calculate the Path of a the Cubic BezierCurve with the special controlPoints to make the wanted behavior.
  224. * @param path the path of the Bezier.
  225. * @param actaul the actual Position of the Path.
  226. * @param target the end Position of the Curve.
  227. */
  228. private void curveTo(Path2D.Double path, Position actual, Position target) {
  229. double mitte = (actual.x + target.x)* 0.5;
  230. path.curveTo(mitte, actual.y, mitte, target.y, target.x, target.y);
  231. }
  232. /**
  233. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  234. * <p>
  235. * Draws a Dot at a Position.
  236. * @param g to draw.
  237. * @param p the position of the Dot.
  238. */
  239. private void drawDot(Graphics2D g, Position p)
  240. {
  241. g.fillOval(p.x -dotSize/2, p.y-dotSize/2, dotSize, dotSize);
  242. }
  243. /**
  244. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  245. * <p>
  246. * This Method draws the UnitGraph as BoolGraph.
  247. * @param g2D to draw.
  248. */
  249. private void drawBoolGraph(Graphics2D g) {
  250. if(actualGraphPoints.size() <= 1) return;
  251. LinkedList<Position> cornerPoints = new LinkedList<Position>();
  252. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  253. Position actual = actualGraphPoints.getFirst().displayedPosition;
  254. Path2D.Double path = new Path2D.Double();
  255. path.moveTo(actual.x, actual.y);
  256. while (iter.hasNext())
  257. {
  258. Position target = iter.next().displayedPosition;
  259. //BooleanConnection
  260. path.lineTo(target.x, actual.y); //line to corner
  261. cornerPoints.add(new Position(target.x, actual.y)); //save corner
  262. path.lineTo(target.x, target.y); //line to next Point
  263. actual = target;
  264. }
  265. g.draw(path);
  266. //Draw the Points on the Corner that dont exist in Data but should be visual
  267. g.setColor(dotColor);
  268. for(Position p: cornerPoints)
  269. {
  270. drawDot(g, p);
  271. }
  272. }
  273. /**
  274. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  275. * <p>
  276. * This Method draws the UnitGraph as BoolGraph in EditMode.
  277. * @param g2D to draw.
  278. */
  279. private void drawBoolGraphInEditMode(Graphics2D g) {
  280. LinkedList<Position> before = new LinkedList<Position>();
  281. LinkedList<Position> after = new LinkedList<Position>();
  282. for(UnitGraphPoint p: actualGraphPoints)
  283. {
  284. if(p.displayedPosition.x < editPosition.x)
  285. before.add(p.displayedPosition);
  286. else
  287. after.add(p.displayedPosition);
  288. }
  289. g.setColor(Color.BLACK);
  290. drawBoolGraphFromList(g, before);
  291. g.setColor(Color.BLACK);
  292. drawBoolGraphFromList(g, after);
  293. //EditGraph
  294. LinkedList<Position> middle = new LinkedList<Position>();
  295. if(!before.isEmpty()) middle.add(before.getLast());
  296. middle.add(editPosition);
  297. if(!after.isEmpty()) middle.add(after.getFirst());
  298. g.setColor(editDotColor);
  299. drawBoolGraphFromList(g, middle);
  300. drawSnappingHint(g);
  301. }
  302. /**
  303. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  304. * <p>
  305. * This Method draws a red Hint to signal the User the snapping of the hovered Point under the Cursor in EditMode.
  306. * @param g2D to draw.
  307. */
  308. private void drawSnappingHint(Graphics2D g)
  309. {
  310. //ColorHint
  311. g.setColor(Color.RED);
  312. //Threshhold Line
  313. final float dash1[] = {10.0f};
  314. final BasicStroke dashed =new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
  315. g.setStroke(dashed);
  316. int halfheight = border + heightWithBorder / 2;
  317. g.drawLine(0, halfheight , widthWithBorder + 2 * border, halfheight);
  318. //Threshhold Text
  319. g.drawString("Snapping Threshold", 10, halfheight - 2);
  320. }
  321. /**
  322. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  323. * <p>
  324. * This Method draws a partial Graph from a Position List as BoolGraph.
  325. * @param g2D to draw.
  326. * @param list the PositionList to draw a BoolGraph
  327. */
  328. private void drawBoolGraphFromList(Graphics2D g, LinkedList<Position> list) {
  329. if(list.size() <= 1) return;
  330. ListIterator<Position> iter = list.listIterator();
  331. LinkedList<Position> cornerPoints = new LinkedList<Position>();
  332. Position actual = list.getFirst();
  333. Path2D.Double path = new Path2D.Double();
  334. path.moveTo(actual.x, actual.y);
  335. while (iter.hasNext())
  336. {
  337. Position target = iter.next();
  338. //BooleanConnection
  339. path.lineTo(target.x, actual.y); //line to corner
  340. cornerPoints.add(new Position(target.x, actual.y)); //save corner
  341. path.lineTo(target.x, target.y); //line to next Point
  342. actual = target;
  343. }
  344. g.draw(path);
  345. g.setColor(dotColor);
  346. for(Position p: cornerPoints)
  347. {
  348. drawDot(g, p);
  349. }
  350. }
  351. /**
  352. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  353. * <p>
  354. * This Method draws the UnitGraph as DoubleGraph.
  355. * @param g2D to draw.
  356. */
  357. private void drawDoubleGraph(Graphics2D g) {
  358. if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
  359. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  360. Position actual = iter.next().displayedPosition;
  361. Path2D.Double path = this.initBezier(actual);
  362. while (iter.hasNext())
  363. {
  364. Position target = iter.next().displayedPosition;
  365. this.curveTo(path, actual, target);
  366. actual = target;
  367. }
  368. g.draw(path);
  369. }
  370. /**
  371. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  372. * <p>
  373. * This Method draws the UnitGraph as DoubleGraph in EditMode.
  374. * @param g2D to draw.
  375. */
  376. private void drawDoubleGraphInEditMode(Graphics2D g) {
  377. LinkedList<Position> before = new LinkedList<Position>();
  378. LinkedList<Position> after = new LinkedList<Position>();
  379. for(UnitGraphPoint p: actualGraphPoints)
  380. {
  381. if(p.displayedPosition.x < editPosition.x)
  382. before.add(p.displayedPosition);
  383. else
  384. after.add(p.displayedPosition);
  385. }
  386. drawUnitGraphFromList(g, before);
  387. drawUnitGraphFromList(g, after);
  388. //EditGraph
  389. LinkedList<Position> middle = new LinkedList<Position>();
  390. if(!before.isEmpty()) middle.add(before.getLast());
  391. middle.add(editPosition);
  392. if(!after.isEmpty()) middle.add(after.getFirst());
  393. g.setColor(editDotColor);
  394. drawUnitGraphFromList(g, middle);
  395. }
  396. /**
  397. * Helper Method to draw the UnitGraphPanel. {@link UnitGraph#paintComponent(Graphics)}
  398. * <p>
  399. * This Method draws a partial Graph from a Position List as DoubleGraph.
  400. * @param g2D to draw.
  401. * @param list the PositionList to draw a DoubleGraph
  402. */
  403. private void drawUnitGraphFromList(Graphics2D g, LinkedList<Position> list) {
  404. if(list.size() <= 1) return;
  405. ListIterator<Position> iter = list.listIterator();
  406. Position actual = list.getFirst();
  407. Path2D.Double path = this.initBezier(actual);
  408. while (iter.hasNext())
  409. {
  410. Position target = iter.next();
  411. curveTo(path, actual, target);
  412. actual = target;
  413. }
  414. g.draw(path);
  415. }
  416. //Under the hood functions to calculate and function the
  417. /**
  418. * A unitgraphpoint have a x and y position to store the data of a graph point.
  419. * Also it have a displayposition to store the Position of the GraphPoints on the Canvas.
  420. * e.g. when the canvas has 500 width and 200 height a GraphPoint with the X=0.5 and Y=1.0 should have a displayposition of (250,3) when border is 3.
  421. */
  422. private void updateRepresentativePositions()
  423. {
  424. for(UnitGraphPoint p : actualGraphPoints) {
  425. p.calcDisplayedPosition(border, widthWithBorder, heightWithBorder);
  426. }
  427. }
  428. /**
  429. * Takes a List of GraphPoints and convert it to the actual UnitGraphPoints with displayposition in the {@link #actualGraphPoints}
  430. * @param stateCurve the list of GraphPoint
  431. */
  432. private void overrideUnitGraph(LinkedList<Point2D.Double> stateCurve) {
  433. actualGraphPoints.clear();
  434. for(Point2D.Double p: stateCurve){
  435. actualGraphPoints.add(new UnitGraphPoint(p));
  436. }
  437. updateRepresentativePositions();
  438. }
  439. /**
  440. * When the PanelSize Change the width and height to calculate the drawings have to be adjusted.
  441. */
  442. private void calculateWidthHeight()
  443. {
  444. widthWithBorder = this.getWidth() - 2 * border;
  445. heightWithBorder = this.getHeight() - 2 * border;
  446. }
  447. /**
  448. * Save the actualGraphPoint List to the GraphEditable Element.
  449. */
  450. private void saveGraph() {
  451. LinkedList<Point2D.Double> actual = actualElement.getStateGraph();
  452. actual.clear();
  453. for(UnitGraphPoint p: actualGraphPoints)
  454. {
  455. actual.add(p.getPoint());
  456. }
  457. actualElement.sampleGraph();
  458. }
  459. /**
  460. * Remove a UnitGraphPoint from the UnitGraphPoint list ({@link #actualGraphPoints} when its near a given Position.
  461. * @param mPosition
  462. */
  463. private void removePointNearPosition(Position mPosition) {
  464. ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
  465. while (iter.hasNext())
  466. {
  467. if(near(mPosition,iter.next().displayedPosition))
  468. {
  469. iter.remove();
  470. break;
  471. }
  472. }
  473. }
  474. /**
  475. * Determine if the Point is a StartPoint , EndPoint or a NormalPoint a.k.a. in between Points.
  476. * @param mPosition The Position to check.
  477. */
  478. private void detectStartEndPoint(Position mPosition)
  479. {
  480. UnitGraphPoint first = actualGraphPoints.getFirst();
  481. UnitGraphPoint last = actualGraphPoints.getLast();
  482. if(near(mPosition, first.displayedPosition)) editPoint = pointType.StartPoint;
  483. else if(near(mPosition, last.displayedPosition)) editPoint = pointType.EndPoint;
  484. else editPoint = pointType.Normal;
  485. }
  486. /**
  487. * Determine if a Point is near the Cursor (depends on Mode what near means). To detect if it should grab the Point or create a new Point.
  488. * @param actual
  489. * @param target
  490. * @return
  491. */
  492. private boolean near(Position actual, Position target) {
  493. switch(actualGraphType)
  494. {
  495. case boolGraph: //Distance only with X
  496. int xDis = target.x - actual.x;
  497. return xDis * xDis < clickThreshholdSquared;
  498. case doubleGraph:
  499. return actual.squareDistance(target) < clickThreshholdSquared;
  500. default:
  501. return false;
  502. }
  503. }
  504. /**
  505. * When the Mouse Drag a Point it updates each time the position.
  506. * @param newPosition
  507. */
  508. private void updateEditPointPosition(Position newPosition) {
  509. //make it in the bounds of the UnitGraph no Point out of the Border
  510. Position currentPosition = setInBounds(newPosition);
  511. if(editPoint != pointType.Normal) attachToBorder(currentPosition);
  512. if(actualGraphType == Graphtype.boolGraph) snapBoolean(currentPosition);
  513. this.editPosition = currentPosition;
  514. }
  515. /**
  516. * No Point on the UnitGraph should exit the UnitGraph.
  517. * @param p the Position
  518. * @return the updated Position.
  519. */
  520. private Position setInBounds(Position p) {
  521. p.clampX(border, border + widthWithBorder);
  522. p.clampY(border, border + heightWithBorder);
  523. return p;
  524. }
  525. /**
  526. * For Switches the Point have to be Snap to the Top or the Bottem.
  527. * @param p the Position
  528. * @return the updated Position.
  529. */
  530. private Position snapBoolean(Position p)
  531. {
  532. if (p.y < border + heightWithBorder / 2) {
  533. p.y = border;
  534. } else {
  535. p.y = border + heightWithBorder;
  536. }
  537. return p;
  538. }
  539. /**
  540. * The First Point has to be at 0(LeftSide) and Last Point has to be at 1(RightSide).
  541. * @param p the Position
  542. * @return the updated Position.
  543. */
  544. private Position attachToBorder(Position p)
  545. {
  546. switch(editPoint)
  547. {
  548. case StartPoint:
  549. p.x = border;
  550. break;
  551. case EndPoint:
  552. p.x = border + widthWithBorder;
  553. break;
  554. default:
  555. break;
  556. }
  557. return p;
  558. }
  559. /**
  560. * Insert a Position in the UnitGraphList at the right order.
  561. * Its sorted based on the xValues.
  562. * @param pos The new UnitGraphPoints Position
  563. */
  564. private void insertNewGraphPoint(Position pos)
  565. {
  566. setInBounds(pos);
  567. ListIterator<UnitGraphPoint> iter2 = actualGraphPoints.listIterator();
  568. while (iter2.hasNext())
  569. {
  570. Position tempPosition = iter2.next().displayedPosition;
  571. if(pos.x <= tempPosition.x)
  572. {
  573. //previous to go back a position to make the new point before the the Position with greater X
  574. iter2.previous();
  575. iter2.add(generateUnitGraphPoint(pos));
  576. break;
  577. }
  578. }
  579. if(!iter2.hasNext()) //if behind last point
  580. {
  581. iter2.add(generateUnitGraphPoint(pos));
  582. }
  583. }
  584. /**
  585. * Generate a UnitGraphPoint from a normal Position in the UnitGraph.
  586. * @param pos the normal pos with xValues from 0..Width and yValues from 0..Height
  587. * @return a UnitGraphPoint
  588. */
  589. private UnitGraphPoint generateUnitGraphPoint(Position pos) {
  590. UnitGraphPoint temp = new UnitGraphPoint((double) (pos.x - border) / (double) widthWithBorder,
  591. 1 - (double) (pos.y - border) / (double) heightWithBorder, true);
  592. temp.displayedPosition = pos;
  593. return temp;
  594. }
  595. /**
  596. * Update the Point Position
  597. */
  598. @Override
  599. public void mouseDragged(MouseEvent e) {
  600. updateEditPointPosition(new Position(e.getPoint()));
  601. repaint();
  602. }
  603. @Override
  604. public void mouseMoved(MouseEvent e) {
  605. }
  606. @Override
  607. public void mouseClicked(MouseEvent e) {
  608. }
  609. @Override
  610. public void mouseEntered(MouseEvent e) {
  611. }
  612. @Override
  613. public void mouseExited(MouseEvent e) {
  614. }
  615. /**
  616. * The First Step.
  617. * When LeftMouseButton its checks if a point is to grab under the cursor or create a new Point. Then enter EditMode.
  618. * When RightMouseButton its delete a point if its under the Curser.
  619. */
  620. @Override
  621. public void mousePressed(MouseEvent e) {
  622. Position mPosition = new Position(e.getPoint());
  623. if (e.getButton() == MouseEvent.BUTTON3) {
  624. // RightMouseButtonEvent
  625. detectStartEndPoint(mPosition);
  626. if (editPoint == pointType.Normal) {
  627. removePointNearPosition(mPosition);
  628. repaint();
  629. }
  630. editMode = false;
  631. } else if (e.getButton() == MouseEvent.BUTTON1) {
  632. // LeftMouseButtonEvent
  633. detectStartEndPoint(mPosition);
  634. removePointNearPosition(mPosition);
  635. updateEditPointPosition(mPosition);
  636. editMode = true;
  637. repaint();
  638. }
  639. }
  640. /**
  641. * The last step to save the Changes.
  642. * Its insert the Hovering Point and exit EditMode.
  643. */
  644. @Override
  645. public void mouseReleased(MouseEvent e) {
  646. if(editMode)
  647. {
  648. this.insertNewGraphPoint(editPosition);
  649. editMode = false;
  650. repaint();
  651. }
  652. saveGraph();
  653. }
  654. /**
  655. * When the Component is Resized.
  656. *
  657. * @param e ComponentEvent
  658. */
  659. public void componentResized(ComponentEvent e) {
  660. calculateWidthHeight();
  661. updateRepresentativePositions();
  662. repaint();
  663. }
  664. @Override
  665. public void componentHidden(ComponentEvent e) {
  666. }
  667. @Override
  668. public void componentMoved(ComponentEvent e) {
  669. }
  670. @Override
  671. public void componentShown(ComponentEvent e) {
  672. }
  673. /**
  674. * Resets the graph to normal.
  675. */
  676. public void reset() {
  677. if(this.actualElement != null) {
  678. actualElement.reset();
  679. overrideUnitGraph(actualElement.getStateGraph());
  680. repaint();
  681. }
  682. }
  683. //LocalMode access methods...
  684. //To access a element from the GUI for the LocalMode
  685. public void setUseLocalPeriod(boolean state) {
  686. if(this.actualElement != null) {
  687. ((LocalMode) actualElement).setUseLocalPeriod(state);
  688. }
  689. }
  690. public void setLocalPeriod(int localLength) {
  691. if(this.actualElement != null) {
  692. ((LocalMode) actualElement).setLocalPeriod(localLength);
  693. }
  694. }
  695. public int getLocalPeriod() {
  696. if(this.actualElement != null) {
  697. return ((LocalMode) actualElement).getLocalPeriod();
  698. }
  699. return -1;
  700. }
  701. public boolean isUsingLocalPeriod() {
  702. if(this.actualElement != null) {
  703. return ((LocalMode) actualElement).isUsingLocalPeriod();
  704. }
  705. return false;
  706. }
  707. /**
  708. * @deprecated
  709. * @param cps
  710. */
  711. public void repaintGraph(AbstractCpsObject cps) {
  712. System.out.println("repaintGraph");
  713. }
  714. /**
  715. * @deprecated
  716. * @param obj
  717. */
  718. public void update(ArrayList<AbstractCpsObject> obj) {
  719. System.out.println("update");
  720. }
  721. }