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