tmf.ui: make XY line chart tooltips use same format as chart

Introduce TooltipString#fromDecimal.

Change-Id: Ia40111736ebcf67e6fc65e5970299a9c1c56cc46
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/143090
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: CI Bot
Reviewed-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
Tested-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/TmfAbstractToolTipHandler.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/TmfAbstractToolTipHandler.java
index d892b97..1a76847 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/TmfAbstractToolTipHandler.java
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/TmfAbstractToolTipHandler.java
@@ -9,6 +9,9 @@
 
 package org.eclipse.tracecompass.tmf.ui.viewers;
 
+import java.text.Format;
+import java.text.NumberFormat;
+import java.util.Locale;
 import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Set;
@@ -40,6 +43,7 @@
 import org.eclipse.swt.widgets.Listener;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Slider;
+import org.eclipse.tracecompass.common.core.format.DecimalUnitFormat;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
 import org.eclipse.tracecompass.internal.tmf.ui.ITmfUIPreferences;
 import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
@@ -59,6 +63,8 @@
  */
 public abstract class TmfAbstractToolTipHandler {
 
+    private static Format sNumberFormat = NumberFormat.getNumberInstance(Locale.getDefault());
+
     /**
      * String used for tool tip category, name or value
      *
@@ -114,6 +120,26 @@
         }
 
         /**
+         * Creates a tool tip string from a decimal number. The HTML string mirror the string value.
+         *
+         * @param decimal
+         *            The number to format
+         * @return the tool tip string
+         */
+        public static ToolTipString fromDecimal(Number decimal) {
+            Format format = sNumberFormat;
+            if (format == null) {
+                format = NumberFormat.getInstance(Locale.getDefault());
+                if (format == null) {
+                    format = new DecimalUnitFormat();
+                }
+                sNumberFormat = format;
+            }
+            String number = format.format(decimal);
+            return new ToolTipString(number, toHtmlString(number));
+        }
+
+        /**
          * Creates a tool tip string from a timestamp. The HTML string will
          * contain an hyperlink to the timestamp.
          *
diff --git a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/xycharts/linecharts/TmfCommonXLineChartTooltipProvider.java b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/xycharts/linecharts/TmfCommonXLineChartTooltipProvider.java
index 65b629c..68e81fa 100644
--- a/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/xycharts/linecharts/TmfCommonXLineChartTooltipProvider.java
+++ b/tmf/org.eclipse.tracecompass.tmf.ui/src/org/eclipse/tracecompass/tmf/ui/viewers/xycharts/linecharts/TmfCommonXLineChartTooltipProvider.java
@@ -12,6 +12,7 @@
 
 package org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts;
 
+import java.text.Format;
 import java.util.Arrays;
 
 import org.eclipse.swt.events.MouseEvent;
@@ -43,6 +44,11 @@
     private final class XYToolTipHandler extends TmfAbstractToolTipHandler {
         private static final String HTML_COLOR_TOOLTIP = "<span style=\"color:%s;\">%s</span>"; //$NON-NLS-1$
 
+        private boolean isValid(int index, ISeries serie) {
+            double[] ySeries = serie.getYSeries();
+            return serie.isVisible() && ySeries != null && ySeries.length > index;
+        }
+
         @Override
         public void fill(Control control, MouseEvent event, Point pt) {
             if (getChartViewer().getWindowDuration() != 0) {
@@ -74,26 +80,31 @@
                 }
 
                 TmfCommonXAxisChartViewer viewer = null;
+                Format format = null;
                 ITmfChartTimeProvider timeProvider = getChartViewer();
                 if (timeProvider instanceof TmfCommonXAxisChartViewer) {
                     viewer = (TmfCommonXAxisChartViewer) timeProvider;
+                    format = viewer.getSwtChart().getAxisSet().getYAxes()[0].getTick().getFormat();
                 }
                 ITmfTimestamp time = TmfTimestamp.fromNanos((long) xCoordinate + getChartViewer().getTimeOffset());
                 addItem(null, ToolTipString.fromString(Messages.TmfCommonXLineChartTooltipProvider_time), ToolTipString.fromTimestamp(time.toString(), time.toNanos()));
-
                 /* For each series, get the value at the index */
                 for (ISeries serie : series) {
                     double[] yS = serie.getYSeries();
-                    /*
-                     * Make sure the series values and the value at index exist
-                     */
+                    /* Make sure the series values and the value at index exist */
                     if (isValid(index, serie)) {
                         String key = serie.getId();
                         if (key != null && viewer != null) {
                             IYAppearance appearance = viewer.getSeriesAppearance(key);
                             key = String.format(HTML_COLOR_TOOLTIP, appearance.getColor(), key);
+
                         }
-                        addItem(null, ToolTipString.fromHtml(key), ToolTipString.fromString(String.format("%12.2f", yS[index]))); //$NON-NLS-1$
+                        double yValue = yS[index];
+                        if (format == null) {
+                            addItem(null, ToolTipString.fromHtml(key), ToolTipString.fromDecimal(yValue));
+                        } else {
+                            addItem(null, ToolTipString.fromHtml(key), ToolTipString.fromString(format.format(yValue)));
+                        }
                     }
                 }
             }
@@ -138,22 +149,30 @@
     // MouseTrackListener
     // ------------------------------------------------------------------------
 
+    /**
+     * @deprecated, do not extend, use as-is
+     */
+    @Deprecated
     @Override
     public void mouseEnter(MouseEvent e) {
+        // do nothing
     }
 
+    /**
+     * @deprecated, do not extend, use as-is
+     */
+    @Deprecated
     @Override
     public void mouseExit(MouseEvent e) {
+        // do nothing
     }
 
+    /**
+     * @deprecated, do not extend, use as-is
+     */
+    @Deprecated
     @Override
     public void mouseHover(MouseEvent e) {
-
+        // do nothing
     }
-
-    private static boolean isValid(int index, ISeries serie) {
-        double[] ySeries = serie.getYSeries();
-        return serie.isVisible() && ySeries != null && ySeries.length > index;
-    }
-
 }