Sort / organize extensions of your Eclipse extension point

In Eclipse, you can publish the extension point for plugins down. When navigating through extensions for your points returned from the registry, there is a way to find out / control the order they return.

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

Update:
Someone asked why I would like to do this, so I will give an example using case

In eclipse, if you run the MultiPageEditor plugin example, you will create an editor with three tabs. If I publish the extension by adding tabs to it and want them to be added in a specific / logical / contextual order, and not in the order of ordering the files in any / OS.

+3
source share
2 answers

When I want to order extension liner points, I add a priority number, for example, int 10.

I use only the numbers 10, 20, 30, ... because you can easily add elements between them in the future. This can be used to order buttons, composites, or anything you cannot order by name.

You can add this priority to your interface, which you use to define your extension point. Or you can hold a field in the description of the extension point.

When you collect all contributions to additional points, you can request a priority and order deposits in a linked list before returning 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 may look something like this:

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
source

, , . ? , , , , , , P2 Eclipse 3.4, , , - , .

, , , , . .

article , , . .

+2

Source: https://habr.com/ru/post/1713928/


All Articles