javatreeviewtooltipeclipse-rcpjface

Jface Tooltip in a Jface TreeViewer disappears too quickly, how can I make it stay visible longer?


I'm working on a Java project using the Eclipse Jface librairies, and my goal is to display a custom tooltip when the user hovers over an item in a Jface TreeViewer. However, I'm encountering an issue where the tooltip disappears too quickly. I would like it to remain visible longer when the user hovers over an item in the tree.

My code is stable in windows but not in Linux i think this because of the gtk i'm currently in RH8 but previously in Centos7. Important things previously we were using an old version of jface 3.8.0 but we have done a migration in Java 11 so we cannot longer use it. Please avoid solution as rollback the dependencies version i cannot.

What i already done is rollback into different version of jface, i have tried from 3.27 to 3.15 still have the problem. I tried different listener as well as MouseEnter or MouseMove but with no success. I already try to set up a big hidelay but it has made no change

Here's an except of my code where I try to create a TreeViewer with a basic tooltip:

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.window.ToolTip;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class BasicToolTipTreeViewerExample {

    private Object hoverEvent = null;

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("TreeViewer avec ToolTip de Base");
        shell.setSize(600, 400);
        shell.setLayout(new FillLayout());

        new BasicToolTipTreeViewerExample().createUI(shell);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    private void createUI(Composite parent) {
        TreeViewer viewer = new TreeViewer(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        viewer.getTree().setHeaderVisible(true);

        TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
        column.getColumn().setText("Nom de l'Événement");
        column.getColumn().setWidth(300);

        TreeItem rootItem = new TreeItem(viewer.getTree(), SWT.NONE);
        rootItem.setText("Événements Principaux");
        rootItem.setExpanded(true);

        TreeItem eventItem1 = new TreeItem(rootItem, SWT.NONE);
        eventItem1.setText("Événement 1");
        eventItem1.setData("Détails de l'Événement 1");

        TreeItem eventItem2 = new TreeItem(rootItem, SWT.NONE);
        eventItem2.setText("Événement 2");
        eventItem2.setData("Détails de l'Événement 2");

        TreeItem eventItem3 = new TreeItem(rootItem, SWT.NONE);
        eventItem3.setText("Événement 3");
        eventItem3.setData("Détails de l'Événement 3");

        createBasicToolTip(viewer);
    }

    private void createBasicToolTip(TreeViewer viewer) {
        ToolTip tooltip = new ToolTip(viewer.getTree()) {
            @Override
            protected Composite createToolTipContentArea(Event event, Composite parent) {
                Composite composite = new Composite(parent, SWT.NONE);
                composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
                composite.setLayout(new FillLayout());

                if (hoverEvent != null) {
                    String text = (String) hoverEvent;
                    if (text != null) {
                        Label label = new Label(composite, SWT.NONE);
                        label.setText(text);
                        label.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
                    }
                }

                return composite;
            }
        };

        tooltip.setPopupDelay(500);
        tooltip.setHideDelay(5000);
        tooltip.setShift(new Point(-5, -5));

        viewer.getTree().addListener(SWT.MouseHover, event -> {
            Point point = new Point(event.x, event.y);
            TreeItem item = viewer.getTree().getItem(point);

            if (item == null || item.getData() == null) {
                hoverEvent = null;
                tooltip.hide();
            } else {
                hoverEvent = item.getData();
                tooltip.show(new Point(event.x, event.y));
            }
        });
    }
}

Is there a way to make the tooltip stay visible longer or prevent it from disappearing ? Are there any additional parameters I should adjust, or a different approach to display more persistent tooltips in a TreeViewer in Jface ?


Solution

  • The problem was in fact the position of the tooltip set in the function

    tooltip.setShift(new Point(-5, -5));
    

    with those parameters the tooltip will pop on the mouse, that will create a collision with the mousehover listener because the the parent's widget (TreeViewer) is not under the mouse.

    I have changed the setShift to this :

    tooltip.setShift(new Point(-5, 15));