0

我对 IAdaptable 和相关类感到困惑。哪个类是适配器、被适配者、可适配类型?

[语境]

我有一个表/树查看器条目的上下文菜单。上下文菜单中的某些操作必须不可见,具体取决于查看器中相应对象的状态(即表格查看器中行的属性值)。

我可以在 plugin.xml 中使用这样的配置来实现这一点:

<extension
         point="org.eclipse.ui.popupMenus">
      <objectContribution
            adaptable="false"
            id="<some_id>"
            objectClass="<object_class_of_viewer_entry>">
            <visibility>
               <objectState name="buildable" value="true"/>
             </visibility>
         <action
               class="<my_action_class>"

但是,这仅在对象类实现 org.eclipse.ui.IActionFilter 时才有效。

[问题]

我的对象类不能实现 IActionFilter,我不想改变它的接口。因此,我需要使用 IAdaptable 机制来解决这个问题。

阅读 Eclipse 文档让我对术语(适配器、适配者、可适配类型)感到困惑,我仍然对如何解决我的问题感到困惑。

对象类(在上面的配置中引用)必须保持不变。

我的方法如下。

<extension
         point="org.eclipse.core.runtime.adapters">
      <factory
            adaptableType="<object_class_of_viewer_entry>"
            class="MyFactory">
         <adapter
               type="org.eclipse.ui.IActionFilter">
         </adapter>
      </factory>
   </extension>

MyFactory 是这样的:

public class MyFactory implements IAdapterFactory {
  private static final Class[] types = {
    <object_class_of_viewer_entry>.class,
  };

  @Override
  public Object getAdapter(Object adaptableObject, Class adapterType) {
    return new <class_that_implements_IActionFilter>((<object_class_of_viewer_entry>) adaptableObject);
  }

  @Override
  public Class[] getAdapterList() {
    return types;
  }
}

这有什么问题?我在哪里错过了什么?

4

1 回答 1

0

原来一切,嗯,几乎一切,都是正确的。我只是在 plugin.xml 中混合了 object_class_of_viewer_entry 的接口和实现。

有帮助的两篇文章:http: //www.eclipsezone.com/articles/what-is-iadaptable/http://wiki.eclipse.org/FAQ_How_do_I_use_IAdaptable_and_IAdapterFactory%3F

于 2010-02-14T12:07:58.993 回答