La parte principale dispone di diverse opzioni per la creazione del contesto:
/**
* View that demonstrates how to create two nested children
* with the default context.
*/
public class DefaultContextView {
public DefaultContextView(Composite parent, IPartFactory
factory) throws CoreException {
// Create a resource navigator
ContainerContext viewContext1
= new ContainerContext();
ISite view1 = factory.createView(
IPageLayout.ID_RES_NAV, parent, viewContext1, null);
// Create property view
ContainerContext viewContext2
= new ContainerContext();
ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET,
parent, viewContext2, null);
parent.setLayout(new FillLayout());
}
}
public class RedirectContextView {
/**
* Component constructor. Do not invoke directly.
*/
public RedirectContextView(Composite parent, IPartFactory
factory, ISelectionHandler selection, IActionBars actionBars) throws CoreException
{
// Create a resource navigator.
Redirect the navigator's selection directly to our parent.
ContainerContext viewContext1
= new ContainerContext()
.addInstance(ISelectionHandler.class,
selection);
ISite view1 = factory.createView(
IPageLayout.ID_RES_NAV,
parent, viewContext1, null);
// Create property view. Allow
the property view to use our action bars directly.
ContainerContext viewContext2
= new ContainerContext()
.addInstance(IActionBars.class,
actionBars);
ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET,
parent, viewContext2, null);
parent.setLayout(new FillLayout());
}
}
public class OverrideInstanceView {
/**
* Component constructor. Do not invoke directly.
*/
public OverrideInstanceView(Composite parent, IPartFactory
factory, final INameable name) throws CoreException {
ContainerContext viewContext1 = new
ContainerContext();
// Add an ISelectionHandler to the
view's context. Whenever the view changes its selection,
// display the number of selected
items in the content description
viewContext1.addInstance(ISelectionHandler.class,
new ISelectionHandler() {
/* (non-Javadoc)
* @see
org.eclipse.ui.part.services.ISelectionHandler#setSelection(org.eclipse.jface.viewers.ISelection)
*/
public void
setSelection(ISelection newSelection) {
if (newSelection instanceof IStructuredSelection) {
IStructuredSelection sel = (IStructuredSelection)newSelection;
int selectionSize = sel.size();
name.setContentDescription(MessageFormat.format("{0} problems selected",
new String[] {Integer.toString(selectionSize)}));
}
}
});
// Create a problem view
ISite view1 = factory.createView(
IPageLayout.ID_PROBLEM_VIEW, parent, viewContext1, null);
// Create property view
ContainerContext viewContext2 = new
ContainerContext();
ISite view2 = factory.createView(IPageLayout.ID_PROP_SHEET,
parent, viewContext2, null);
parent.setLayout(new FillLayout());
}
}