站点接口

部件的构造函数中的自变量称为依赖项。部件依赖项替换不同的适配器和 get 方法,例如,要访问站点的 IActionBars,基于组件的部件将在其构造函数中获取 IActionBars 实例。

新型站点提供一组可随意更改的接口。任何插件都可以通过使用 org.eclipse.core.component.types 扩展点来扩展站点接口集。尽管站点接口集可以扩展,但所有站点都支持相同的集合,以确保可以将任何部件插入任何站点。可以通过使用 PDE 插件注册表视图来查看完整的站点接口集。

工作台提供下列站点接口:

接口
描述
IErrorContext
构造 IStatus 消息并将它记录到插件日志。
Bundle
包含部件的实现的插件 bundle。它自动向其它组件(如 IErrorContext 的实现)报告它们的插件。
IContainer
站点的容器。可以查询此对象以获取其它任何站点接口,如果部件想要多路复用所有各项或将它们从其站点重定向至其子代,则此项很有用。
INameable
允许部件设置其名称、图标、工具提示和内容描述。替换 IWorkbenchPart 上的各种 get 方法和侦听器。
组合体
部件的父组合体。未与任何其它部件共享该组合体,并且将与部件同时进行除去。允许部件设置布局并将侦听器连接至此组合体。
ISecondaryId
一种接口,当用作多实例视图时返回部件的辅助标识。
ResourceManager
安全地分配和取消分配图像、字体、颜色和其它 SWT 资源。确保在部件之间共享相同的资源,并确保在关闭部件时清除任何泄露。
IDirtyHandler
允许部件设置或清除其脏状态。
IMessageDialogs
对用户显示错误、警告和信息对话框。
IActionBars
与 Eclipse 3.0 API 配合使用时,与 getViewSite().getActionBars() 的作用相同。
IMultiplexer
为多路复用的组件提供对多路复用器和任何共享接口的访问权。
ISavedState
保存部件的先前持续的状态。
IPartFactory
允许部件创建嵌套的视图和编辑器。
IPartDescriptor
保存关于部件的元数据信息,如部件的标识、标题和缺省图像等。
IEditorInput
保存编辑器的编辑器输入。指向视图的空的编辑器输入。
ISelectionHandler
处理选择更改。部件可以使用此项来更改它们提供给工作台的选择。

由部件包含的上下文确定部件是获取每个接口的唯一实例还是获取在几个部件之间共享的对象。部件的构造函数从不会接收空的自变量。

 

对现有部件使用新的站点接口

尽管构造函数注入是便利的,但为了使用构造函数注入以便利用新的站点接口而重新编写每个现有编辑器和视图,是不切实际的。因此,所有新接口也可作为 IWorkbenchPartSite 上的适配器用于现有视图。

以下是一个视图,它包含一个用于打开消息对话框的按钮。

DependenciesViewOld 的图像

以下示例显示新型视图的源代码,该视图通过使用新的 IMessageDialogs 接口来打开对话框。

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

此示例显示一个传统视图,该视图通过使用 IMessageDialogs 来打开对话框,并演示了也可以使用传统工作台部件来执行此操作。红色字体的行显示在各种情况下如何初始化和使用 IMessageDialogs 接口。

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