使用ColumnViewerToolTipSupport时无法修复shell大小的原因?

时间:2017-11-09 04:38:46

标签: eclipse swt tooltip eclipse-rcp jface

我需要为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)没有任何影响。在父类(ToolTip.java)中检索shell大小时,将返回一个不同的值,从而显示一个巨大的shell。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

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

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

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