How Do NetBeans Extensions Work?

Page content

Regardless of the fact that which module system seems best to you, the most important part is that you have a system that helps to create a modular architecture for your application. This ensures creation of a well maintainable architecture and easy to use extension points so that other users can extend your application without facing any problem. Keeping in mind all these facts we come to the conclusion that NetBeans platform has a much defined and maintainable architecture.

The easiest method of providing extension points in NetBeans is through the layer.xml, as you might know that the layer file is the central configuration file in NetBeans module. However, there are certain prerequisites and procedures to create your own extension points in NetBeans.

Procedural Steps:

At first step we need to create a Service Interface. We use the module “extensionpointinterface” in order to define an interface that is to be used by the Service Providers and by the module that uses the extension point as well. Create interface “MessageProviderInterface” inside that module with the single method getMessage() returning a string. Now this has to be made a part of the public API. For this, select API Versioning by right clicking the project node and select the check box of XX.ABCD.extensionpointinterface. This makes other modules to access this package.

At second step we need to create Service Provider implementations. Some modules are required that implement the ExtensionPointInterface and for this we will be using the modules “messageprovider1” and “messageprovider2”. Both of these require a dependency on “extensionpointinterface” so as to implement the interface. For this, right click the project node and select “Libraries” category, click “Add Dependency”, select “extensionpointinterface” and click “OK”. Now that you have access to the interface in the modules, you can easily implement it. Again for both the modules go to “Java Class” and select “New”. In the wizard’s name field type “MessageProvider1” or “MessageProvider2”. Now implement the interface and note that each of them should provide a different string. To make the MessageProviders available as services, you need to add these entries in the layer of the two modules. Now the following set of tags to be added between the layer file’s tags. Add the first set of tags in the first module and the second set in the second module;


The most primary prerequisite factor for this is that you should have NetBeans 6.1 or other older versions.

The second primary thing is to create a new ModuleSuite. From File option go to New Project and under Categories, select NetBeans Plug-in Modules. Under projects section, select the option “NetBeans Platform Application” or “Module Suite Project” and click Next. Type “layerextensionpoints” as Project Name in the Name and Location Panel. You can change the Project Location to any directory and click Finish. Next you need to create four modules within the suite “extensionpointinterface”, “messagereader”, “messageprovider1″ and “messageprovider2″.

The final part is to go to File option and choose New Project again. Select NetBeans Plug-in Modules under Categories and select Module Project and click next. Type the name in Project Name in the Name and Location panel. Note that the default in the wizard will be to create the module under the directory where the suite has just been created. Then click on “Next” button. Replace the Code Name Base with XX.ABCD. in the Basic Module Configuration panel and click Finish.

1st Set

2nd set

With this an instance of your MessageProviders is created using the standard constructor.

The next step includes the accessing of the services without setting a module dependency for them. So we are now going to find and use the Service Providers. For this we shall use the module “messagereader” to display the messages that have been provided by the two MessageProviders. To do so, you need to create a TopComponent inside the “messagereader” module.

Lastly, you need to sort the Service Providers, in NetBeans Platform you get a method to sort the layer entries. All you need to do is insert the “position” attributes in the layer file of the two modules so that the messages are displayed in correct order.This is all how we can implement NetBeans Extensions and how it works.