如何将DefaultToolTip设置为固定大小?

时间:2017-10-13 06:46:57

标签: java eclipse swt tooltip rcp

我有一个自定义工具提示类,它扩展了ColumnViewerToolTipSupport。问题是工具提示边界扩展以适应动态内容。我需要实现一个固定大小的工具提示,它不会根据内容大小进行扩展。 怎么办呢?

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

        ScrolledComposite scrolledComposite = new ScrolledComposite(parent,SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
        final Composite comp = new Composite(scrolledComposite, SWT.NONE);
        comp.getShell().setBackgroundMode(SWT.INHERIT_FORCE);
        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));
        scrolledComposite.setContent(comp);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);
        scrolledComposite.setMinSize(comp.computeSize(150, 100));

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

        String lineSep = System.getProperty("line.separator"); 
        Matcher matcher = Pattern.compile("(" + lineSep + ")" + ".*?:").matcher(text); 
        while (matcher.find()) {
            style = new StyleRange();
            style.start = matcher.start();
            style.length = matcher.end() - matcher.start();
            style.fontStyle = SWT.BOLD;
        }
        return scrolledComposite;
    }
}

0 个答案:

没有答案