如何使TreeViewer单元格的一部分变为粗体?

时间:2013-11-24 03:56:16

标签: java swt jface

我目前想要编写一个基于JFace TreeViewer的Eclipse编辑器。我在CellLabelProvider添加了TreeViewer。如果我直接在update的{​​{1}}方法中将单元格的字体设置为粗体,则标签会以粗体显示。

但我只想将标签的一部分显示为粗体。所以我将CellLabelProvider s应用于单元格。 “StyleRange”中的所选颜色完美无缺,但将StyleRange的字体设置为粗体,似乎不起作用。

为什么会这样,我该如何解决?

2 个答案:

答案 0 :(得分:7)

根据greg-449的规定,基本上您可以创建Font并将其设置为粗体

  class BoldFontStyler extends Styler {                                                                  
    @Override
    public void applyStyles(final TextStyle textStyle)
    {
        FontDescriptor boldDescriptor = FontDescriptor.createFrom(new FontData()).setStyle(SWT.BOLD);
        Font boldFont = boldDescriptor.createFont(Display.getCurrent());
        textStyle.font = boldFont;
    }
}

然后,您可以将其应用于StyledString内的#getStyledText()

我不确定这是可移植的,我只是在我的Linux机器上测试过。

完整示例(只需将其附加到Part):

package com.ebotlution.c4ids.comms.ui;

import javax.annotation.PostConstruct;

import org.eclipse.jface.resource.FontDescriptor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.StyledString.Styler;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.TreeColumn;

class BoldFontStyler extends Styler {
    @Override
    public void applyStyles(final TextStyle textStyle)
    {
        FontDescriptor boldDescriptor = FontDescriptor.createFrom(new FontData()).setStyle(SWT.BOLD);
        Font boldFont = boldDescriptor.createFont(Display.getCurrent());
        textStyle.font = boldFont;
    }
}

class NormalColumnLabelProvider extends ColumnLabelProvider {
    @Override
    public String getText(Object element) {
        return (element).toString().split(" ")[0];
    }
}

class BoldColumnLabelProvider extends DelegatingStyledCellLabelProvider {
    static final Styler BOLD_FONT_STYLER = new BoldFontStyler();

    public BoldColumnLabelProvider() {
        super(new IStyledLabelProvider() {
            @Override
            public Image getImage(Object element) {
                return null;
            }

            @Override
            public StyledString getStyledText(Object element) {
                return new StyledString((element).toString().split(" ")[1], BOLD_FONT_STYLER);
            }

            @Override
            public void addListener(ILabelProviderListener listener) {}

            @Override
            public void dispose() {}

            @Override
            public boolean isLabelProperty(Object element, String property) {
                return false;
            }

            @Override
            public void removeListener(ILabelProviderListener listener) {}
        });
    }
}

class MyContentProvider implements ITreeContentProvider {

    static final Object[] EMPTY_ARRAY = new Object[0];

    Object[] data;

    @Override
    public void dispose() {}

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        data = (Object[]) newInput;
    }

    @Override
    public Object[] getElements(Object inputElement) {
        return data;
    }

    @Override
    public Object[] getChildren(Object parentElement) {
        return EMPTY_ARRAY;
    }

    @Override
    public Object getParent(Object element) {
        return null;
    }

    @Override
    public boolean hasChildren(Object element) {
        return false;
    }
}

public class BoldColumnTreeViewerEx {

    TreeViewer treeViewer;

    @PostConstruct
    public void createControls(Composite parent) {
        treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
        treeViewer.setUseHashlookup(true);
        treeViewer.setContentProvider(new MyContentProvider());
        // Do all your stuff
        // [...]

        TreeViewerColumn normalCol = createTreeViewerColumn("Normal Column");
        normalCol.setLabelProvider(new NormalColumnLabelProvider());

        TreeViewerColumn boldCol = createTreeViewerColumn("Bold Column");
        boldCol.setLabelProvider(new BoldColumnLabelProvider());

        treeViewer.setInput(new String[] { "Hello Dude", "SWT JFace" });
        treeViewer.expandAll();
    }

    private TreeViewerColumn createTreeViewerColumn(String title) {
        TreeViewerColumn viewerColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
        TreeColumn column = viewerColumn.getColumn();
        column.setText(title);
        column.setWidth(100);
        return viewerColumn;
    }
}

example-screenshor

答案 1 :(得分:2)

您可能需要使用基于StyledCellLabelProvider的标签提供程序。

DelegatingStyledCellLabelProvider最容易使用,因为您只需提供实施DelegatingStyledCellLabelProvider.IStyledLabelProvider的标签提供商。其中的关键方法是

public StyledString getStyledText(Object element);

允许您为每个对象返回StyledString