Creating extensions dynamically

I have been asked a few times how we create perspectives dynamically in Lotus Expeditor and Notes in the composite application space. We actually create many extensions dynamically using the Eclipse RegistryFactory API. The getRegistry() method returns an instance of the extension registry factory (IExtensionRegistry) where you can call addContribution(). Once this method is called the extension becomes available for use – usually reflected in a toolbar, menu, etc.

What would this kind of stuff be good for?

Say you have data stored in some other format – like an NSF, database, a external system – and you want that to be represented as some existing extension. You can now dynamically create those extensions from those other sources. This is exactly what we do for perspectives and other extensions in composite applications.

Another great sample would be to clone an existing perspective but maybe add a different context to it. For example, you could have your IPerspectiveFactory class create the layout for your perspective and base the contents on the Page data stored in the composite application model. This would allow your perspective to use this data space to initialize views and the data that is used in them. This means you could then contribute a special launch contribution item to the Open menu, dynamically create a new perspective off of the current workbench selection and initialize that perspective with the selection data. This would be yery similar to the Web Browser and Symphony documents. Your perspective may be a “user session” for a banking application. Each perspective may be a specific customer profile – so when you open up multiple instances of this perspective they would all point to different customers.

The cool thing about using page and component data is it can be conditionally used. You can use core Eclipse API's to get the data and if nothing is returned then its not available – if the data is returned you can use it! Check out the Wiki page I wrote about this model.

Tags: : :

To see code samples
The code below has a variable called buffer. The contents of buffer simply need to be the XML you would normally use for a plugin extension. For example, the buffer could be this xml:

<perspective
name="Test"
class="org.eclipse.ui.articles.perspective.TestPerspective"
id="org.eclipse.ui.articles.perspective.Test">

That sample plugin xml code came from the Using Eclipse Perspectives article.

byte[] bytes = buffer.toString( ).getBytes("UTF-8");
is = new ByteArrayInputStream(bytes);

IExtensionRegistry registry = RegistryFactory.getRegistry( );

if (!registry.addContribution(is, contributor, false, null, translations, null)){
//Log an error or something
}

Unfortunately there are some caveats to the call. The one major problem is the “persist” flag does nothing today. So if you want your contributions to be available on next launch you need to do this every time.

Advertisements

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.