In the previous tutorial, we presented how to create a module project and a first part of implementation. In this tutorial, we will describe how to create an input form and use the menu item & toolbar button to display this form.
Create input form
- Right click on fr.adfab.magebeans.guis, choose New à Other
- Choose File Type:
- In Categories, choose Swing GUI Forms
- File types: choose JDialog Form.
- Name and Location:
- Class Name: CreateModuleForm
An Java class CreateModuleForm is created. This class contains a form so we can use the design mode to add the input elements to this form.
Before implement the event for the two button “Create” and “Cancel”, we need to implement a Java class in fr.adfab.magebeans.processes which is handle the process of creating folder structure for a magento module. We call it CreateModuleProcess m and we do not describe here. You can find this class in source code on github (MageBeans).
In the input form for creating a module, there are two button that we need to implement a function which handle their click event.
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}
btnCreateActionPerformed
private void btnCreateActionPerformed(java.awt.event.ActionEvent evt) {
CreateModuleProcess createModuleProcess = new CreateModuleProcess();
String codePool = "local";
if (rbtnCommunity.isSelected()) {
codePool = "community";
}
try {
createModuleProcess.setProjectDir(this.projectDir);
createModuleProcess.setCodePool(codePool);
createModuleProcess.setCompany(txtCompany.getText());
createModuleProcess.setModule(txtModule.getText());
...
createModuleProcess.process();
dispose();
} catch (FileNotFoundException | IllegalArgumentException ex) {
Exceptions.printStackTrace(ex);
}
}
In the function which performed on the button Create click event, we use the variable projectDir to store the path of magento project on the hard driver. This variable is used in CreateModuleProcess to generate the module file structure in the right project. But this form is executed from the CreateModuleAction so it cannot take the Netbeans project as a context. We need to provide this value as an argument.
public void run() {
CreateModuleForm dialog = new CreateModuleForm(new javax.swing.JFrame(), true);
dialog.setProjectDir(args[0]);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
Open input form from action
All the required elements are ready, now we need to integrate them into the Netbeans framework action to make the first version of plugin work completely. In the last tutorial, we already created an action which create a button in toolbar and an item in the source menu. A function will be called in performing the clicked event on these items.
@Override
public void actionPerformed(ActionEvent ev) {
FileObject projectDir = project.getProjectDirectory();
String filePath = projectDir.getPath() + "/app/Mage.php";
File mageFile = new File(filePath);
if (mageFile.exists() && !mageFile.isDirectory()) {
String args[] = new String[1];
args[0] = projectDir.getPath();
CreateModuleForm.main(args);
} else {
JOptionPane.showMessageDialog(null, "Current project is not magento 1x", "MageBeans plugin", JOptionPane.ERROR_MESSAGE);
}
}
The CreateModuleAction is conditionally enabled action type with the cookie class is Project so a Project instant is passed as the parameter in its constructor function which is used to get the project path in actionPerformed function
public final class CreateModuleAction implements ActionListener {
private final Project project;
public CreateModuleAction(Project context) {
this.project = context;
}
...
Build nbm file
A plugin can be added into a NetBeans IDE by installing from NetBeans Plugin Portal or from a nbm local file. So to deploy a plugin, you need to build a nbm file for the both ways of installation. To install a NetBeans plugin, in Tools → Plugins, choose Available Plugins tab and select the plugin to install online or choose Downloaded tab to install from a nbm local file.
Before packaging the plugin into a nbm file, you need to fill the plugin information:
- Right click on project in Projects view, choose Properties
- In Display section, fill the information form
- In Build → Packaging, upload the license file
This is very easy to build the nbm file in NetBeans:
- Right click on the MageBeans project in Project view
- Choose Create NBM
- The nbm file is located in folder /build
Now the 1st version of MageBeans plugin is released, you can install it into NetBeans IDE to use. You will see the button on toolbar & menu as in the figure below.
And the module folder structure created by MageBeans plugin
Conclusion
After two tutorials, a simple version of MageBeans is released which help us to understand how to create and implement a plugin for NetBeans IDE. In the next tutorial, we will discuss furthermore on the implementation of code completion which support for Magento in development.