Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
FAQ What is a viewer?
The purpose of a viewer is to simplify the interaction between an underlying model and the widgets used to present elements of that model. A viewer is not used as a high-level replacement for an SWT widget but as an adapter that sits beside an SWT widget and automates some of the more mundane widget-manipulation tasks, such as adding and removing items, sorting, filtering, and refreshing.
A viewer is created by first creating an SWT widget, constructing the viewer on that widget, and then setting its content provider, label provider, and input. This snippet is from the BooksView class in the FAQ Examples plug-in, which creates a table viewer to display a library of books:
int style = SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL; Table table = new Table(parent, style); TableViewer viewer = new TableViewer(table); viewer.setContentProvider(new BookshelfContentProvider()); viewer.setLabelProvider(new BookshelfLabelProvider()); viewer.setInput(createBookshelf());
In general, JFace viewers allow you to create a model-view-controller (MVC) architecture. The view is the underlying SWT widget, the model is specified by the framework user, and the JFace viewer and its associated components form the controller. The viewer input is a model element that seeds the population of the viewer.
The JDT Package Explorer uses a TreeViewer to display the contents of a workspace, represented as a JavaModel.
The corresponding Spider diagram (Figure 8.2) shows the relationships among the Package Explorer, its viewer, its model, the underlying widget, and the label and content providers for the JDT Package Explorer.
<img src="../images/ViewerModel.png" width=547> Figure 8.2 Spider diagram of Package Explorer components
- FAQ What are content and label providers?
- FAQ How do I sort the contents of a viewer?
- FAQ How do I filter the contents of a viewer?
- FAQ What is a view?
- FAQ What is the difference between a view and a viewer?
This FAQ was originally published in Official Eclipse 3.0 FAQs. Copyright 2004, Pearson Education, Inc. All rights reserved. This text is made available here under the terms of the Eclipse Public License v1.0.