views:

291

answers:

2

In Eclipse you can publish an extension point for downstream plugins to use. When looping through the extensions for your point, as returned from the registry, is there a way to know/control the order they are returned.

How does eclipse find extensions, by order of files on disk?, name?

Update:
Someone asked why I would want to do this so I'll give and example use case

In eclipse if you run the MultiPageEditor plugin example you'll create an editor with three tabs. If I publish an extension allowing the addition of tabs to this and wanted them added in a certain/logical/contextual order and not a random/OS specific file ordering order.

+2  A: 

You don't have any direct control over the order they are parsed or returned, but this shouldn't matter. Why is it important to you for them to be returned in a particular order? For what it's worth I vaguely recall that each extension location is read in turn in the order they are defined, and each plugin in the location is read alphabetically, but this might have changed since P2 came along in Eclipse 3.4 as the processing is very different. I wouldn't rely on any read ordering anyway as you aren't supposed to be privy to this knowledge.

If you need to order your contributions, when you get the extensions from the registry, you can add them to a list, implement a comparator, and sort them if desired. Or you can iterate them in order and sort the resultant objects before proceeding.

This article is a little old but I believe still valid. It gives you some pointers on extension-point processing.

Rich Seller
+1  A: 

When i want to order the extension point contributions, i add a priority number, like int 10. I only use numbers of 10, 20 , 30,... because you can easily put elements between in the future. This you can use for ordering Buttons, Composites, or everything which you can not order by name.

You can add this priority into your interface you are using to define your extension point. Or you can spend a field in the extension point description.

When you collect all extension point contrubutions you can ask for the priority and order the contributions in a linked list before returnin them.

 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;

the order code could by something like

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;
        }
    });
Markus Lausberg