getAdapter理解

先看官方的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。

 

转载于:https://www.cnblogs.com/crazywings/p/3255458.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值