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
15
16
17
18
19
20
21
22
23 abstract class PointerHandler implements MouseListener, MouseMotionListener
24 {
25
26
27 @Override
28 public void mouseDragged(final MouseEvent e)
29 {
30
31 }
32
33
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
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()), plot
52 .getRangeAxis().java2DToValue(p.getY(), pi.getDataArea(), plot.getRangeAxisEdge()));
53 }
54 else
55 {
56 updateHint(Double.NaN, Double.NaN);
57 }
58 }
59
60
61
62
63
64
65
66 abstract void updateHint(double domainValue, double rangeValue);
67
68
69 @Override
70 public void mouseClicked(final MouseEvent e)
71 {
72
73 }
74
75
76 @Override
77 public void mousePressed(final MouseEvent e)
78 {
79
80 }
81
82
83 @Override
84 public void mouseReleased(final MouseEvent e)
85 {
86
87 }
88
89
90 @Override
91 public void mouseEntered(final MouseEvent e)
92 {
93
94 }
95
96
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
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);
110 }
111 }