This tutorial shows how to add an action, invoked by a menu item, for reading values from the grid view selection and calculating averages or sums.
This tutorial assumes that you have worked through IJC Plugin: Quick Start and IJC Plugin: Hello World! tutorials and that you are familiar with the concepts and instructions described therein. As before, you will use NetBeans IDE to create the example plugin that will extend Instant JChem.
Contents:
- Exploring 'Math computation actions' plugin example
- Using the IDE's Wizards
- Coding the Module
- Building and running the plugin
- Conclusion
Before we begin creating our module,
let's get to know the sample that
we will build in this tutorial. The
sample is available as part of the api-examples-suite
project that you've downloaded in IJC Plugin: Quick Start.
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 grid view'
view.
'Help' -> 'IJC API Examples'
menu
there are two menu items called 'Compute average'
and 'Compute sum'
. If you selected two or
more numeric values these actions should be enabled.
Instant JChem with the demo project and two values - 169.07 and 202.55 - selected.
The result of 'Compute Sum' action - the sum of the two selected values
Now that you know the functionality that this tutorial provides, let's create the 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 'MyMathCalcPlugin'
and don't forget to add it to 'IJC-Extensions' module suite.
As the module's 'Code Name Base' enter org.myorg.mymathcalcplugin
and tick the 'Generate XML Layer' checkbox, leave the default location offered for
the 'XML Layer' file.
New 'MyMathCalcPlugin' 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.
'MyMathCalcPlugin' 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 a set of utility classes for creating dialog boxes. | |
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, provides the ability to extend the IJC user interface. | |
Nodes API | Provides a way to represent JavaBeans or other property containers, formed into a hierarchical tree. | |
Utilities API | Provides a set of utility classes covering general infrastructure points in the NetBeans Platform. |
ComputationPerformer
InterfaceFirst, let's begin by creating an interface for our computations.
org.myorg.mymathcalcplugin
,
create a java interface called ComputationPerformer
. Right click
the package's node and in the popup menu choose 'New' -> 'Java Interface...'
.
This will start the 'New Java Interface' wizard where you can fill in
the interface name.
New Java Interface wizard - creating 'ComputationPerformer' interface
ComputationPerformer.java source file
AbstractComputationPerformer
classNext, we'll implement the interface that we created in the previous section.
AbstractComputationPerformer
.
It's similar process as with the interface before. Right click the package's node
and choose 'New' -> 'Java Class...'
, which will start
the 'New Java Class' wizard.
AbstractComputationPerformer.java source file
AbstractComputationAction
classWe will create an abstract action, which we will use as the basis of both the actions that we create in the following section.
AbstractComputationAction
.
AbstractComputationAction.java source file
ComputeSumAction
classLet's now implement the abstract action from the previous section. Here, we implement it to create an action to compute sums.
ComputeSumAction
.
ComputeSumAction.java source file
ComputeAverageAction
classLet's now implement the abstract action from the previous section. Here, we implement it to create an action to compute averages.
ComputeAverageAction
.
ComputeAverageAction.java source file
Bundle.properties
The centralized location for strings in our plugin is the resource bundle,
which is located in the main package and called Bundle.properties
.
Let's update it to contain all localizable strings that we have used throughout the code:
Bundle.properties source file
Finally, let's register our actions in the layer file.
layer.xml source file
If you've followed this tutorial closely you should now have all files in your 'MyMathCalcPlugin' project ready. The project in NetBeans IDE's 'Projects' explorer should look like the picture below.
Complete 'MyMathCalcPlugin' project in NetBeans IDE's 'Projects' explorer
You can now r-click the project's node and build the project by choosing
'Clean and Build' and then run it by invoking 'Run' from the same popup
menu. Instant JChem will start with MyMathCalcPlugin installed and the
actions that we have created will be available in the main menu
'Help' -> 'IJC API Examples'
.
Congratulations, you have created actions for computing sums and averages in Instant JChem. In this tutorial you have learned:
layer.xml
.For other IJC plugin development related tutorials please see their complete list here.