8.1 概述
这个功能简单的说,就是将选中的节点与对应的编辑器关联起来,可以通过选择节点关联对应的编辑器,反之亦然。
8.2 扩展点
扩展点org.eclipse.ui.navigator.linkHelper,允许用户自己设计节点与编辑器中间的关联关系。
这个扩展点比较简单,需要提供实现类和ID,另外需要指定一些条件:
- selectionEnablement:何时启用选中节点向编辑器的关联;
- editorInputEnablement:何时启用编辑器向节点的关联;
- class:实现类,实现接口org.eclipse.ui.navigator.ILinkHelper
- id;唯一的标记符。
定义好的Linker,通过viewerContentBinding引用,与filter类似。
8.3 Eclipse实现
Eclipse的Project Explorer提供了关联操作,单击树形结构的节点时,会自动将对应的编辑器设计当前的激活编辑器,而选中编辑器时,会自动选中树形结构的对应节点。
实现代码:
<extension point="org.eclipse.ui.navigator.linkHelper"> <linkHelper class="org.eclipse.ui.internal.navigator.resources.workbench.ResourceLinkHelper" id="org.eclipse.ui.navigator.resources.linkHelper"> <selectionEnablement> <instanceof value="org.eclipse.core.resources.IFile"/> </selectionEnablement> <editorInputEnablement> <instanceof value="org.eclipse.ui.IFileEditorInput"/> </editorInputEnablement> </linkHelper> </extension>
类的实现也简单:
public class ResourceLinkHelper implements ILinkHelper {
/* (non-Javadoc)
* @see org.eclipse.ui.navigator.ILinkHelper#findSelection(org.eclipse.ui.IEditorInput)
*/
public IStructuredSelection findSelection(IEditorInput anInput) {
IFile file = ResourceUtil.getFile(anInput);
if (file != null) {
return new StructuredSelection(file);
}
return StructuredSelection.EMPTY;
}
/* (non-Javadoc)
* @see org.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui.IWorkbenchPage, org.eclipse.jface.viewers.IStructuredSelection)
*/
public void activateEditor(IWorkbenchPage aPage,
IStructuredSelection aSelection) {
if (aSelection == null || aSelection.isEmpty())
return;
if (aSelection.getFirstElement() instanceof IFile) {
IEditorInput fileInput = new FileEditorInput((IFile) aSelection.getFirstElement());
IEditorPart editor = null;
if ((editor = aPage.findEditor(fileInput)) != null)
aPage.bringToTop(editor);
}
}
}
方法findSelection()进行设置节点选中状态,而activateEditor则进行编辑器激活操作。
8.4 说明