本篇博文接上一篇(让你的视图(ViewPart)支持Show In请求(1))。
本篇要描述的内容为:
当你自己的视图(或编辑器)激活时,显示相应的目标Show In目标视图并为即将show in的目标视图提供数据支持。
1.实现org.eclipse.ui.part.IShowInSource接口,并实现public ShowInContext getShowInContext();方法。
IShowInSourcer接口提供了输入和选中的内容,并把它们作为ShowInContext类的一个实例。
eg(代码摘自AntView):
IShowInSourcer接口提供了输入和选中的内容,并把它们作为ShowInContext类的一个实例。
eg(代码摘自AntView):
public ShowInContext getShowInContext() {
AntElementNode node= getSelectionNode();
if (node != null) {
IFile buildFile= node.getIFile();
if (buildFile != null) {
ISelection selection= new StructuredSelection(buildFile);
return new ShowInContext(null, selection);
}
}
return null;
}
如果未实现IShowInSource接口或getShowInContext返回为null,则会show in可能会
显示为"<No Applicable Views>"。

2.实现IShowInTargetList接口。实现其的getShowInTargetIds方法。getShowInTargetIds方法返回你希望在Navigator|Show In菜单级联中列出的目标。eg:
public String[] getShowInTargetIds() {
return new String[] {IDebugUIConstants.ID_DEBUG_VIEW, JavaUI.ID_PACKAGES};
}
另一个方案是实现视图或编辑器的getAdapter方法(这时候,不需要实现IShowInTargetLis接口)
public Object getAdapter(Class required) {
if (required == IShowInTargetList.class) {
return new IShowInTargetList() {
public String[] getShowInTargetIds() {
return new String[] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV };
}
};
}
return super.getAdapter(required);
}
需要指出的是,此接口并不是强制需要实现的,在上一篇博文中看到,可以在Perspecitve中进行配置。
例如,为Sample View视图的实现效果如下:
