Anand
Anand

Reputation: 12152

eclipse RCP UI through java code

Can I implement Eclipse RCP UI using java code only and not plugin.xml?

Upvotes: 1

Views: 250

Answers (2)

Marteng
Marteng

Reputation: 1305

You can programmatically add and remove extensions. See following example methods (adapt on demand):

public void addExtension() throws UnsupportedEncodingException {
    String pluginXmlAsString = "<a string with the content of plugin.xml";
    InputStream pluginXmlIs = new ByteArrayInputStream(pluginXmlAsString.getBytes(StandardCharsets.UTF_8.name()));

    IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
    Object token = ((ExtensionRegistry) extensionRegistry).getTemporaryUserToken();
    IContributor contributor = ContributorFactoryOSGi.createContributor(Platform.getBundle("org.acme.mybundle"));

    extensionRegistry.addContribution(pluginXmlIs, contributor, false, null, null, token);
}

public static void removeExtensionsContributedByMe() {
    String extensionPointId = "<ID of the extension point for remove an extension of";
    String extensionContributor = "org.acme.mybundle";

    ExtensionRegistry extensionRegistry = (ExtensionRegistry) Platform.getExtensionRegistry();
    IExtensionPoint extensionPoint = extensionRegistry.getExtensionPoint(extensionPointId);
    IExtension[] extensions = extensionPoint.getExtensions();
    Object token = extensionRegistry.getTemporaryUserToken();

    for (IExtension extension : extensions) {
        if (extensionContributor.equals(extension.getContributor().getName())) {
            extensionRegistry.removeExtension(extension, token);
        }
    }
}

We use this for unit tests which add extensions as preparation and remove extension to clean up. This way the tests do not influence each other (which would be the case if the extensions are "hard coded" in plugin.xml or fragment.xml).

Upvotes: 0

VonC
VonC

Reputation: 1328912

While it might be possible in theory (eclipse plugins are OSGi bundle which are read by the extension registry), I don't think it is practical (unless you re-implement the extension registry lifecycle).

Eclipse Equinox precisely extends the concept of bundles with the concept of extension points, hence the mandatory presence of plugin.xml.

Upvotes: 1

Related Questions