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