UnitGraph.java 24 KB

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