5.1.6. ListViewer classThe ListViewer class wraps the List widget and is used to view a collection of objects rather than a flat collection of strings. A list viewer needs to be configured with label and content providers. Useful APIs include:
The Person domain model class for the next few examples looks like the following. public class Person { public String firstName = "John"; public String lastName = "Doe"; public int age = 37; public Person[] children = new Person[0]; public Person parent = null; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public Person(String firstName, String lastName, int age, Person[] children) { this(firstName, lastName, age); this.children = children; for (int i = 0; i < children.length; i++) { children[i].parent = this; } } public static Person[] example() { return new Person[] { new Person("Dan", "Rubel", 41, new Person[] { new Person("Beth", "Rubel", 11), new Person("David", "Rubel", 6)}), new Person("Eric", "Clayberg", 42, new Person[] { new Person("Lauren", "Clayberg", 9), new Person("Lee", "Clayberg", 7)}), new Person("Mike", "Taylor", 55) }; } public String toString() { return firstName + " " + lastName; } } The example code that follows creates a list viewer with a label provider, content provider, and viewer sorter (see Figure 5-8). Note: To run the JFace demos standalone, you need to add the following four entries to your Java Build Path (plug-in version numbers should match those used in your Eclipse installation). ECLIPSE_HOME/plugins/org.eclipse.core.runtime_3.1.2.jar ECLIPSE_HOME/plugins/org.eclipse.jface_3.1.1.jar ECLIPSE_HOME/plugins/org.eclipse.jface.text_3.1.2.jar ECLIPSE_HOME/plugins/org.eclipse.text_3.1.1.jar import org.eclipse.jface.viewers.*; import org.eclipse.swt.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class ListViewerExample { public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setText("List Viewer Example"); shell.setBounds(100, 100, 200, 100); shell.setLayout(new FillLayout()); final ListViewer listViewer = new ListViewer(shell, SWT.SINGLE); listViewer.setLabelProvider( new PersonListLabelProvider()); listViewer.setContentProvider( new ArrayContentProvider()); listViewer.setInput(Person.example()); listViewer.setSorter(new ViewerSorter() { public int compare( Viewer viewer, Object p1, Object p2) { return ((Person) p1).lastName .compareToIgnoreCase(((Person) p2).lastName); } }); listViewer.addSelectionChangedListener( new ISelectionChangedListener() { public void selectionChanged( SelectionChangedEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); System.out.println("Selected: " + selection.getFirstElement()); } }); listViewer.addDoubleClickListener( new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { IStructuredSelection selection = (IStructuredSelection) event.getSelection(); System.out.println("Double Clicked: " + selection.getFirstElement()); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } } Figure 5-8. ListViewer example.
After the list viewer has been created, the label provider is set by using the setLabelProvider() method and the content provider is set with the setContentProvider() method. PersonListLabelProvider, the label provider, returns a text label composed of the person's first and last names and does not return an icon. The class looks like this: public class PersonListLabelProvider extends LabelProvider { public Image getImage(Object element) { return null; } public String getText(Object element) { Person person = (Person) element; return person.firstName + " " + person.lastName; } } For the content provider, use the built-in ArrayContentProvider class that maps an input collection to an array. The input object is set using the setInput() method. The viewer sorter defines a custom compare() method that sorts the elements based on a person's last name. Finally, a selectionChanged listener and a doubleClick listener are added that override the selectionChanged() method and the doubleClick() method, respectively. |