This tutorial will introduce you to the basics of IJC plugin development and using Instant JChem APIs. It will walk you step by step through the process of creating 'Add Field' IJC plugin included in IJC API Examples suite.
In this tutorial we expect that you have made yourself familiar with the development tools and set up your environment as it is described in IJC Plugin: Quick Start and IJC Plugin: Hello World! tutorial. If you haven't done so, we strongly recommend you to read these two tutorials first before you proceed with the 'Add Field' plugin tutorial.
During this tutorial we are going to create our own version of the 'Add Field'
plugin, which is available as part of IJC API Examples suite. This gives
you chance to choose how exactly you want to follow this tutorial. You can
either recreate the plugin step by step as described here or you can use this
tutorial as a guide for exploring the 'Instant JChem API Examples - Add Field'
project that you have downloaded as part of api-examples-suite
in
IJC Plugin: Quick Start
tutorial.
By the end of this tutorial, you will understand the basic process of creating new extensions for IJC and you will also have been introduced to various resources that you can use to further develop your knowledge and skill in extending IJC. In particular, in this tutorial you will learn the following:
Contents:
- Exploring 'Add Field' plugin example
- Using the IDE's Wizards
- Coding the Module
- Conclusion
Before we begin creating our module, let's look at the sample that we will build in this tutorial. Once we have tried it out, we will know for ourselves what the result of this tutorial will be.
api-examples-suite
project as described in
IJC Plugin: Quick Start
'File' -> 'New Project...'
and choosing
'General' -> 'IJC Project (local database with demo data)'
in the 'New Project' wizard.
'localdb' -> 'Pubchem demo' -> 'Pubchem tabular form'
view.
'Add Atom count field'
menu item in the main menu 'Help' -> 'API Examples'
.
'Atom Count'
field added to the JChem table, as
shown below:
The new 'Atom Count' field added to the table.
Note: In the bottom right corner of the screenshot above, you can see a progress bar, with the text "Filling data". In this tutorial, you will learn, among many other things, how to create a progress bar to enable asynchronous tasks to run in the background, so that the user interface remains enabled while the field is being added to the table.
In this tutorial we are going to implement the same functionality from scratch.
If you followed the IJC Plugin: Hello World! tutorial you should have 'IJC Extensions' module suite and 'HelloWorldPlugin' module already available. Open 'IJC Extensions' suite project in NetBeans IDE, we will add a new module to it.
In your IDE go to main menu 'File' -> 'New Project...'
and create a
new 'NetBeans Modules' -> 'Module'
project. Call the project 'MyAddFieldPlugin'
and don't forget to add it to 'IJC-Extensions' module suite.
As the module's 'Code Name Base' enter org.myorg.myaddfieldplugin
.
New 'MyAddFieldPlugin' module project in NetBeans IDE's 'Projects' explorer
Our next step is to specify which APIs we want to use. Each API is provided by a module. Some of the APIs belong to the NetBeans API, while others belong to the Instant JChem API. Because our module is deployed to Instant JChem (ie. 'IJC Platform' that we set up earlier), which itself makes use of all NetBeans and Instant JChem APIs that we need, we can simply just set dependencies on modules available in 'IJC Platform'.
'Libraries' -> 'Module Dependencies'
and click 'Add Dependency...'
button to specify modules
that 'MyAddFieldPlugin' module will use.
Commons - Generic IM Commons
,
Commons - NB dependent
,
Dialogs API
, DIF API
, DIF Core Implementation
,
Instant JChem Core
, Library - JChem
,
Nodes API
, UI Utilities API
, Utilities API
.
'OK'
and close the 'Properties' dialog.
The list of module dependencies in the 'Project Properties' dialog should look like this.
'MyAddFieldPlugin' module dependencies
The dependencies we have just added as well as reasons for adding them (i.e. the classes these modules provide in their API) are summarized in the following table.
Module | Description | Classes that we need |
---|---|---|
Dialogs API | Provides miscellaneous general classes, especially relating to the handling of user notifications and the displaying of dialogs and wizards. |
|
DIF API DIF Core Implementation (Discovery Informatics Framework) |
Provides the data model and persistence tiers for applications that need to use chemical and biological data. |
|
Instant JChem Core | Provides the basic user interface for Instant JChem and through its API it provides the ability to extend the IJC user interface. |
|
JChem | Provides access to ChemAxon's Java toolkits that let you perform sophisticated chemistry and database operations in Java code. |
|
Nodes API | Provides support for NetBeans Platform design patterns, called 'cookies', which are used to add behavior to existing data objects and nodes. |
|
Utilities API | Provides a set of utility classes covering general infrastructure points in the NetBeans Platform. |
|
In this section, we use the New Action wizard to create a menu item and toolbar button that will be context-aware, which means that they will only be enabled when needed. In this tutorial's scenario, the menu item and toolbar button will only be enabled when a table, also known as an 'entity', is selected in IJC. Once we have completed the wizard, we will have a skeleton "action", which the user will be able to invoke either from the menu item or from the toolbar button, if enabled. Later on we will fill out this action with additional functionality needed for this module.
New Action wizard - choose 'Conditionally Enabled' action type
Click Next.
New Action Wizard - choose where the action will appear in UI
Click Next.
You should now see the following:
New Action wizard - enter class name, display name and choose package for the new action
The wizard creates a new action class and uses the
layer.xml
file to register
it as a menu item and toolbar button. In addition, the icons are copied
into the module. You should see the action class in NetBeans 'Projects' explorer:
The new 'AddFieldAction' class
As you can see the action class and its parent nodes are marked with the error badge.
This indicates that something is broken in the class. Let's open the class and examine it.
You will notice that the line referring to EntityCookie
is underlined in red,
which means that the java compiler does not know EntityCookie
class.
In this case, we need to add an import statement for the EntityCookie
.
Right-click in the editor and choose Fix Imports (Ctrl-Shift-I
).
The IDE adds the import statement and the red underline disappears.
'AddFieldAction' class with no errors
We can now try out the module and see if the action works. The action's menu item and toolbar button should be enabled and disabled depending on what is selected in Instant JChem.
'Run'
.
The IDE will deploy the plugin and start Instant JChem. You should see the
action's toolbar button and also the 'Add Atom Count Field'
menu item in the main menu
'Edit'
.
'Pubchem demo' -> 'Pubchem grid view'
and both
the toolbar button and the menu item for our action should become enabled.
Instant JChem with MyAddFieldPlugin
- 'Add Atom Count Field' toolbar button enabled
'Pubchem demo' -> 'Pubchem grid view'
node the button and
the menu item will become disabled. This is because the selected node
does not provide the EntityCookie
while the opened
'Pubchem grid view' editor provides this cookie.
Instant JChem with MyAddFieldPlugin
and the AddAtomCountFieldAction
's toolbar button
AddAtomCountFieldAction.performAction()
method yet.
Before continuing with the coding of our module, let's examine the artifacts that the New Action wizard generated for us.
The generated action class looks as follows. Note that comments that briefly explain each method are found highlighted inline below.
AddAtomCountFieldAction.java source file
The layer.xml
file contains the user interface registration for
our action. Notice that there is a top-level
element called "filesystem", containing the sub-elements "Actions", "Menu", and "Toolbars".
The "Actions" element determines where the action will be registered in the Options window,
the "Menu" element determines where the action's menu item will be found, and the "Toolbars"
element specifies the toolbar where the action's toolbar button will be located.
layer.xml source file
Currently, when the toolbar button or the menu item are clicked, nothing happens.
In this section, we fill out the performAction()
method so that a dialog
is shown when the action is invoked.
performAction()
method:
Code snippet - AddAtomCountFieldAction
class, performAction()
method
Code snippet - Bundle.properties
file
Instant JChem with MyAddFieldPlugin
- dialogs shown from 'Add Atom Count Field' action
In this section we are going to create a field by using asynchronous tasks running in background.
Code snippet - AddAtomCountFieldAction
class, static fields declaration
Code snippet - AddAtomCountFieldAction
class, findStructureField()
method
Code snippet - AddAtomCountFieldAction
class, createNewField()
method
Code snippet - AddAtomCountFieldAction
class, fillData()
method
Code snippet - Bundle.properties
file, adding string resources
<MyAddFieldPlugin Project>/src/org/myorg/myaddfieldplugin/AddAtomCountFieldAction.java |
createNewField(entity);
The plugin is now done! Let's try out if it works.
'Edit' -> 'Add Atom Count Field'
menu item.
It will add a new field called "Atom Count" to 'Pubchem demo' entity
and will fill it with the actual atoms count for each structure.
Instant JChem with MyAddFieldPlugin
- creating 'Atom Count' field and filling it with data
Congratulations! You have successfully completed an IJC plugin that adds a new field to an existing entity and fills it with the atom count for each structure in the entity. In this tutorial you have learned:
For other IJC plugin development related tutorials please see their complete list here.