View Javadoc
1   package org.opentrafficsim.graphs;
2   
3   import java.awt.event.MouseEvent;
4   import java.awt.event.MouseListener;
5   import java.awt.event.MouseMotionListener;
6   import java.awt.geom.Point2D;
7   
8   import org.jfree.chart.ChartPanel;
9   import org.jfree.chart.event.PlotChangeEvent;
10  import org.jfree.chart.plot.PlotRenderingInfo;
11  import org.jfree.chart.plot.XYPlot;
12  
13  /**
14   * <p>
15   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * <p>
18   * $LastChangedDate: 2015-07-26 01:01:13 +0200 (Sun, 26 Jul 2015) $, @version $Revision: 1155 $, by $Author: averbraeck $,
19   * initial version Aug 13, 2014 <br>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   */
22  abstract class PointerHandler implements MouseListener, MouseMotionListener
23  {
24      /** {@inheritDoc} */
25      @Override
26      public void mouseDragged(final MouseEvent e)
27      {
28          // No action
29      }
30  
31      /** {@inheritDoc} */
32      @Override
33      public void mouseMoved(final MouseEvent mouseEvent)
34      {
35          final ChartPanel cp = (ChartPanel) mouseEvent.getSource();
36          final XYPlot plot = (XYPlot) cp.getChart().getPlot();
37          // Show a cross hair cursor while the mouse is on the graph
38          final boolean showCrossHair = cp.getScreenDataArea().contains(mouseEvent.getPoint());
39          if (cp.getHorizontalAxisTrace() != showCrossHair)
40          {
41              cp.setHorizontalAxisTrace(showCrossHair);
42              cp.setVerticalAxisTrace(showCrossHair);
43              plot.notifyListeners(new PlotChangeEvent(plot));
44          }
45          if (showCrossHair)
46          {
47              Point2D p = cp.translateScreenToJava2D(mouseEvent.getPoint());
48              PlotRenderingInfo pi = cp.getChartRenderingInfo().getPlotInfo();
49              updateHint(plot.getDomainAxis().java2DToValue(p.getX(), pi.getDataArea(), plot.getDomainAxisEdge()), plot
50                  .getRangeAxis().java2DToValue(p.getY(), pi.getDataArea(), plot.getRangeAxisEdge()));
51          }
52          else
53          {
54              updateHint(Double.NaN, Double.NaN);
55          }
56      }
57  
58      /**
59       * Called when the pointer is positioned inside the data area of the graph, or when it leaves the data area. <br>
60       * When the mouse is outside the data area both parameters are set to Double.NaN.
61       * @param domainValue Double; the X-value (in domain units), or Double.NaN if the pointer is outside the data area
62       * @param rangeValue Double; the Y-value (in domain units), or Double.NaN if the pointer is outside the data area
63       */
64      abstract void updateHint(double domainValue, double rangeValue);
65  
66      /** {@inheritDoc} */
67      @Override
68      public void mouseClicked(final MouseEvent e)
69      {
70          // No action
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public void mousePressed(final MouseEvent e)
76      {
77          // No action
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public void mouseReleased(final MouseEvent e)
83      {
84          // No action
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public void mouseEntered(final MouseEvent e)
90      {
91          // No action
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public void mouseExited(final MouseEvent mouseEvent)
97      {
98          final ChartPanel cp = (ChartPanel) mouseEvent.getSource();
99          final XYPlot plot = (XYPlot) cp.getChart().getPlot();
100         // Remove the cross hair cursor when the cursor moves outside the graph
101         if (cp.getHorizontalAxisTrace())
102         {
103             cp.setHorizontalAxisTrace(false);
104             cp.setVerticalAxisTrace(false);
105             plot.notifyListeners(new PlotChangeEvent(plot));
106         }
107         updateHint(Double.NaN, Double.NaN); // Clear the hint text
108     }
109 }