为什么在使用 ColumnViewerToolTipSupport 时无法修复 shell 大小?

Why shell size cannot be fixed while using ColumnViewerToolTipSupport?

我需要为 TreeViewer.ColumnViewerToolTipSupport 实现工具提示帮助我提供自定义工具提示组合。但这里的问题是 父 shell 的设置大小对 shell 大小 没有影响。 看看这里的代码:

 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;
    }
}

这里parent.setSize(450,300)没有任何影响。当在父 class(ToolTip.java) 中检索 shell 大小时,将返回一个不同的值,从而显示一个巨大的 shell。

我该如何解决??

调用createViewerToolTipContentArea的代码在调用此方法后调用了shellpack()方法。 pack() 根据正在使用的布局计算 shell 的大小并调用 setSize。因此它会覆盖您的 setSize 呼叫。

由于您使用的是 GridLayout,您可以尝试使用 GridData widthHintheightHint 字段来为您的合成建议尺寸。

执行此操作的代码是私有 toolTipShow 方法中的 org.eclipse.jface.window.ToolTip。这不容易被覆盖。