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