Skip to content

Commit 1a33cc3

Browse files
Optimize code
1 parent c56d7c9 commit 1a33cc3

1 file changed

Lines changed: 81 additions & 51 deletions

File tree

tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/xychart/linechart/TmfCommonXLineChartTooltipProvider.java

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
* SPDX-License-Identifier: EPL-2.0
1010
*
1111
* Contributors:
12-
* Geneviève Bastien - Initial API and implementation
12+
* Geneviève Bastien - Initial API and implementation
1313
*******************************************************************************/
1414

15-
package org.eclipse.tracecompass.tmf.ui.viewers.xychart.linechart;
15+
package com.renesas.debug.epl.tracecompass.memory.transaction.ui;
1616

1717
import java.text.Format;
18+
import java.util.ArrayList;
1819
import java.util.Arrays;
1920
import java.util.List;
2021

@@ -33,16 +34,12 @@
3334
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.ITmfChartTimeProvider;
3435
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.IXYSeries;
3536
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.TmfBaseProvider;
37+
import org.eclipse.tracecompass.tmf.ui.viewers.xychart.linechart.TmfCommonXAxisChartViewer;
3638

3739
/**
38-
* Displays a tooltip on line charts. For each series, it shows the y value at
39-
* the selected x value. This tooltip assumes that all series share a common set
40-
* of X axis values. If the X series is not common, the tooltip text may not be
41-
* accurate.
42-
*
43-
* @since 6.0
40+
* Tooltip provider for line charts with a common X axis.
4441
*/
45-
public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider {
42+
public class TmfCommonXLineChartTooltipProviderClone extends TmfBaseProvider {
4643

4744
private static final String HTML_COLOR_TOOLTIP = "<span style=\"color:%s;\">%s</span>"; //$NON-NLS-1$
4845
private final CommonToolTipHandler fToolTipHandler = new CommonToolTipHandler();
@@ -53,7 +50,7 @@ public class TmfCommonXLineChartTooltipProvider extends TmfBaseProvider {
5350
* @param tmfChartViewer
5451
* The parent chart viewer
5552
*/
56-
public TmfCommonXLineChartTooltipProvider(ITmfChartTimeProvider tmfChartViewer) {
53+
public TmfCommonXLineChartTooltipProviderClone(ITmfChartTimeProvider tmfChartViewer) {
5754
super(tmfChartViewer);
5855
register();
5956
}
@@ -72,62 +69,97 @@ public void refresh() {
7269
// nothing to do
7370
}
7471

75-
protected boolean isTooltipAvailable() {
76-
return getChartViewer().getWindowDuration() != 0;
77-
}
72+
// ======================================================================
73+
// HOOK METHODS
74+
// ======================================================================
7875

79-
protected int getHoveredIndex(List<IXYSeries> series, double xCoordinate) {
80-
if (series.isEmpty()) {
81-
return -1;
82-
}
83-
double[] xSeries = series.get(0).getXSeries();
84-
if ((xSeries == null) || (xSeries.length == 0)) {
85-
return -1;
86-
}
87-
int index = Arrays.binarySearch(xSeries, xCoordinate);
88-
index = (index >= 0) ? index : -index - 1;
89-
return (index < xSeries.length) ? index : -1;
76+
@FunctionalInterface
77+
protected interface ITooltipItemAdder {
78+
void addItem(ToolTipString key, ToolTipString value);
9079
}
9180

92-
protected boolean isValidSeriesIndex(IXYSeries series, int index) {
93-
double[] ySeries = series.getYSeries();
94-
return series.isVisible() && ySeries != null && index >= 0 && index < ySeries.length;
95-
}
96-
97-
protected void addAdditionalTooltipItems(double xCoordinate, String seriesKey) {
98-
ITmfTimestamp time = TmfTimestamp.fromNanos((long) xCoordinate + getChartViewer().getTimeOffset());
99-
addItem(null,
81+
protected void addAdditionalTooltipItems(ITooltipItemAdder adder, double xCoordinate, String key) {
82+
long timeNanos = Math.round(xCoordinate) + getChartViewer().getTimeOffset();
83+
ITmfTimestamp time = TmfTimestamp.fromNanos(timeNanos);
84+
adder.addItem(
10085
ToolTipString.fromString(Messages.TmfCommonXLineChartTooltipProvider_time),
10186
ToolTipString.fromTimestamp(time.toString(), time.toNanos()));
10287
}
10388

104-
protected void addSeriesTooltipItem(IXYSeries series, int index, Format format) {
105-
double[] ySeries = series.getYSeries();
89+
protected void addSeriesTooltipItem(ITooltipItemAdder adder, IXYSeries xySeries, int index, Format format) {
90+
double[] ySeries = xySeries.getYSeries();
10691
if (ySeries == null || index < 0 || index >= ySeries.length) {
10792
return;
10893
}
10994

110-
String label = formatSeriesLabel(series);
95+
String label = formatSeriesLabel(xySeries);
11196
double yValue = ySeries[index];
11297
if (format == null) {
113-
addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromDecimal(yValue));
98+
adder.addItem(
99+
ToolTipString.fromHtml(label),
100+
ToolTipString.fromDecimal(yValue));
114101
} else {
115-
addItem(null, ToolTipString.fromHtml(label), ToolTipString.fromString(format.format(yValue)));
102+
adder.addItem(
103+
ToolTipString.fromHtml(label),
104+
ToolTipString.fromString(format.format(yValue)));
116105
}
117106
}
118107

119-
protected String formatSeriesLabel(IXYSeries series) {
120-
String key = series.getId();
121-
String label = (key == null) ? "" : key; //$NON-NLS-1$
122-
Color color = series.getColor();
108+
// ======================================================================
109+
// PRIVATE METHODS
110+
// ======================================================================
111+
112+
private String formatSeriesLabel(IXYSeries xySeries) {
113+
String label = xySeries.getId();
114+
if (label == null) {
115+
label = ""; //$NON-NLS-1$
116+
}
117+
118+
Color color = xySeries.getColor();
123119
if (color != null) {
124120
RGBA rgba = color.getRGBA();
125121
RGBAColor rgbaColor = new RGBAColor(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha);
126-
label = String.format(TmfCommonXLineChartTooltipProvider.HTML_COLOR_TOOLTIP, rgbaColor, label);
122+
label = String.format(TmfCommonXLineChartTooltipProviderClone.HTML_COLOR_TOOLTIP, rgbaColor, label);
127123
}
124+
128125
return label;
129126
}
130127

128+
private boolean isTooltipAvailable() {
129+
return getChartViewer().getWindowDuration() != 0;
130+
}
131+
132+
private int getHoveredIndex(List<IXYSeries> series, double xCoordinate) {
133+
if (series.isEmpty()) {
134+
return -1;
135+
}
136+
137+
double[] xSeries = series.get(0).getXSeries();
138+
if (xSeries == null || xSeries.length == 0) {
139+
return -1;
140+
}
141+
142+
int index = Arrays.binarySearch(xSeries, xCoordinate);
143+
if (index < 0) {
144+
index = -index - 1;
145+
index = Math.max(0, index - 1);
146+
}
147+
148+
return index < xSeries.length ? index : -1;
149+
}
150+
151+
private boolean isValidSeriesIndex(IXYSeries series, int index) {
152+
double[] ySeries = series.getYSeries();
153+
return series.isVisible()
154+
&& ySeries != null
155+
&& index >= 0
156+
&& index < ySeries.length;
157+
}
158+
159+
// ======================================================================
160+
// TOOLTIP HANDLER
161+
// ======================================================================
162+
131163
private final class CommonToolTipHandler extends TmfAbstractToolTipHandler {
132164

133165
@Override
@@ -153,20 +185,18 @@ public void fill(Control control, MouseEvent event, Point pt) {
153185
format = chartViewer.getSwtChart().getAxisSet().getYAxes()[0].getTick().getFormat();
154186
}
155187

156-
String firstValidSeriesKey = null;
188+
List<IXYSeries> validXYSeries = new ArrayList<>();
157189
for (IXYSeries xySeries : series) {
158190
if (isValidSeriesIndex(xySeries, index)) {
159-
firstValidSeriesKey = xySeries.getId();
160-
break;
191+
validXYSeries.add(xySeries);
161192
}
162193
}
163-
addAdditionalTooltipItems(xCoordinate, firstValidSeriesKey);
164194

165-
for (IXYSeries xySeries : series) {
166-
if (!isValidSeriesIndex(xySeries, index)) {
167-
continue;
168-
}
169-
addSeriesTooltipItem(xySeries, index, format);
195+
String firstValidSeriesKey = validXYSeries.isEmpty() ? null : validXYSeries.get(0).getId();
196+
addAdditionalTooltipItems((key, value) -> addItem(null, key, value), xCoordinate, firstValidSeriesKey);
197+
198+
for (IXYSeries xySeries : validXYSeries) {
199+
addSeriesTooltipItem((key, value) -> addItem(null, key, value), xySeries, index, format);
170200
}
171201
}
172202
}

0 commit comments

Comments
 (0)