tmf.core: Introduce TmfTimestamp factory methods
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / xycharts / linecharts / TmfCommonXLineChartTooltipProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
3 *
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 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts;
14
15 import java.text.FieldPosition;
16 import java.text.Format;
17 import java.text.ParsePosition;
18
19 import org.eclipse.swt.events.MouseEvent;
20 import org.eclipse.swt.events.MouseTrackListener;
21 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
22 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.ITmfChartTimeProvider;
23 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.TmfBaseProvider;
24 import org.swtchart.IAxis;
25 import org.swtchart.ISeries;
26
27 /**
28 * Displays a tooltip on line charts. For each series, it shows the y value at
29 * the selected x value. This tooltip assumes that all series share a common set
30 * of X axis values. If the X series is not common, the tooltip text may not be
31 * accurate.
32 *
33 * @author Geneviève Bastien
34 */
35 public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider implements MouseTrackListener {
36
37 private static final Format DEFAULT_FORMAT = new Format() {
38
39 /**
40 * Default serial ID
41 */
42 private static final long serialVersionUID = -6130622953193109057L;
43
44 @Override
45 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
46 return toAppendTo.append(obj);
47 }
48
49 @Override
50 public Object parseObject(String source, ParsePosition pos) {
51 return source;
52 }
53
54 };
55
56 /**
57 * Constructor for the tooltip provider
58 *
59 * @param tmfChartViewer
60 * The parent chart viewer
61 */
62 public TmfCommonXLineChartTooltipProvider(ITmfChartTimeProvider tmfChartViewer) {
63 super(tmfChartViewer);
64 register();
65 }
66
67 // ------------------------------------------------------------------------
68 // TmfBaseProvider
69 // ------------------------------------------------------------------------
70
71 @Override
72 public void register() {
73 getChart().getPlotArea().addMouseTrackListener(this);
74 }
75
76 @Override
77 public void deregister() {
78 if ((getChartViewer().getControl() != null) && !getChartViewer().getControl().isDisposed()) {
79 getChart().getPlotArea().removeMouseTrackListener(this);
80 }
81 }
82
83 @Override
84 public void refresh() {
85 // nothing to do
86 }
87
88 // ------------------------------------------------------------------------
89 // MouseTrackListener
90 // ------------------------------------------------------------------------
91
92 @Override
93 public void mouseEnter(MouseEvent e) {
94 }
95
96 @Override
97 public void mouseExit(MouseEvent e) {
98 }
99
100 @Override
101 public void mouseHover(MouseEvent e) {
102 if (getChartViewer().getWindowDuration() != 0) {
103 IAxis xAxis = getChart().getAxisSet().getXAxis(0);
104
105 double xCoordinate = xAxis.getDataCoordinate(e.x);
106
107 ISeries[] series = getChart().getSeriesSet().getSeries();
108
109 if ((xCoordinate < 0) || (series.length == 0)) {
110 return;
111 }
112
113 /* Find the index of the value we want */
114 double[] xS = series[0].getXSeries();
115 if (xS == null) {
116 return;
117 }
118 int index = 0;
119 for (int i = 0; i < xS.length; i++) {
120 if (xS[i] > xCoordinate) {
121 break;
122 }
123 index = i;
124 }
125
126 /* set tooltip of closest data point */
127 Format format = getChart().getAxisSet().getYAxis(0).getTick().getFormat();
128 if (format == null) {
129 format = DEFAULT_FORMAT;
130 }
131 StringBuffer buffer = new StringBuffer();
132 buffer.append("time="); //$NON-NLS-1$
133 buffer.append(TmfTimestamp.fromNanos((long) xCoordinate + getChartViewer().getTimeOffset()).toString());
134 buffer.append('\n');
135
136 /* For each series, get the value at the index */
137 for (ISeries serie : series) {
138 double[] yS = serie.getYSeries();
139 /* Make sure the series values and the value at index exist */
140 if (yS == null || yS.length <= index) {
141 continue;
142 }
143 buffer.append(serie.getId());
144 buffer.append('=');
145 buffer.append(format.format(yS[index]));
146 buffer.append('\n');
147 }
148
149 getChart().getPlotArea().setToolTipText(buffer.toString());
150 getChart().redraw();
151 }
152 }
153
154 }
This page took 0.036441 seconds and 5 git commands to generate.