实例化部件

所有与部件的交互都是通过 ISite 来完成的。从创建部件的人员的角度来看,ISite 就是该部件。可以从 IPartFactory 创建 ISite,也可以通过直接实例化 Site 类来创建 ISite。

要破坏部件,应除去其 ISite 的控件。

使用 IPartFactory 实例化部件

视图和编辑器通常是使用 IPartFactory 创建的,并且 IPartFactory 只能创建向视图或编辑器扩展点注册的部件。由 IPartFactory 创建的部件依赖于 IWorkbenchPage,并且生命周期不能超过 IWorkbenchPage。IPartFactory 按标识创建部件,并且部件的实现类不必是 API。可以从 IWorkbenchPage 中获取 IPartFactory。部件也可以在其构造函数中获取 IPartFactory 来创建嵌套子代。

此示例演示如何使用 IPartFactory 创建视图。

/**
 * Used to create instances of editors and views.
 *
 * @since 3.1
 */
public interface IPartFactory {
    /**
     * Creates an instance of a view. Returns an <code>ISite</code> for the newly created view.
     * When the caller is done with the part it should dispose the part's main control. This can
     * be accomplished by calling <code>ISite.getControl().dispose()</code>, or by disposing the
     * parent composite.
     *
     * @param viewId ID of the view, as registered with the org.eclipse.ui.views extension point
     * @param parentComposite parent composite for the view. If the view is successfully created, it
     *        will create exactly one new child control in this composite.
     * @param context local context for the view. This object can override any or all of the view's dependencies.
     *        If the view has a dependency that isn't found in the local context, a default implementation will
     *        be supplied by the org.eclipse.core.component.types extension point.
     * @param savedState previously saved state of the part, or null if none
     * @return an ISite for the newly created view
     * @throws CoreException if unable to create the part
     */
    public ISite createView(String viewId, Composite parentComposite, IContainerContext context, IMemento savedState) throws CoreException;
   
    /**
     * Creates an instance of an editor. Returns an <code>ISite</code> for the newly created editor.
     * When the caller is done with the part it should dispose the part's main control. This can
     * be accomplished by calling <code>ISite.getControl().dispose()</code>, or by disposing the
     * parent composite.
     *
     * @param editorId ID of the editor, as registered with the org.eclipse.ui.editors extension point
     * @param parentComposite parent composite for the editor. If the editor is successfully created,
     *        it will create exactly one new child control in this composite.
     * @param context local context for the editor. This object can override any or all of the part's dependencies.
     *        If the part has a dependency that isn't found in the local context, a default implementation will
     *        be supplied by the org.eclipse.core.component.types extension point.
     * @param input IEditorInput for this editor
     * @param savedState previously saved state for the part, or null if none
     * @return an ISite for the newly created editor
     * @throws CoreException if unable to create the part
     */
    public ISite createEditor(String editorId, Composite parentComposite, IContainerContext context, IEditorInput input, IMemento savedState) throws CoreException;
}


以下是使用 IPartFactory 创建视图的操作示例

/**
 * Demonstrate how to open a view by its ID from an IWorkbenchPage.
 */
public class CreateViewByIdAction implements IWorkbenchWindowActionDelegate {
    private IWorkbenchWindow window;

    public void run(IAction action) {       
        IWorkbenchPage page = window.getActivePage();
       
        if (page == null) {
           
// ...uninteresting error-handling code removed...
            return;
        }
       
        final Shell tempShell = new Shell(window.getShell(), SWT.DIALOG_TRIM | SWT.RESIZE);
       
        tempShell.setLayout(new FillLayout());
        tempShell.setText("Problems");
       
        IPartFactory factory = page.getPartFactory();
        try {
            factory.createView(IPageLayout.ID_PROBLEM_VIEW, tempShell, new ContainerContext(), null);
        } catch (CoreException e) {
           
// ...uninteresting error-handling code removed...
        }
       
        // Open the dialog
        tempShell.open();
        Display d = tempShell.getDisplay();
        while (!tempShell.isDisposed()) {
            if (!d.readAndDispatch())
                d.sleep();
        }
       
    }

    public void init(IWorkbenchWindow window) {
        this.window = window;
    }

    // ...remaining (empty) methods removed...
}

使用 Site 类来实例化部件

并非所有部件都是视图或编辑器。客户机也可以使用部件 API 来创建它们自己的可重用组件。虽然没有将这种部件向视图或编辑器扩展点注册,但它们可以使用视图和编辑器可用的大多数 API。尽管没有任何事情阻止客户机为可重用组件创建它们自己的 API,但使用部件模式将允许它们的组件嵌入支持视图或编辑器嵌入的任何对象中。

使用 Site 类创建的部件具有以下属性: 必须对站点构造函数提供部件的实现类和相关联的插件 bundle。插件 bundle 确定日志错误消息将记录在何处以及部件在它自己的插件中查找资源时应搜索何处。

以下示例演示如何使用 Site 类来直接实例化部件。在此示例中,我们在一个对话框中实例化 NameTestView 部件。尽管 NameTestView 称为视图且可以利用视图 API,但实际上并不必将它向 org.eclipse.ui.views 扩展点注册,除非实际上假定工作台将它用作视图。


/**
 * Demonstrate how to open a part programmatically using the Site class.
 */
public class ProgrammaticViewCreationExampleAction implements IWorkbenchWindowActionDelegate {
    private IWorkbenchWindow window;

    public void run(IAction action) {       
        // Create a shell
        final Shell tempShell = new Shell(window.getShell(), SWT.DIALOG_TRIM | SWT.RESIZE);
        tempShell.setLayout(new FillLayout());
        tempShell.setText("Name test view");
       
        Bundle thisPlugin = ComponentExamplesPlugin.getDefault().getBundle();
       
        try {
            // Instantiate the NameTestView part (this line is the whole point of the example)
            // It demonstrates how the Site class can be used instead of calling NameTestView's constructor.
            Site testPart = new Site(tempShell, new ContainerContext(),
                    thisPlugin,
                    NameTestView.class);           
           
        } catch (CoreException e) {
            // ...uninteresting error-handling code removed...
        }
       
        // Open a modal dialog
        tempShell.open();
        Display d = tempShell.getDisplay();
        while (!tempShell.isDisposed()) {
            if (!d.readAndDispatch())
                d.sleep();
        }
       
    }
\
    public void init(IWorkbenchWindow window) {
        this.window = window;
    }

   
// ...remaining (empty) methods removed...
}