1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.barcharts
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.events
.MouseEvent
;
16 import org
.eclipse
.swt
.events
.MouseMoveListener
;
17 import org
.eclipse
.swt
.events
.MouseTrackListener
;
18 import org
.eclipse
.swt
.events
.PaintEvent
;
19 import org
.eclipse
.swt
.events
.PaintListener
;
20 import org
.eclipse
.swt
.widgets
.Display
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
23 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.ITmfChartTimeProvider
;
24 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.TmfBaseProvider
;
25 import org
.swtchart
.IAxis
;
26 import org
.swtchart
.ISeries
;
29 * Tool tip provider for TMF bar chart viewer. It displays the y value of
30 * position x as well as it highlights the bar of the x position.
31 * It only considers the first series of the chart.
33 * @author Bernd Hufmann
35 public class TmfHistogramTooltipProvider
extends TmfBaseProvider
implements MouseTrackListener
, MouseMoveListener
, PaintListener
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
40 /** X coordinate for highlighting */
41 private int fHighlightX
;
42 /** y coordinate for highlighting */
43 private int fHighlightY
;
44 /** Flag to do highlighting or not */
45 private boolean fIsHighlight
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 * Constructor for a tool tip provider.
53 * @param tmfChartViewer
54 * - the parent chart viewer
56 public TmfHistogramTooltipProvider(ITmfChartTimeProvider tmfChartViewer
) {
57 super(tmfChartViewer
);
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 public void register() {
66 getChart().getPlotArea().addMouseTrackListener(this);
67 getChart().getPlotArea().addMouseMoveListener(this);
68 getChart().getPlotArea().addPaintListener(this);
72 public void deregister() {
73 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
74 getChart().getPlotArea().removeMouseTrackListener(this);
75 getChart().getPlotArea().removeMouseMoveListener(this);
76 getChart().getPlotArea().removePaintListener(this);
81 public void refresh() {
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
89 public void mouseEnter(MouseEvent e
) {
93 public void mouseExit(MouseEvent e
) {
97 public void mouseHover(MouseEvent e
) {
98 if (getChartViewer().getWindowDuration() != 0) {
99 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
100 IAxis yAxis
= getChart().getAxisSet().getYAxis(0);
102 double xCoordinate
= xAxis
.getDataCoordinate(e
.x
);
104 ISeries
[] series
= getChart().getSeriesSet().getSeries();
106 if ((xCoordinate
< 0) || (series
.length
== 0)) {
111 double rangeStart
= 0.0;
112 double rangeEnd
= 0.0;
114 // Consider first series only
115 double[] xS
= series
[0].getXSeries();
116 double[] yS
= series
[0].getYSeries();
118 if ((xS
== null) || (yS
== null)) {
122 for (int i
= 0; i
< xS
.length
- 1; i
++) {
123 int pixel
= xAxis
.getPixelCoordinate(xS
[i
]);
126 rangeEnd
= (long) xS
[i
+ 1];
127 if (xCoordinate
>= rangeStart
) {
135 ITmfChartTimeProvider viewer
= getChartViewer();
137 /* set tooltip of closest data point */
138 StringBuffer buffer
= new StringBuffer();
139 buffer
.append("Range=["); //$NON-NLS-1$
140 buffer
.append(new TmfTimestamp((long) rangeStart
+ viewer
.getTimeOffset(), ITmfTimestamp
.NANOSECOND_SCALE
).toString());
142 buffer
.append(new TmfTimestamp((long) rangeEnd
+ viewer
.getTimeOffset(), ITmfTimestamp
.NANOSECOND_SCALE
).toString());
143 buffer
.append("]\n"); //$NON-NLS-1$
144 buffer
.append("y="); //$NON-NLS-1$
145 buffer
.append((long) y
);
146 getChart().getPlotArea().setToolTipText(buffer
.toString());
149 fHighlightY
= yAxis
.getPixelCoordinate(y
);
155 // ------------------------------------------------------------------------
157 // ------------------------------------------------------------------------
159 public void mouseMove(MouseEvent e
) {
160 fIsHighlight
= false;
164 // ------------------------------------------------------------------------
166 // ------------------------------------------------------------------------
168 public void paintControl(PaintEvent e
) {
170 e
.gc
.setBackground(Display
.getDefault().getSystemColor(
174 e
.gc
.fillOval(fHighlightX
- 5, fHighlightY
- 5, 10, 10);