How do I make part of a TreeViewer cell bold?
Asked Answered
M

2

6

I currently want to write an Eclipse editor based on a JFace TreeViewer. I added a CellLabelProvider to the TreeViewer. If I set the font of a cell with directly in the update method of the CellLabelProvider to a bold font the label is shown bold.

But I want only part of the label to be shown as bold. So I apply StyleRanges to the cell. Selected colors in the 'StyleRange's work perfectly, but setting the font of a StyleRange to a bold one, does not seem to work.

Why is that and how do I fix it?

Martymartyn answered 24/11, 2013 at 3:56 Comment(0)
T
2

You probably need to use a label provider based on StyledCellLabelProvider.

DelegatingStyledCellLabelProvider is easiest to use because you only need to provide a label provider implementing DelegatingStyledCellLabelProvider.IStyledLabelProvider. The key method in this is

public StyledString getStyledText(Object element);

which lets you just return a StyledString for each object.

Trellas answered 24/11, 2013 at 9:43 Comment(1)
Hey greg, can you be more specific? I'm meeting the same problem to bold a TreeViewer's node https://mcmap.net/q/1773095/-is-it-possible-to-make-jface-treeviewer-specific-node-support-bold-font-for-its-labelprovider/1299675?sem=2Reidreidar
B
8

As specified by greg-449, essentially you could create your Font and set it as bold:

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

Then you can apply that to your StyledString inside #getStyledText().

I am not sure this is portable, I just tested here on my Linux machine.

A full example (just attach it to a 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

Bahena answered 20/5, 2015 at 17:20 Comment(3)
This answer works fine and provides a good implementation of the solution. Consider follow this idea.Insert
To create the Bold font you can simply use JFaceResources.getFontRegistry().getBold("a symbolic name"). At least for nodes of a tree the result is better (the font is bold and it has the same size as the other fonts in the tree).Hilel
Can I have both bold and italic font at the same time using JFaceResources?Ow
T
2

You probably need to use a label provider based on StyledCellLabelProvider.

DelegatingStyledCellLabelProvider is easiest to use because you only need to provide a label provider implementing DelegatingStyledCellLabelProvider.IStyledLabelProvider. The key method in this is

public StyledString getStyledText(Object element);

which lets you just return a StyledString for each object.

Trellas answered 24/11, 2013 at 9:43 Comment(1)
Hey greg, can you be more specific? I'm meeting the same problem to bold a TreeViewer's node https://mcmap.net/q/1773095/-is-it-possible-to-make-jface-treeviewer-specific-node-support-bold-font-for-its-labelprovider/1299675?sem=2Reidreidar

© 2022 - 2024 — McMap. All rights reserved.