Writing Code Specific to a List of Configurations


Perhaps one of the most uniquely challenging aspects of handling the device fragmentation problem has been managing differences in code between distributions of an application. The NetBeans approach to the problem has been to integrate a preprocessor into the build process that activates or deactivates sections of code based on which configuration is active at compile time.

The NetBeans preprocessor is a low-impact tool that uses Java comments both to define code sections and to activate and deactivate these sections. As such, all files before and after preprocessing can be valid, syntactically correct Java source. This also ensures that the preprocessed files integrate seamlessly with the debugger.

Source files are always saved to the hard drive as though the default configuration were active. This eliminates the VCS conflicts that could otherwise occur due to the local version's having a different active configuration than the VCS version.


A series of context-menu commands exist to assist you in adding these special comments to your source files. These commands are described in the following sections.

Duplicating a Code Block

Sometimes, there is a section of code that must be defined differently for some configurations and that must exist even for configurations that don't explicitly define a code block in the section. An example of this would be if your class definition is fragmented. For example, all of your configurations that will be deployed to Nokia devices might define your Canvas object like this

public class MIDPCanvas extends com.nokia.mid.ui.FullCanvas {


while the rest of your configurations would just extend Canvas. After adding the appropriate code blocks, you would be left with the code shown in Figure 14-6.

Figure 14-6. Duplicated code blocks in the Source Editor


These types of blocks can be created in one step using the Create If / Else Block context-menu command:

  1. Highlight the code section that you would like to be duplicated.

  2. Right-click in the Source Editor and choose Preprocessor Blocks | Create If / Else Block.

  3. Type in a configuration name or other statement right after the #if directive. The IDE offers a list of all available configurations and abilities.

    Two code blocks are created, each containing the highlighted code.

Only full lines of text may be associated with a configuration. If you would like to associate only part of a line with the configuration, first break the line into smaller sections and associate the sections appropriately.


How to Interpret Code Block Visualization

The visualization of code blocks within the IDE is that of color highlighted sections. This visualization can help you quickly determine some information about code sections:

  • Sections that are active for the currently selected configuration are pink. Also, code contained within the section is uncommented.

  • Inactive sections are gray. Each line in the code section is prepended with a specially formatted line comment (//#).

The state of the special comments within the IDE's Source Editor is unimportant, as the preprocessor runs before any compilation command and places the lines of each code block in the correct comment state.

If the special comments within the code blocks fall out of sync with the current state of the code block in the IDE, you can correct them by right-clicking them in the editor and choosing Preprocessor Block | Re-comment or by simply switching configurations. The preprocessor automatically fixes the errors.




NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net