2009-07-30 62 views
3

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

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

更新:
有人问我为什么会想这样做,所以我给和实例的使用情况

在Eclipse中,如果您运行MultiPageEditor插件例子中,你将创建三个选项卡编辑。如果我发布一个扩展,允许添加制表符,并希望它们以某种/逻辑/上下文顺序添加,而不是随机/特定于操作系统的文件排序顺序。

回答

2

当我要订购扩展点贡献,我添加了一个优先数,如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; 

订货代码可以通过这样的事情:

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; 
     } 
    }); 
2

您没有任何直接控制它们被解析或返回的顺序,但这应该不重要。为什么他们以特定的顺序返回对你来说很重要?我隐约记得,每个扩展位置都是按照它们定义的顺序依次读取的,并且位置中的每个插件都按字母顺序读取,但由于P2在Eclipse 3.4中出现,因此处理方式可能会有所不同。我不会依赖任何阅读顺序,因为你不应该知道这些知识。

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

article是有点老,但我相信仍然有效。它给你一些关于扩展点处理的指针。