将自变量传递至部件

父代通过使用 ContainerContext 将自变量传递到其子代。上下文可以将自变量传递到部件的构造函数,也可以指定一个工厂来构造这些自变量。例如,特定 ContainerContext 可能指定如下规则:
如果上下文未提供特定依赖项,则子代将使用来自 org.eclipse.core.component.types 扩展点的缺省实现。由于上下文可以覆盖部件在其构造函数中获得的任何自变量,所以父代可以覆盖部件通常从其站点获取的任何接口。例如,父代可以通过在上下文中提供 IActionBars 来强制其子代使用 IActionBars 的不同实现。

父代有几个用于构造上下文的选项:

以下 DefaultContextViewRedirectContextViewOverrideInstanceView 示例演示了每种可能性。

缺省上下文

以下示例显示视图的源代码,该视图在缺省上下文中创建两个嵌套的子代。在缺省上下文中创建子代这一事实意味着父代不关心子代的名称、工具栏和选择等,并且不准备处理它们。如果父代想要进行某些处理,比如说处理它的一个子代中的当前选择,则它需要将 ISelectionHandler 传递至该子代。最终结果显示在该示例的下面。

/**
 * 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());       
    }
}

DefaultContextView 的屏幕快照

将依赖项从父代重定向至子代

此示例演示如何将站点接口从父代重定向至子代。此组合视图包含一个资源导航器和一个属性视图。此视图将其选择处理程序重定向至资源导航器,并将其操作栏重定向至属性视图。结果是一个视图,该视图提供资源选择并包含来自属性视图的工具栏和菜单,如下所示。

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());       
    }
}


RedirectContextView 的屏幕快照

直接提供依赖项

此示例演示如何为子代直接提供它的一个依赖项。在此示例中,我们创建一个包含问题视图和属性视图的组合视图。我们为问题视图提供 ISelectionHandler,以便在内容描述中显示所选问题的数目。最终结果显示在下面。

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());
    }
}



OverrideInstanceView 的屏幕快照