1 /**********************************************************************
2 * Copyright (c) 2013 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
.linuxtools
.tmf
.ui
.viewers
.xycharts
.barcharts
;
14 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
15 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
16 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
.ITmfChartTimeProvider
;
17 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
.TmfBaseProvider
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.events
.MouseEvent
;
20 import org
.eclipse
.swt
.events
.MouseMoveListener
;
21 import org
.eclipse
.swt
.events
.MouseTrackListener
;
22 import org
.eclipse
.swt
.events
.PaintEvent
;
23 import org
.eclipse
.swt
.events
.PaintListener
;
24 import org
.eclipse
.swt
.widgets
.Display
;
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 * @autor Bernd Hufmann
36 public class TmfHistogramTooltipProvider
extends TmfBaseProvider
implements MouseTrackListener
, MouseMoveListener
, PaintListener
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
41 /** X coordinate for highlighting */
42 private int fHighlightX
;
43 /** y coordinate for highlighting */
44 private int fHighlightY
;
45 /** Flag to do highlighting or not */
46 private boolean fIsHighlight
;
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 * Constructor for a tool tip provider.
54 * @param tmfChartViewer
55 * - the parent chart viewer
57 public TmfHistogramTooltipProvider(ITmfChartTimeProvider tmfChartViewer
) {
58 super(tmfChartViewer
);
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 public void register() {
67 getChart().getPlotArea().addMouseTrackListener(this);
68 getChart().getPlotArea().addMouseMoveListener(this);
69 getChart().getPlotArea().addPaintListener(this);
73 public void deregister() {
74 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
75 getChart().getPlotArea().removeMouseTrackListener(this);
76 getChart().getPlotArea().removeMouseMoveListener(this);
77 getChart().getPlotArea().removePaintListener(this);
82 public void refresh() {
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
90 public void mouseEnter(MouseEvent e
) {
94 public void mouseExit(MouseEvent e
) {
98 public void mouseHover(MouseEvent e
) {
99 if (getChartViewer().getWindowDuration() != 0) {
100 IAxis xAxis
= getChart().getAxisSet().getXAxis(0);
101 IAxis yAxis
= getChart().getAxisSet().getYAxis(0);
103 double xCoordinate
= xAxis
.getDataCoordinate(e
.x
);
105 ISeries
[] series
= getChart().getSeriesSet().getSeries();
107 if ((xCoordinate
< 0) || (series
.length
== 0)) {
112 double rangeStart
= 0.0;
113 double rangeEnd
= 0.0;
115 // Consider first series only
116 double[] xS
= series
[0].getXSeries();
117 double[] yS
= series
[0].getYSeries();
119 if ((xS
== null) || (yS
== null)) {
123 for (int i
= 0; i
< xS
.length
- 1; i
++) {
124 int pixel
= xAxis
.getPixelCoordinate(xS
[i
]);
127 rangeEnd
= (long) xS
[i
+ 1];
128 if (xCoordinate
>= rangeStart
) {
136 ITmfChartTimeProvider viewer
= getChartViewer();
138 /* set tooltip of closest data point */
139 StringBuffer buffer
= new StringBuffer();
140 buffer
.append("Range=["); //$NON-NLS-1$
141 buffer
.append(new TmfTimestamp((long) rangeStart
+ viewer
.getTimeOffset(), ITmfTimestamp
.NANOSECOND_SCALE
).toString());
143 buffer
.append(new TmfTimestamp((long) rangeEnd
+ viewer
.getTimeOffset(), ITmfTimestamp
.NANOSECOND_SCALE
).toString());
144 buffer
.append("]\n"); //$NON-NLS-1$
145 buffer
.append("y="); //$NON-NLS-1$
146 buffer
.append((long) y
);
147 getChart().getPlotArea().setToolTipText(buffer
.toString());
150 fHighlightY
= yAxis
.getPixelCoordinate(y
);
156 // ------------------------------------------------------------------------
158 // ------------------------------------------------------------------------
160 public void mouseMove(MouseEvent e
) {
161 fIsHighlight
= false;
165 // ------------------------------------------------------------------------
167 // ------------------------------------------------------------------------
169 public void paintControl(PaintEvent e
) {
171 e
.gc
.setBackground(Display
.getDefault().getSystemColor(
175 e
.gc
.fillOval(fHighlightX
- 5, fHighlightY
- 5, 10, 10);