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