Site interfaces

The arguments in a part's constructor are known as dependencies. Part dependencies replace the various adapters and get methods, for example, to access a site's IActionBars, a component-based part would take an IActionBars instance in its constructor.

New-style sites offer an open-ended set of interfaces. Any plug-in can extend the set of site interfaces using the org.eclipse.core.component.types extension point. Although the set of site interfaces can be extended, all sites support the same set, ensuring that any part can be plugged into any site. It is possible to view the complete set of site interfaces using the PDE plug-in registry view.

The workbench supplies the following site interfaces:

Interface
Description
IErrorContext
Constructs and logs IStatus messages to the plug-in log.
Bundle
The plug-in bundle containing the part's implementation. This informs other components, such as the implementation of IErrorContext, automatically of their plug-ins.
IContainer
The site's container. This object can be queried for any of the other site interfaces, which can be useful if the part wants to redirect or multiplex everything from its site to its children.
INameable
Allows a part to set its name, icon, tooltip, and content description. Replaces the various get methods and listeners on IWorkbenchPart.
Composite
Parent composite for the part. This composite is not shared with any other parts, and will be disposed at the same time as the part. The part is allowed to set the layout and attach listeners to this composite.
ISecondaryId
Interface that returns a part's secondary ID when used as a multi-instance view.
ResourceManager
Safely allocates and deallocates Images, Fonts, Colors, and other SWT resources. Ensures that identical resources are shared between parts and that any leaks get cleaned up when the part is closed.
IDirtyHandler
Allows parts to set or clear their dirty state.
IMessageDialogs
Displays error, warning, and info dialogs to the user.
IActionBars
Serves the same purpose as getViewSite().getActionBars() did with the Eclipse 3.0 API.
IMultiplexer
Provides a multiplexed component with access to its multiplexer and any shared interfaces
.
ISavedState
Holds the previously-persisted state of the part.
IPartFactory
Allows a part to create nested views and editors.
IPartDescriptor
Holds meta-info about a part such as its ID, title, default image, and so on.
IEditorInput
Holds the editor input for an editor. Points to an empty editor input for views.
ISelectionHandler
Handles selection changes. Parts can use this to change the selection they provide to the workbench.

It is up to the part's containing context to determine whether the part gets a unique instance for each interface or an object shared among several parts. The part's constructor never receives a null argument.

 

Using new site interfaces with existing parts

Although constructor injection is convenient, it would be impractical to rewrite every existing editor and view to use constructor injection in order to take advantage of the new site interfaces. For this reason, all of the new interfaces are also available to existing views as adapters on IWorkbenchPartSite.

Here is a view containing a single button that opens a message dialog.

Image of DependenciesViewOld

The following example shows the source for a new-style view that opens the dialog using the new IMessageDialogs interface.

/**
 * Demonstrates how to use component dependencies in a new-style part.
 *
 * @since 3.1
 */
public class DependenciesViewNew {
    // Dependencies
    private IMessageDialogs dialogs;
   
    /**
     * Component constructor. Do not invoke directly.
     */
    public DependenciesViewNew(Composite parent, IMessageDialogs dialogs) {
        this.dialogs = dialogs;
       
        Button testButton = new Button(parent, SWT.PUSH);
        testButton.setText("Open a dialog");
        testButton.addSelectionListener(new SelectionAdapter() {
        /* (non-Javadoc)
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        public void widgetSelected(SelectionEvent e) {
            openADialog();
        }
        });
    }
   
    private void openADialog() {
        dialogs.open(IStatus.INFO, "This is a message");
    }
}

This example shows a traditional view that opens a dialog using IMessageDialogs demonstartes that this is also possible using a traditional workbench part. The lines in red font show how the IMessageDialogs interface is initialized and used in each case.

/**
 * Demonstrates how to use component dependencies in an old-style view
 *
 * @since 3.1
 */
public class DependenciesViewOld extends ViewPart {
    // Dependencies
    private IMessageDialogs dialogs;
    
    // Main widget
    private Composite parent;
    
    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
     */
    public void createPartControl(Composite parent) {
        this.parent = parent;
        this.dialogs = (IMessageDialogs)getSite().getAdapter(IMessageDialogs.class);
        
        Button testButton = new Button(parent, SWT.PUSH);
        testButton.setText("Open a dialog");
        testButton.addSelectionListener(new SelectionAdapter() {
        /* (non-Javadoc)
         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
         */
        public void widgetSelected(SelectionEvent e) {
            openADialog();
        }
        });
    }
    
    private void openADialog() {
        if (dialogs != null) {
            dialogs.open(IStatus.INFO, "This is a message");
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IWorkbenchPart#setFocus()
     */
    public void setFocus() {
        parent.setFocus();
    }
}