RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Tackle Device Fragmentation with NetBeans and the NetBeans Mobility Pack : Page 3

Building J2ME applications with a single code base that deploys to an ever-growing set of platforms is a tough and maybe impossible task. Find out how using Sun's NetBeans IDE with the NetBeans Mobility Pack can help.

NetBeans Project Configurations
In order to address some of the device configuration issues, NetBeans and the Mobility Pack provide a feature called "project configurations." Essentially, a project configuration defines a device environment in which your application will execute. In general, a JAR and JAD file are created from each project configuration. A single set of source code is maintained, but the project configurations provide direction to the IDE on how to build and generate the deployable code files for the various environments—stripping out and adding elements as necessary.

Figure 4. MIDP Properties Dialog: Edit the properties associated with a project configuration through this dialog box. To create or remove a project configuration, hit the manage configurations button.
Figure 5. The NetBeans with Mobility Pack IDE Button Bar: The drop down list in the middle of the IDE's button bar sets the current project configuration.

Several project configurations can be and often are defined for each project (or MIDlet suite). A project configuration defines such elements as what version of the MIDP profile and CLDC configuration a target platform provides and what optional J2ME APIs are installed, thereby restricting application code accordingly. It also specifies what emulator to use in order to test the application. Other environmental properties include a project configuration file that defines include API permissions, obfuscation levels, and other build parameters, security domains and signing, adjustments to the application descriptor, and "abilities" which are identifiers that can be used to associate source code to one or more specific features of a target device like having a larger screen or sound.

To create or manage a project configuration, right click on the NetBeans MIDP project and select "Properties" from the menu. In the ensuing dialog window (see Figure 4), a drop down list of existing project configurations and a "Manage Configurations…" button allow you add and remove project configurations. For StockQuote, three project configurations have already been created (OldPhone, NewPhone, and PDA) to represent the three target environments listed in Table 1 along with the DefaultConfiguration, which is provided by default with any new J2ME project. Select any of the project configurations and then take a look at the Categories listed on the left side of the dialog window. Click on the categories and examine the configuration properties that can be set in a project configuration and you begin to see how a tool like NetBeans can help with some of the fragmentation issues.

You will also notice on the NetBeans IDE button bar a drop down list that contains the names of the project configurations (see Figure 5). This is your means to quickly select and set the current project configuration as you work on your application. Based on which project configuration is selected, the appropriate code is built and emulator used in testing your project.

Click on the category called "Abilities" in the Properties dialog (as shown in Figure 4). As already noted, abilities are just identifiers or tags that are associated with a project configuration. Abilities suggest what specific feature or "ability" the target device might have. In the provided example code, three abilities have been created: bigScreen, hasSound, and newJ2ME. These abilities have been associated with the project configurations as shown in Table 2 and, as one might expect, given the capabilities of the devices suggested in Table 1. For example, the new phones are capable of playing sound, so the hasSound ability is associated to the NewPhone project configuration.

Device A – OldPhone project configuration

Device B – NewPhone project configuration

Device C – PDA project configuration

no abilities






Table 2. Project Configurations and Associated Abilities: Abilities are used to tag or identify device capabilities or characteristics of a target device specified by the project configurations.

Alone, abilities and their associations to project configurations really don't mean much. Its in their associations to lines of source code in preprocessor blocks (see the next section) that they allow you to tackle device fragmentation issues associated with device "abilities" like sound and screen size.

Preprocessor Blocks
Through special comment markups, lines of source code can be associated with project configurations and/or abilities. Known as "preprocessor blocks," these markups in the code, associating the code with either a project configuration or ability, indicate to the build facility in the IDE whether to add or remove the code when building the application class files. In other words, they help to define code that is active only for a specific device or device feature.

Figure 6. Managing Preprocessor Blocks: To set, unset, or modify a preprocessor block around any Java code in your MIDP application, select and highlight the code for the block and right click. Then select the appropriate project configuration or ability to associate with the block.

A small code example will help explain and demonstrate preprocessor blocks. Suppose, based on the size of a device's screen, you wanted to limit the size of a text field. Using preprocessor blocks and project configurations in NetBeans, the source code might look something like the following:

entryField = new TextField(“Your name:”,null,25,TextField.ANY);
entryField = new TextField(“Your name:”,null,10,TextField.ANY);
The preprocessor comments provide a kind of if/else conditional creating the appropriately sized text field (either 25 or 10 characters long) as needed depending on whether the code was compiled for the project configuration defined as BigDevice or not BigDevice (!BigDevice).

And how do these special preprocessor blocks get placed in the code? NetBeans Mobility pack provides a special code editor feature that allows any code to be easily and quickly associated with a project configuration and/or ability. Simply select text in the code editor and right click. A menu will appear and at the top of the menu (as shown in Figure 6) is an option for Preprocessor Blocks. Clicking on the Preprocessor Blocks option reveals a list of your project configurations and abilities. Simply select a project configuration or ability to put the code in a preprocessor block. The IDE will then take charge of putting the appropriate commenting around the code that the compiler will pick up and use when building your project. You will also notice that an "inverse" or "else" conditional preprocessor block can be set up using the same menu options. NetBeans will also use the same comments to appropriately highlight and comment/uncomment the preprocessor blocks while you are working on the application based on whichever project configuration is selected. This allows you to work with one source code file set and, through the selection of project configuration, drive the inclusion or exclusion of code blocks from the application that is built, tested, and ultimately deployed.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date