Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make part of a TreeViewer cell bold?

Tags:

java

swt

jface

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?

like image 443
manuel Avatar asked Nov 24 '13 03:11

manuel


2 Answers

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

like image 75
Campa Avatar answered Nov 15 '22 00:11

Campa


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.

like image 30
greg-449 Avatar answered Nov 15 '22 01:11

greg-449