1 /**********************************************************************
2 * Copyright (c) 2015, 2016 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
8 **********************************************************************/
9 package org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.views
.segmentstore
.density
;
11 import java
.text
.Format
;
12 import java
.text
.MessageFormat
;
14 import org
.eclipse
.jdt
.annotation
.Nullable
;
15 import org
.eclipse
.swt
.events
.MouseEvent
;
16 import org
.eclipse
.swt
.events
.MouseTrackListener
;
17 import org
.eclipse
.swt
.graphics
.Rectangle
;
18 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.SubSecondTimeWithUnitFormat
;
19 import org
.eclipse
.tracecompass
.analysis
.timing
.ui
.views
.segmentstore
.density
.AbstractSegmentStoreDensityViewer
;
20 import org
.swtchart
.IAxis
;
21 import org
.swtchart
.IBarSeries
;
22 import org
.swtchart
.ISeries
;
25 * Tool tip provider for density viewer. It displays the x and y value of the
26 * current mouse position.
28 * @author Bernd Hufmann
29 * @author Marc-Andre Laperle
31 public class SimpleTooltipProvider
extends BaseMouseProvider
implements MouseTrackListener
{
33 private static final Format FORMAT
= new SubSecondTimeWithUnitFormat();
36 * Constructor for a tool tip provider.
38 * @param densityViewer
39 * The parent density viewer
41 public SimpleTooltipProvider(AbstractSegmentStoreDensityViewer densityViewer
) {
47 public final void register() {
48 getChart().getPlotArea().addMouseTrackListener(this);
52 public final void deregister() {
53 if (!getChart().isDisposed()) {
54 getChart().getPlotArea().removeMouseTrackListener(this);
59 public void mouseEnter(@Nullable MouseEvent e
) {
63 public void mouseExit(@Nullable MouseEvent e
) {
67 public void mouseHover(@Nullable MouseEvent e
) {
68 if (e
== null || getChart().getAxisSet().getXAxes().length
== 0 || getChart().getAxisSet().getYAxes().length
== 0 || getDensityViewer().getControl().getSeriesSet().getSeries().length
== 0) {
71 ISeries series
= getDensityViewer().getControl().getSeriesSet().getSeries()[0];
72 getChart().getPlotArea().setToolTipText(null);
73 if (series
instanceof IBarSeries
) {
74 IBarSeries barSeries
= (IBarSeries
) series
;
75 // Note: getBounds is broken in SWTChart 0.9.0
76 Rectangle
[] bounds
= barSeries
.getBounds();
78 if (barSeries
.getXSeries().length
< 2) {
81 double delta
= barSeries
.getXSeries()[1] - barSeries
.getXSeries()[0];
82 for (int i
= 0; i
< bounds
.length
; i
++) {
83 Rectangle rec
= bounds
[i
];
88 int end
= start
+ rec
.width
;
89 if (e
.x
>= start
&& e
.x
<= end
) {
90 long x1
= (long) barSeries
.getXSeries()[i
];
91 long x2
= (long) (x1
+ delta
);
92 IAxis yAxis
= getChart().getAxisSet().getYAxes()[0];
93 long y
= Math
.round(yAxis
.getDataCoordinate(rec
.y
)) - 1;
95 String toolTipText
= MessageFormat
.format(Messages
.SimpleTooltipProvider_toolTipText
, FORMAT
.format(x1
), FORMAT
.format(x2
), y
);
96 getChart().getPlotArea().setToolTipText(toolTipText
);