workListReg = WorkListPlugin.getDefault().getWorkListRegistry();
viewer.setInput(workListReg);
initListeners(viewer);
return viewer;
}
这样,就可以实现显示了。
那么,怎样实现选择某个扩展后,通过双击执行其功能呢?我们对TreeViewer添加了鼠标双击事件支持,关键代码如下:
protected void handleDoubleClick(DoubleClickEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
Object element = selection.getFirstElement();
TreeViewer viewer = getWorkListViewer();
if (viewer.isExpandable(element)) {
viewer.setExpandedState(element, !viewer.getExpandedState(element));
}else {
WorkListDescriptor workList = (WorkListDescriptor)element;
try {
IWorkListPart workListPart = (IWorkListPart) workList.createWorkList();
workListPart.run();
} catch (CoreException e) {
// should add something to handle the exception
}
}
}
其中IWorkListPart很简单,使所有实现workList扩展必须实现的接口:
public interface IWorkListPart {
public void run();
}
只有一个run方法(可以自行添加其他的支持)。
其中WorkListDescriptor.createWorkList方法实现根据class的字符串创建一个对象,也是超级简单,因为Eclipse已经为我们编好了:
public Object createWorkList() throws CoreException {
Object obj = WorkbenchPlugin.createExtension(configElement, "class");
return obj;
}
这样就可以执行扩展的功能了。
但是别忘了,还要编写pluin.xml,否则Eclipse可不认吆:
<extension
point="net.softapp.worklist.workList">
<category
name="HelloTest"
id="HelloTest"/>
<worklist
icon="icons/example.ico"
class="net.softapp.internal.worklist.Hello"
category="HelloTest"
name="Hello"
id="net.softapp.internal.worklist.Hello"/>
</extension>
4.测试新扩展点
OK,开始运行Eclipse的plugin调试环境,打开WorkList视图,看看在树状列表里是不是有一个HelloTest目录,下面有Hello。双击它,你编写的代码出来了吧!






