先看官方的doc说明:
Object org.eclipse.core.runtime.IAdaptable.getAdapter(Class adapter) Returns an object which is an instance of the given class associated with this object. Returns null if no such object can be found. Parameters: adapter the adapter class to look up Returns: a object castable to the given class, or null if this object does not have an adapter for the given class
再看ContentOutline的代码:
//Creates a page for a given part. Adds it to the pagebook but does not show it.
protected PageRec doCreatePage(IWorkbenchPart part) { // Try to get an outline page. Object obj = ViewsPlugin.getAdapter(part, IContentOutlinePage.class, false);
想例如Outline,PropertyView之类的视图都是基于WorkbenchPart产生,看看其定义
org.eclipse.ui.IWorkbenchPart
A workbench part is a visual component within a workbench page. There are two subtypes: view and editor, as defined by IViewPart and IEditorPart.
从上图可以发现,propertySheet,ContentOutline等的设计上为了监听workbench的selection的变化并作出反映;反过来workbenchPart必须根据不同的的listener,即实现IPartListener的view,来返回不同的实现。
但是为什么getAdapter返回的是Page,而最终显示的是view,两者之间如何关联的呢?
getAdapter返回page,是在这里提供了自定义propertySheet的可能,该page将通过getContributorId()得到的唯一标示与tabs和section关联,所以需要显示则需要继续实现tabs和section。
看下面的代码:
public ITabDescriptor[] getTabDescriptors(IWorkbenchPart part, ISelection selection) { if (selection == null || selection.isEmpty()) { return EMPTY_DESCRIPTOR_ARRAY; } ITabDescriptor[] allDescriptors = null; if (tabDescriptorProvider == null) { allDescriptors = getAllTabDescriptors(); } else { allDescriptors = tabDescriptorProvider.getTabDescriptors(part, selection); } ITabDescriptor[] result = filterTabDescriptors(allDescriptors, part, selection); return result;
需要注意的是,如果使用了tabPropertySheet,则不用去实现IPropertySource。