The IStyledLabelProvider
is used to style the representation of the tree viewer, as used by the Java outline viewer for displaying the return type of the method, and by the team's decorator when showing when changes have occurred.
IStyledLabelProvider
interface to the TimeZoneLabelProvider
, and create the getStyledText()
method. If the selected element is a Map.Entry
that contains a TimeZone
, add the offset afterwards in brackets.public class TimeZoneLabelProvider extends LabelProvider implements IStyledLabelProvider { public StyledString getStyledText(Object element) { String text = getText(element); StyledString ss = new StyledString(text); if (element instanceof TimeZone) { int offset = -((TimeZone) element).getOffset(0); ss.append(" (" + offset / 3600000 + "h)", StyledString.DECORATIONS_STYLER); } return ss; } }
DelegatingStyledCellLabelProvider
. Modify the constructor, called from the createPartControl()
method of TimeZoneTreeView
.treeViewer.setLabelProvider( new DelegatingStyledCellLabelProvider( new TimeZoneLabelProvider(ir)));
Font
used by the view, the TimeZoneLabelProvider
needs to implement the IFontProvider
interface. JFace's FontRegistry
can be used to return an italicized version of the default font. Add a FontRegistry
parameter to the TimeZoneLabelProvider
constructor, and implement the getFont()
method as follows:public class TimeZoneLabelProvider extends LabelProvider implements IStyledLabelProvider, IFontProvider { private final FontRegistry fr; public TimeZoneLabelProvider(ImageRegistry ir, FontRegistry fr){ this.ir = ir; this.fr = fr; } public Font getFont(Object element) { Font italic = fr.getItalic(JFaceResources.DEFAULT_FONT); return italic; } }
TimeZoneTreeView
to instantiate and pass in the global FontRegistry
from the JFaceResources
class.FontRegistry fr = JFaceResources.getFontRegistry(); //The following commented code needs to be removed /*treeViewer.setLabelProvider( new DelegatingStyledCellLabelProvider( new TimeZoneLabelProvider(ir)));*/ treeViewer.setLabelProvider( new DelegatingStyledCellLabelProvider( new TimeZoneLabelProvider(ir, fr)));
By implementing the IStyledLabelProvider
and wrapping it with a DelegatingStyledCellLabelProvider
, the style of the individual elements in the tree can be controlled, including any additions or style/colors of the item. The StyledText
can render the string in different styles.
Although the DecorationsStyler
was used here, additional stylers can be defined with StyledString.createColorRegistryStyler("foreground", "background")
, where the two strings are keys in the global JFace ColorRegistry
.
Although Colours
can be changed on a character-by-character basis, the Font
is global for the string. That's because when the label is calculated, its size is calculated based on the assumption that the string is displayed in a single Font
.
It's generally good programming practice to have the content or label providers use resource managers that are passed in at construction time. That way, the code can be tested using automated tests or other mock resources. Whether using the Eclipse 3.x or the Eclipse 4.x programming model, decoupling where the resources come from is key to testing.
Q1. What methods are present on LabelProvider
?
Q2. What is the difference between hasChildren()
and getChildren()
on the ContentProvider
?
Q3. What is an ImageRegistry
used for?
Q4. How do you style entries in a TreeView
?
Now that the basics have been covered, try extending the example as follows:
TimeZoneLabelProvider
so that it shows hours/minutes offset from GMT