Requirement: A simple Hello World message display when an event is triggered.
Plugins:
testcore - defines a plugin extension point called eventhandler.
testplugin1 - contributes to the extension point - eventhandler.
testplugin2 - contributes to the extension point - eventhandler.
Step A: Create Extension Point: EventHandler
open plugin.xml of plugin:testcore.
1. Open Extension points tab, Click Add, which opens a New Extension Point Dialog, Give the following entries as shown in the below picture.
fig 4: Extension Point Attribute: "class" definition
2b. Create IEventHandler class in package testcore.eventhandler with following details:package testcore.eventhandler;
public interface IEventHandler {
public void handleEvent(String message);
}
2c) Open Runtime tab, Add testcore.eventhandler package for IEventHandler to be visible to other plugins
3. Open plugin.xml of Plugin: testplugin1.
3a) Open Dependencies tab, Add testcore plugin as a dependency.
3b) Open Extensions tab, Add testcore.eventhandler extension point. And edit id and class values for the event handler as shown in picture below:
3c) Create Plugin1EventHandler class in testplugin1.eventhandler package.
package testplugin1.eventhandler;
import testcore.eventhandler.IEventHandler;
public class Plugin1EventHandler implements IEventHandler {
public Plugin1EventHandler() {
// TODO Auto-generated constructor stub
}
public void handleEvent(String message) {
// TODO Auto-generated method stub
System.out.println("in Plugin1EventHandler:"+message);
}
}
4. Open plugin.xml of Plugin: testplugin2.
4a) Open Dependencies tab, Add testcore plugin as a dependency.
4b) Open Extensions tab, Add testcore.eventhandler extension point. And edit id and class values for the event handler as shown in picture below:
4c) Create Plugin2EventHandler class in testplugin1.eventhandler package.
package testplugin2.eventhandler;
import testcore.eventhandler.IEventHandler;
public class Plugin2EventHandler implements IEventHandler {
public Plugin2EventHandler() {
// TODO Auto-generated constructor stub
}
public void handleEvent(String message) {
// TODO Auto-generated method stub
System.out.println("in Plugin2EventHandler:"+message);
}
}
5. Open Activator class of testcore plugin and do the following in start method.
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
IExtensionRegistry registry = Platform.getExtensionRegistry();
IExtensionPoint point = registry.getExtensionPoint("testcore.eventhandler");
if (point == null) return;
IExtension[] extensions = point.getExtensions();
for (int i = 0; i < extensions.length; i++){
IConfigurationElement[] configElements = extensions[i]
.getConfigurationElements();
for(IConfigurationElement configElement : configElements){
System.out.println(configElement.getAttribute("class"));
IEventHandler eventHandler = (IEventHandler) configElement
.createExecutableExtension("class");
eventHandler.handleEvent("Hello World");
}
}
}
6. Run testcore as Eclipse Application. Add testplugin1 and testplugin2 to its run time class path. You should see the following messages:
testplugin1.eventhandler.Plugin1EventHandler
in Plugin1EventHandler:Hello World
testplugin2.eventhandler.Plugin2EventHandler
in Plugin2EventHandler:Hello World
which shows the contributor plugins : testplugin1 and testplugin2 's EventHandler's getting called.
No comments:
Post a Comment