The application you have created already contains a pre-defined view. This is because we used the template "RCP application with a view". In this view a content provider and a label provider are given and assigned to a table viewer. Open now the class View.java and review and the method "createPartControl".
The following will replace these default providers with our own providers.
Create your own Content Provider. First create the package "de.vogella.jface.tableviewer.providers". Create the class "PersonContentProvider" which implement the Interface "org.eclipse.jface.viewers.IStructuredContentProvider". The class "PersonContentProvider" reads the data model and to return the data as an array.
Tip
It is important to note that within the content provider no assumption is made how and which data of the data model is displayed by the view. This is the responsibility of the label provider.
package de.vogella.jface.tableviewer.providers;
import java.util.List;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;
import de.vogella.jface.tableviewer.model.Person;
public class PersonContentProvider implements IStructuredContentProvider {
@Override
public Object[] getElements(Object inputElement) {
@SuppressWarnings("unchecked")
Listpersons = (List ) inputElement;
return persons.toArray();
}
@Override
public void dispose() {
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
The method getElements() will be called by the viewer. The viewer has method setInput(). This input will be provided to the contentProvider as the parameter in method getElements().
We will use icons for the married Status. If the folder "icons" does not exists in your project create it. Download the icons from the link provided at the end of this article and place the two icons in the folder icons.
Tip
You can also return null in the method getColumnImage() of the label provider in case you don't want to use the icons.The class "View" already contains a Label Provider from the example. Currently the toString() method of the object Person is used in this label provider. We will replace this default label provider with our own label providers.
Create class "PersonLabelProvider" implementing the interface org.eclipse.jface.viewers.ITableLabelProvider.
package de.vogella.jface.tableviewer.providers;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import de.vogella.jface.tableviewer.Activator;
import de.vogella.jface.tableviewer.model.Person;
public class PersonLabelProvider extends LabelProvider implements
ITableLabelProvider {
// We use icons
// We use icons
private static final Image CHECKED = Activator.getImageDescriptor(
"icons/checked.gif").createImage();
private static final Image UNCHECKED = Activator.getImageDescriptor(
"icons/unchecked.gif").createImage();
@Override
public Image getColumnImage(Object element, int columnIndex) {
// In case you don't like image just return null here
if (columnIndex == 3) {
if (((Person) element).isMarried()) {
return CHECKED;
} else {
return UNCHECKED;
}
}
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
Person person = (Person) element;
switch (columnIndex) {
case 0:
return person.getFirstName();
case 1:
return person.getLastName();
case 2:
return person.getGender();
case 3:
return String.valueOf(person.isMarried());
default:
throw new RuntimeException("Should not happen");
}
}
}
Important thing to note:
-
getColumnText: will return the value for a certain element, e.g. in our example row and column. May not be null.
-
getColumnImage: will return the image for a certain element and a certain column. May be null
We have to change the class "View.java" to use our content and label providers. We will also delete the internal class ViewLabelProvider and ViewContentProvider which the tempate generated.
package de.vogella.jface.tableviewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.part.ViewPart;
import de.vogella.jface.tableviewer.model.ModelProvider;
import de.vogella.jface.tableviewer.providers.PersonContentProvider;
import de.vogella.jface.tableviewer.providers.PersonLabelProvider;
public class View extends ViewPart {
public static final String ID = "de.vogella.jface.tableviewer.view";
private TableViewer viewer;
public void createPartControl(Composite parent) {
createViewer(parent);
// Get the content for the viewer, setInput will call getElements in the
// contentProvider
viewer.setInput(ModelProvider.getInstance().getPersons());
}
private void createViewer(Composite parent) {
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.FULL_SELECTION);
createColumns(viewer);
viewer.setContentProvider(new PersonContentProvider());
viewer.setLabelProvider(new PersonLabelProvider());
}
// This will create the columns for the table
private void createColumns(TableViewer viewer) {
String[] titles = { "First name", "Last name", "Gender", "Married" };
int[] bounds = { 100, 100, 100, 100 };
for (int i = 0; i < titles.length; i++) {
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
column.getColumn().setText(titles[i]);
column.getColumn().setWidth(bounds[i]);
column.getColumn().setResizable(true);
column.getColumn().setMoveable(true);
}
Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
viewer.getControl().setFocus();
}
}
The method createColumns create the table columns, headers, sets the size of the columns, makes the columns resizable and show the lines in the table.
Run the example. The application should look like this.
0 comments:
Post a Comment