Practical Database Programming With Java
.pdf
5.3 Exploring NetBeans IDE 6.8 299
5.3.8 Build a NetBeans Module
A module can be considered as an independent object or unit that can be combined or bound together to form a big and more complex application. In fact, a NetBeans module enables NetBeans to be extended dynamically. All of the Open APIs are designed to be used for purposes of implementing modules. Modules can be ranged in complexity from a single Java class, properly packaged to do something elementary such as add a menu item to the Edit menu to display the contents of the clipboard, to a full-scale integration of a major external application, such as a Java profiling suite.
All modules are distributed and installed as JAR files. The basic format should be rather familiar; classes constituting the module are archived in the JAR, and special entries in the manifest file are recognized.
To the greatest extent possible, NetBeans has designed the module system to reuse standard technologies when they are sufficient, and to follow the style of others when not.
The basic idea for the format of modules is taken from the Java Extension Mechanism. The basic idea behind the Package Versioning Specification is used to handle dependencies both between modules and of modules to the system.
All modules have some set of basic properties that indicate: which types of features they provide, which Java classes implement these features, and what special option settings should be used when installing these features. Some of this information is listed in the manifest file using the customary format and NetBeans-specific attributes. The Java Activation Framework, as well as JDK-internal features, such as support for executable JAR files, is used as a model for how to specify the useful contents of a JAR in a data-driven fashion: many modules will need no special installation code other than attributes in the manifest and an XML layer giving additional more specific deployment information.
All module implementation classes must reside in a JAR file when they are finished in building. If you want to split up a large application into several pieces, perhaps so as to make independent upgrades possible, you should do so by creating multiple modules and relating them using versioning.
The so-called module versioning provides a way for modules to be specified in:
•Which module they are (i.e., that one JAR is an upgrade from another);
•Which version they are;
•Whether they have introduced incompatible API changes since a previous version;
•Whether they depend on other modules or system features.
While very simple modules may not require special versioning support, this system should be used for any module published on a general release schedule, or where it is expected other modules may want to make use of.
A module is recognized as such by NetBeans, by virtue of its having a special magic tag in the global section of the manifest file. Modules can do three things with versioning:
1.Specify what they are. This is done by the special OpenIDE-Module tag, whose value should be a unique (programmatic) identifier for the module, as mentioned above. Not be
300 Chapter 5 Introduction to NetBeans IDE
confused with the display name, which is free form and may be changed at will, this code name should not be changed arbitrarily—pick a name and stick with it throughout module releases.
2.Specify which version they are. In line with the Java Versioning Specification, modules can indicate two pieces of version information about themselves using the OpenIDE-Module- Specification-Version and the OpenIDE-Module-Implementation- Version tags. Modules are also permitted to use OpenIDE-Module-Build-Version to give information about when or by whom they were physically built, in case there is more specialized semantics given to the implementation version.
3.Specify which features they depend on. Again, this is done using the Versioning Specification—modules can request general or specific versions of other modules (OpenIDE-Module-Module-Dependencies), Java packages (OpenIDE- Module-Package-Dependencies), or Java itself (OpenIDE-Module-Java- Dependencies).
In the following sections, we will build a real NetBeans application to wrap three modules together to illustrate how to use the NetBeans Module to build and implement modules to develop a big and more complex application.
5.3.8.1 Create a New NetBeans Module Project
In this section,we will create a new NetBeans Module application JavaModuleCustomer, and then we will develop some other units and convert them to the associated modules. Finally, we will combine all associated modules with our main module JavaModuleCustomer together to make a big application.
The operation steps required for building this application are:
1.Create our main module application JavaModuleCustomer
2.Create an entity class for the Customer table in the JavaDB sample database and wrap that entity class into a module—module 2 in this application.
3.Wrap the system library EclipseLink, which works as a persistence library for our persistence API, and the database connector into another two modules—modules 3 and 4 in this application.
4.Create a new module that provides the user interface for our application. The new module gives the user a tree hierarchy showing data from the database—module 5 in this application.
5.Create another module that lets the user edit the data displayed by module 5.
By separating the viewer from the editor in distinct modules, we will enable the user to install a different editor for the same viewer, since different editors could be created by external vendors, some commercially and some for free. It is this flexibility that the modular architecture of the NetBeans Platform makes possible.
The software requirements for building this application are: NetBeans 6.8 or higher, and Java Developer Kit (JDK) 6.0 or higher.
Now let’s first create a new NetBeans Module application project
JavaModuleCustomer.
Launch NetBeans IDE 6.8 and go to File > New Project. In the opened New Project wizard, select NetBeans Modules from the Categories list and NetBeans
5.3 Exploring NetBeans IDE 6.8 301
Figure 5.146. The finished New Project wizard.
Platform Application from the Projects list, and then click on the Next button to continue.
Enter JavaModuleCustomer into the Project Name field and a desired location into the Project Location field. Make sure that the Set as Main Project checkbox is checked. Your finished New Project wizard should match one that is shown in Figure 5.146. Click on the Finish button to complete this creation process.
Next, let’s create the entity class for the Customer table in the JavaDB sample database and wrap it into a module.
5.3.8.2 Create the Customer Entity Class and Wrap It into a Module
Since we will not include this entity class into our new project, instead, we need to create a new entity class for the Customer table and wrap it into a new module. So perform the following operations to create this Customer entity class and wrap it into a module:
1. In the opened NetBeans IDE 6.8, go to File > New Project menu item to open the
New Project wizard.
2.Select Java from the Categories list and Java Class Library from the Projects list. Click on the Next button to continue.
3. Enter CustomerLibrary into the Project Name field, and click on the Finish button to close this process.
4.In the Projects window, right click on the CustomerLibrary project and choose
New > Entity Classes from Database.
5.In the opened wizard, select the JavaDB sample database by choosing its URL from the
Database Connection field, jdbc:derby://localhost:1527/sample[app on APP].
6. Select the Customer table from the Available Tables list and click on the Add button to add it into the Selected Tables list. The Discount Code table is also added automatically since there is a relationship between these two tables. Click on the Next button to continue.
302Chapter 5 Introduction to NetBeans IDE
7.Click on the Create Persistence Unit button to open the Create Persistence Unit wizard, since we need to use this persistence later. Keep all default settings unchanged and click on the Create button to create this persistence.
8.Enter CustomerPackage into the Package field as the package name, and click on the
Next button to continue. Your finished New Entity Classes from Database wizard should match one that is shown in Figure 5.147.
9.Click on the Finish button to close this process.
Once you have completed this step, look at the generated code and notice that, among other things, you now have a persistence.xml file in a folder called META-INF, as well as entity class Customer.java for the Customer table, as shown in Figure 5.148.
Figure 5.147. The finished New Entity Classes from Database wizard.
Figure 5.148. The created entity class for the Customer table.
5.3 Exploring NetBeans IDE 6.8 303
Figure 5.149. The finished New Library Wrapper Module Wizard.
NowopentheFileswindowandyoucanfindourlibraryJARfile,CustomerLibrary. jar, in the library project’s dist folder.
Next, we need to wrap this library module into our main module application
JavaModuleCustomer we created before.
Perform the following operations to finish this wrapping:
1.Right click on JavaModuleCustomer’s Modules node in the Projects window and choose Add New Library from the popup menu.
2.On the opened wizard, click on the Browse button that is next to the Library field to locate the folder in which our CustomerLibrary.jar file is located. In this application, it is C:\Book 9\DBProjects\Chapter 5\CustomerLibrary\dist. Click this file to select it and click on the Select button. Leave the License field empty and click on the Next button to continue.
3.Click on the Next button to the next wizard.
4.Enter org.customer.module into the Code Name Base field and click on the
Finish button to complete this process. Your finished wizard should match one that is shown in Figure 5.149.
Now you can find that a wrapped module CustomerLibrary has been added into our main project JavaModuleCustomer under the Modules node, as shown in Figure 5.150.
Next, let’s create another two modules to wrap the system library EclipseLink, which works as a persistence library for our persistence API, and the database connector into another two modules.
5.3.8.3 Create Other Related Modules
Perform the following operations to create and wrap these two libraries into the first module:
1.Right click on the Modules node under our project JavaModuleCustomer from the Projects window and select the Add New Library item from the popup menu to open the New Library Wrapper Module Project wizard.
304 Chapter 5 Introduction to NetBeans IDE
Figure 5.150. The newly wrapped module CustomerLibrary.
Figure 5.151. The finished New Library Wrapper Module Project wizard.
2.Click on the Browse button that is next to the Library field and browse to the location where the system library EclipseLink is located; it is CustomerLibrary\dist\ lib. Select two library files, eclipselink-2.0.0.jar and eclipselink-javax. persistence-2.0.jar, and click on the Select button.
3.Leave the License field empty and click on the Next button to continue.
4. In the Name and Location wizard, keep all default settings unchanged and click on the Next button to continue.
5. In the Basic Module configuration wizard, enter org.eclipselink.module into the Code Name Base field and keep all other default settings unchanged. Your finished Basic Module configuration wizard should match one that is shown in Figure 5.151.
6. Click on the Finish button to complete this process.
Immediately after you complete this creation and wrapper process, you can find that a new module eclipselink has been added into our project JavaModuleCustomer under the Modules node in the Projects window. If you open the Files window and expand the project folder to CustomerLibrary\dist\lib, you can find those two wrapper module files have been there, as shown in Figure 5.152.
Now let’s create another library wrapper module to make our database connector to another module.
5.3 Exploring NetBeans IDE 6.8 305
Figure 5.152. Two created wrapper module files.
Figure 5.153. The finished wrapper module derbyclient.
Perform the following operations to create this wrapper module:
1. Right click on the Modules node under our project JavaModuleCustomer from the Projects window, and select the Add New Library item from the popup menu to open the New Library Wrapper Module Project wizard.
2.Click on the Browse button that is next to the Library field and browse to the location where the Java DB client JAR is located in this application, which is C:\Program Files\Sun\JavaDB\lib. Select file, derbyclient.jar, and click on the Select button.
3.Leave the License field empty and click on the Next button to continue.
4. In the Name and Location wizard, keep all default settings unchanged and click on the Next button to continue.
5. In the Basic Module configuration wizard, enter org.derbyclient.module into the Code Name Base field and keep all other default settings unchanged. Your finished Basic Module configuration wizard should match one that is shown in Figure 5.153.
6. Click on the Finish button to complete this process.
Immediately, you can find that a new module derbyclient has been added into our project JavaModuleCustomer under the Modules node in the Projects window.
306 Chapter 5 Introduction to NetBeans IDE
Figure 5.154. The finished New Module Project wizard.
Next, let’s create a user interface to display the retrieved data from the Customer table in the JavaDB sample database.
5.3.8.4 Create the User Interface Module
In this section, we will create a user interface module and use it to display our retrieved data from the Customer table. Perform the following operations to complete this process:
1.Right click on the Modules node under our project JavaModuleCustomer from the Projects window, and select the Add New item from the popup menu to open the New Module Project wizard.
2.Enter CustomerViewer into the Project Name field and keep all default settings unchanged. Click on the Next button to continue.
3.In the Basic Module configuration wizard, enter org.customer.ui into the
Code Name Base field and keep all other default settings unchanged. Your finished Basic Module configuration wizard should match one that is shown in Figure 5.154.
4.Click on the Finish button to complete this process.
5.In the Files window, right click on the newly created module CustomerViewer, and
select New > Window Component item from the pop-up menu.
6.In the opened New Window wizard, select the editor from the Window Position combo box and check the Open on Application Start checkbox. Click on the Next button to continue.
7.Enter Customer into the Class Name Prefix field and keep all other default set-
tings unchanged. Your finished New Window wizard should match one that is shown in Figure 5.155. Click on the Finish button to complete this GUI creation process.
8. Use the Palette to drag and drop a Label to the top of this GUI window. Change the text of this label to The Details in the Customer Table.
9. Use the Palette to drag and drop a Text Area on this GUI window. Right click on this newly added Text Area and select the Change Variable Name item to change
5.3 Exploring NetBeans IDE 6.8 307
Figure 5.155. The finished New Window wizard.
Figure 5.156. The finished GUI Window.
its name to CustomerTextArea. Your finished GUI window should match one that is shown in Figure 5.156.
10.Click on the Source button to open the code window of this GUI Window, and add the codes that are shown in Figure 5.157 to the end of the TopComponent() constructor.
Let’s have a closer look at this piece of newly added codes to see how it works.
308
………
public CustomerTopComponent() { initComponents();
setName(NbBundle.getMessage(CustomerTopComponent.class, "CTL_CustomerTopComponent")); setToolTipText(NbBundle.getMessage(CustomerTopComponent.class, "HINT_CustomerTopComponent")); // setIcon(ImageUtilities.loadImage(ICON_PATH, true));
A EntityManager entityManager = Persistence.createEntityManagerFactory("CustomerLibraryPU").createEntityManager(); B Query query = entityManager.createQuery("SELECT c FROM Customer c");
CList<Customer> resultList = query.getResultList();
Dfor (Customer c : resultList) {
CustomerTextArea.append(c.getName() + " (" + c.getCity() + ")" + "\n");
}
}
Figure 5.157. The added codes to the end of the constructor.
