Search code examples
eclipseswttooltipeclipse-rcpjface

Why shell size cannot be fixed while using ColumnViewerToolTipSupport?


I need to implement a tooltip for TreeViewer.ColumnViewerToolTipSupport helps me with providing custom tooltip composite. But the problem here is setting size of the parent shell does not have an impact on the shell size. Look at the code here :

 private static class MyToolTip extends ColumnViewerToolTipSupport {

    public static final void enableFor(ColumnViewer viewer, int style) {
        new MyToolTip(viewer, style, false);            
    }

    private MyToolTip(ColumnViewer viewer, int style, boolean manualActivation) {
        super(viewer, style, manualActivation);
        setHideOnMouseDown(false);
    }

    @Override
    protected Composite createViewerToolTipContentArea(Event event, ViewerCell cell, Composite parent) {
        String text = getText(event);
        if (text == null || text.isEmpty()) {
            return super.createViewerToolTipContentArea(event, cell, parent);
        }

        parent.setSize(450,300); 
        final Composite comp = new Composite(parent, SWT.NONE);
        GridLayout gridLayout = new GridLayout(1, false);
        gridLayout.horizontalSpacing = 0;
        gridLayout.verticalSpacing = 0;
        gridLayout.marginHeight = 0;
        gridLayout.marginWidth = 0;
        comp.setLayout(gridLayout);
        comp.setLayoutData(new GridData(GridData.FILL_BOTH));

        StyledText styledText = new StyledText(comp, SWT.BORDER);
        styledText.setText(text);

        StyleRange style = new StyleRange();
        style.start = 0;
        style.length = text.indexOf(":");
        style.fontStyle = SWT.BOLD;

        styledText.setStyleRange(style);        
        return comp;
    }
}

Here parent.setSize(450,300) does not have any impact. When shell size is retrieved in parent class(ToolTip.java), a different value is returned altogether, thereby displaying a huge shell.

How do I fix it ??


Solution

  • The code which calls createViewerToolTipContentArea has a call to the shell pack() method after calling this method. pack() calculates the size of the shell based on the layout being used and calls setSize. So it overrides your setSize call.

    Since you are using GridLayout you can try using the GridData widthHint and heightHint fields to suggest sizes for you composite.

    The code that does this is org.eclipse.jface.window.ToolTip in the private toolTipShow method. This can't easily be overridden.