Using JFace with PFGrid.SWT
In this tutorial is shown how to use a JFace-TreeListViewer
of PFGrid to display
hierarchical content in an RCP-Application.
First of all we are using the Eclipse-wizard to create an RCP-Application with UI.
The wizard creates a plugin with a class derived from ViewPart. Remove all code in
this class so that an empty composite will remain.
Add Content- and LabelProvider
When displaying hierarchical content you have to define
a ContentProvider that implements
ITreeContentProvider. In our sample we will display objects of the class
Group, each Group
can contain a list of User-objects (your business classes). Here are the Content-
and LabelProvider-classes:
/** The contentprovider for treelist */
class GroupUserContentProvider implements ITreeContentProvider {
public Object[] getChildren(Object parentElement) {
Group g = (Group) parentElement;
return g.getUsers().toArray();
}
public boolean hasChildren(Object element) {
if (!(element instanceof Group))
return false;
Group g = (Group) element;
return (g.getUsers().size() > 0);
}
public Object[] getElements(Object inputElement) {
return ((ArrayList) inputElement).toArray();
}
}
/** the labelprovider for treelist */
class GroupUserLabelProvider implements ITableLabelProvider {
@Override
public Image getColumnImage(Object element, int columnIndex) {
if (columnIndex == 0) {
IGrantee grantee = (IGrantee) element;
return grantee.getImage();
}
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
IGrantee grantee = (IGrantee) element;
switch (columnIndex) {
case 0:
return grantee.getName();
case 1:
return grantee.getShortName();
case 2:
return (grantee.isActive()) ? "true" : "false";
}
return null;
}
@Override
public boolean isLabelProperty(Object element, String property) {
return false;
}
@Override
public void removeListener(ILabelProviderListener listener) {
}
}
Defining the UI
The UI is created in the method createTreeViewer which is defined in the ViewPart
of the RCP-Application:
private void createTreeViewer(Composite parent, ArrayList groups) {
viewer = new TreeListViewer(parent, SWT.NONE);
// Initialize the underlying widget
final TreeListView treeList = viewer.getTreeListView();
treeList.addTreeListEditorListener(new GranteeEditorListener());
treeList.setSelectionType(SelectionType.Cell);
// Create columns
TreeListViewerColumn viewerColumnName = new TreeListViewerColumn(viewer);
TreeListColumn colName = viewerColumnName.getColumn();
colName.setText("Name");
colName.setWidth(350);
TreeListViewerColumn viewerColumnShortName = new TreeListViewerColumn(
viewer);
TreeListColumn colShortName = viewerColumnShortName.getColumn();
colShortName.setText("Shortname");
colShortName.setWidth(100);
TreeListViewerColumn viewerColumnActive = new TreeListViewerColumn(
viewer, new CheckBoxColumn(treeList));
TreeListColumn colActive = viewerColumnActive.getColumn();
colActive.setText("Active");
colActive.setWidth(50);
// Set Content- and LabelProvider
viewer.setContentProvider(new GroupUserContentProvider());
viewer.setLabelProvider(new GroupUserLabelProvider());
viewer.setInput(groups);
// Initially expand the root nodes
viewer.setExpandedState(groups.get(0), true);
viewer.setExpandedState(groups.get(1), true);
viewer.addDoubleClickListener(this);
}
In this method
the TreeListViewer and the TreeListViewerColumns are defined (the
JFace-representation of the UI) and the content- and labelprovider are assigned.
The root-nodes of the content are expanded.
Add the content
The last thing to do is adding the input-data to the viewer. The method createInputData
delivers a list of Group-objects which are assigend to the viewer in the method
createTreeViewer:
private ArrayList createInputData(Image imgGroup, Image imgUser) {
ArrayList groups = new ArrayList();
// Group of developers
Group developers = new Group(imgGroup, "Developers", "Grp. 1");
developers.getUsers().add(new User(imgUser, "Matthias P.", "mp"));
developers.getUsers().add(new User(imgUser, "Georg S.", "gs"));
// Group of teamleaders
Group teamleaders = new Group(imgGroup, "Team leaders", "Grp. 2");
teamleaders.getUsers().add(new User(imgUser, "Günther F.", "gf"));
teamleaders.getUsers().add(new User(imgUser, "Herbert E.", "he"));
groups.add(developers);
groups.add(teamleaders);
return groups;
}
The column Active represents a boolean-value which is displayed as CheckBox. This
is achieved by the following code:
TreeListViewerColumn viewerColumnActive = new TreeListViewerColumn(
viewer, new CheckBoxColumn(treeList));
That's all - please refer to the sample in the Download-section.