Eclipse扩展点扩展的排序/排序

Eclipse扩展点扩展的排序/排序,eclipse,plugins,Eclipse,Plugins,在Eclipse中,您可以发布一个扩展点供下游插件使用。 当循环通过从注册表返回的点扩展时,是否有方法知道/控制它们返回的顺序 eclipse如何按磁盘上文件的顺序查找扩展名 更新: 有人问我为什么要这样做,所以我将给出示例和用例 在eclipse中,如果运行MultiPageEditor插件示例,您将创建一个包含三个选项卡的编辑器。如果我发布了一个扩展,允许在此扩展中添加选项卡,并希望它们以特定的/逻辑/上下文顺序添加,而不是以随机的/操作系统特定的文件顺序添加。您无法直接控制它们被解析或返回

在Eclipse中,您可以发布一个扩展点供下游插件使用。 当循环通过从注册表返回的点扩展时,是否有方法知道/控制它们返回的顺序

eclipse如何按磁盘上文件的顺序查找扩展名

更新:
有人问我为什么要这样做,所以我将给出示例和用例


在eclipse中,如果运行MultiPageEditor插件示例,您将创建一个包含三个选项卡的编辑器。如果我发布了一个扩展,允许在此扩展中添加选项卡,并希望它们以特定的/逻辑/上下文顺序添加,而不是以随机的/操作系统特定的文件顺序添加。

您无法直接控制它们被解析或返回的顺序,但这不重要。为什么按特定顺序退货对您很重要?值得一提的是,我隐约记得,每个扩展位置都是按照定义的顺序依次读取的,位置中的每个插件都是按字母顺序读取的,但是自从Eclipse3.4中出现P2之后,这可能已经发生了变化,因为处理过程非常不同。无论如何,我不会依赖任何阅读顺序,因为你不应该知道这些知识

如果您需要对贡献进行排序,当您从注册表获得扩展时,您可以将它们添加到列表中,实现一个比较器,并根据需要对它们进行排序。或者,您可以按顺序迭代它们,并在继续之前对结果对象进行排序


这有点陈旧,但我相信仍然有效。它为您提供了一些关于扩展点处理的指针。

当我想要订购扩展点贡献时,我会添加一个优先级编号,如int 10

我只使用10,20,30。。。因为您可以在将来轻松地将元素放在两个元素之间。这可以用于订购按钮、复合材料或无法按名称订购的所有物品

您可以将此优先级添加到用于定义扩展点的接口中。也可以在扩展点描述中使用一个字段

当您收集所有扩展点贡献时,您可以询问优先级,并在返回之前在链接列表中对贡献进行排序

 String tmpExtensionPoint = "EXTENSION POINT ID"; //$NON-NLS-1$
 IExtensionRegistry registry = Platform.getExtensionRegistry();
 IConfigurationElement[] elements = registry.getConfigurationElementsFor(tmpExtensionPoint);

    List references = new LinkedList();
    if (elements != null && elements.length > 0) {
        for (int i = 0; i < elements.length; i++) {
            try {
                Object obj = elements[i].createExecutableExtension("class");
                references.add((IExtensionPointInterface)obj); //$NON-NLS-1$
            } catch (CoreException e) {
                logger.error("Get Extension Point For " + tmpExtensionPoint, e);
            }
        }
    }

//...
//ORDER here

return references;
String tmpExtensionPoint=“扩展点ID”//$非NLS-1$
IExtensionRegistry=Platform.getExtensionRegistry();
IConfigurationElement[]elements=registry.getConfigurationElements for(tmpExtensionPoint);
列表引用=新建LinkedList();
if(elements!=null&&elements.length>0){
for(int i=0;i
订单代码可以通过以下方式显示:

Arrays.sort(references, new Comparator() {
        public int compare(Object arg0, Object arg1) {
            if (!(arg0 instanceof IExtensionPointInterface )) {
                return -1;
            }

            if (!(arg1 instanceof IExtensionPointInterface )) {
                return -1;
            }

            IExtensionPointInterface part0 = (IExtensionPointInterface)arg0;
            IExtensionPointInterface part1 = (IExtensionPointInterface)arg1;

            if (part0.getPriority() < part1.getPriority()) {
                return -1;
            }

            if (part0.getPriority() > part1.getPriority()) {
                return 1;
            }

            return 0;
        }
    });
Arrays.sort(引用,新的比较器(){
公共整数比较(对象arg0、对象arg1){
if(!(IExtensionPointInterface的arg0实例)){
返回-1;
}
if(!(IExtensionPointInterface的arg1实例)){
返回-1;
}
IExtensionPointInterface部件0=(IExtensionPointInterface)arg0;
IExtensionPointInterface part1=(IExtensionPointInterface)arg1;
if(part0.getPriority()part1.getPriority()){
返回1;
}
返回0;
}
});