當我要訂購擴展點貢獻,我添加了一個優先數,如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;
}
});