| ## Java 1.1 Unleashed # - 56 - Netscape's Internet Foundation Classes by William R. Stanek IN THIS CHAPTER The Next Generation of AWT: The IFC What's in the IFC? Installing the IFC Creating IFC-Enhanced Applets and Applications Testing the Installation As Java evolves, it becomes increasingly apparent that there are many ways it can be improved--especially when it comes to the graphical user interface contained in the Abstract Windowing Toolkit (AWT). Because Java is an object-oriented programming language, the user interface can be easily extended with a set of advanced and fully customizable components. This is where the Netscape Internet Foundation Classes (IFC) come into the picture. Netscape's IFC features enhancements to Java's user interface that you simply cannot find in Java 1.1. But best of all, if you use the IFC, you can finally unleash the multimedia capabilities of your system. The IFC features extensive support for images, sounds, and animation. The IFC packages are included with Netscape Navigator 4.0 and later. If you don't use Netscape Navigator, you can still use Netscape IFC, but you will have to download the developer kit. ## The Next Generation of AWT: The IFC With the Internet Foundation Classes, Netscape establishes itself as a leader in Java development. Keep in mind that the Internet Foundation Classes are designed to compliment Java's Abstract Windowing Toolkit (AWT). As such, the IFC does not replace the AWT; it enhances the AWT. Because the IFC APIs and packages are layered on top of Java's AWT, the IFC is fully compatible with existing versions of the Java programming language (see Figure 56.1). Before Netscape finalized IFC version 1, the company made several important changes to the interface to ensure that the IFC is compatible with future versions of Java. NOTE: The IFC APIs and packages are bundled with Netscape Communicator and Navigator version 4.0 and later. This means that the IFC APIs are available to applets you want to run in your Netscape browsers. Because Netscape IFC is 100-percent Java based, the classes can be used with just about any Java-capable browser. If you want to use IFC classes with standalone applications, you can bundle the classes with the application, but you are subject to the terms of the Netscape ONE license. Specifically, you must sign the Netscape ONE license before you distribute IFC Java source code. You can find more information at this URL: `developer.netscape.com`. The IFC is designed to help developers create advanced applications in less time and with greater efficiency. The most important area in which the IFC comes to the developer's aid is the enhanced user interface. If you use this interface, your programs can perform many advanced tasks simply by calling methods of the interface objects. This means that you get the level of functionality you need with a limited amount of fuss and programming. Figure 56.1. Netscape's IFC is layered on top of Java's AWT. Behind the many predefined actions and operations of the interface are the interface components themselves. To be truly useful, components such as selection lists, popup windows, and dialog boxes should be fully customizable. It is no surprise, then, that all the components of the IFC are completely customizable. The modular design of the components also makes it easy to override their default look and feel, giving you advanced control with minimal coding. With IFC, even a simple button becomes an avenue to a multimedia feast without extensive coding. Stitch a few simple lines of code together and your IFC-enhanced buttons can have images, sounds, and animation associated with them. ## What's in the IFC? Netscape IFC is divided into two package libraries: `netscape.application` `netscape.uti` ### Overview of the netscape.application Package The `netscape.application` package supplies you with top-level classes for creating user interfaces of applications; it also provides services such as event handling and drawing. The complete list of classes, interfaces, and exceptions supported by `netscape.application` is shown in Table 56.1. #### Table 56.1. The netscape.application package summary.
Classes | Interfaces | Exception
`AWTCompatibility` | `ApplicationObserver` | `HTMLParsingException`
`AWTComponentView` | `DragDestination` |
`Alert` | `DragSource` |
`Application` | `DrawingSequenceOwner` |
`BezelBorder` | `EventFilter` |
`Bitmap` | `EventProcessor` |
`Border` | `ExtendedTarget` |
`Button` | `LayoutManager` |
`Color` | `ScrollBarOwner` |
`ColorChooser` | `Scrollable` |
`ColorWell` | `Target` |
`CommandEvent` | `TextFieldOwner` |
`ContainerView` | `TextFilter` |
`DebugGraphics` | `TextViewOwner` |
`DragSession` | `Window` |
`DragWell` | `WindowOwner` |
`DrawingSequence` |  |
`EmptyBorder` |  |
`Event` |  |
`EventLoop` |  |
`ExternalWindow` |  |
`FileChooser` |  |
`Font` |  |
`FontChooser` |  |
`FontMetrics` |  |
`FoundationApplet` |  |
`FoundationPanel` |  |
`Graphics` |  |
`GridLayout` |  |
`Image` |  |
`ImageAttachment` |  |
`ImageSequence` |  |
`InternalWindow` |  |
`InternalWindowBorder` |  |
`KeyEvent` |  |
`LineBorder` |  |
`ListItem` |  |
`ListView` |  |
`Menu` |  |
`MenuItem` |  |
`MouseEvent` |  |
`PackConstraints` |  |
`PackLayout` |  |
`Point` |  |
`Polygon` |  |
`Popup` |  |
`PopupItem` |  |
`Range` |  |
`Rect` |  |
`RootView` |  |
`ScrollBar` |  |
`ScrollGroup` |  |
`ScrollView` |  |
`Size` |  |
`Slider` |  |
`Sound` |  |
`TargetChain` |  |
`TextAttachment` |  |
`TextField` |  |
`TextParagraphFormat` |  |
`TextView` |  |
`Timer` |  |
`View` |  |
`WindowContentView` |  |
### Overview of the netscape.util Package The `netscape.util` package provides the necessary utility classes for the IFC, such as timers and archives. The complete list of classes, interfaces, and exceptions supported by `netscape.util` is shown in Table 56.2. #### Table 56.2. The netscape.util package summary.
Classes | Interfaces | Exceptions
`Archive` | `Codable` | `CodingExceptionInconsistencyException`
`Archiver` | `Comparable` | `DeserializationException`
`ClassInfo` | `Decoder` | `NoSuchElementException`
`ClassTable` | `Encoder` | `InconsistencyException`
`Deserializer` | `Enumeration` |
`FormattingSerializer` |  |
`Hashtable` |  |
`Serializer` |  |
`Sort` |  |
`Unarchiver` |  |
`Vector` |  |
### Overview of the IFC Object Framework Together, the `netscape.application` and `netscape.util` packages form a library with an extensive object framework. As Table 56.3 shows, you can find objects for interface components, windowing, animation, and much more. #### Table 56.3. Object framework in the IFC.
Framework | Description
Animation | Enables advanced animation with line art and images; includes methods for buffering, flicker control, and transparency.
Application Components | Provides key application components such as buttons, checkboxes, radio buttons, and text fields.
Composite Components | Creates complex interface components such as file choosers, font choosers, and color choosers.
Drag-and-Drop | Enables drag-and-drop features among components of an application.
Event Drawing | Provides essential event-handling methods and allows objects called by an event to draw themselves.
Multifont Text/Image Object | Allows for the display of multiple fonts in text elements; also allows for the display of embedded images.
Object Persistence | Allows objects to maintain their state so that they can be reused later to initialize an application with user preferences.
Timers | Enables multitasking without having to use multithreading.
Windowing | Allows you to use internal and external windows.
## Installing the IFC To compile and run Java programs that use IFC classes, you need a developer environment. The developer environment is found in the IFC SDK, which includes documentation, reference resources, and examples. You should install a developer environment on the system you plan to use for the development of IFC-enhanced Java programs. If you just want to run Java programs that use IFC classes, you need a user environment. The IFC user environment is included with Netscape Communicator version 4.0 and later. However, you can install the IFC on any system to which Java has been ported. All you need is Java, a Java-compatible browser, and the IFC user download. To obtain the IFC SDK or IFC user download, visit the Web page shown in Figure 56.2. The URL for the IFC Developer Central is `http://developer.netscape.com/library/ifc/../index.html`. Netscape archived the IFC files using several different compression formats (such as `zip`, `gzip`, and `tar`). Because the IFC is written completely in Java, you do not have to download different archive files for different systems. The sections that follow look at the installation process by platform. There are separate installation sections for UNIX, Windows 95, Windows NT, and Macintosh systems. After you read the setup procedures for your system, jump ahead to the section called "Creating IFC-Enhanced Applets and Applications." Keep in mind that the installation process is similar regardless of whether you plan to install a user environment or a developer environment. The key difference is that you use different filenames to retrieve and install the IFC archive file. Figure 56.2. Netscape's IFC Developer Central Web page. ### Quick Setup for UNIX The IFC archive file you probably want to use for UNIX systems is the one compressed using `gzip` and `tar`. After you obtain the IFC archive, check to make sure that the size of the file you downloaded matches the file size listed on the Web site. If the file sizes are not identical, your file may have been corrupted during download. In that case, delete the file from your system and download the IFC archive again. After you move the archive file to the base directory you want to use for installation (for example, `/home/users`), you can unzip and unpack the archive using the following command:
    gunzip ifc60.tar.gz | tar xf -
NOTE: The `gunzip` command lists the contents of the compressed files and directories. The output of this command is sent to the UNIX tape-archive command `tar`, which extracts the files and directories but does not list the extraction to the screen. Next, you have to let your system know where you installed the IFC class files. To do this, you use the `CLASSPATH` environment variable. `CLASSPATH` tells the Java interpreter and compiler where to look for your class files. When you installed Java, you probably assigned a value to the `CLASSPATH` variable. Do not remove this value; simply add another file path for the IFC classes. C shell users will want to edit the `.cshrc` file in their home directory and update the statement that sets the `CLASSPATH` variable. If you installed the IFC class files in `$HOME/ifc11/classes`, you can update the `CLASSPATH` variable as follows:
    setenv CLASSPATH java/classes:java/lib/classes.zip:$HOME/ifc11/classes
If the `CLASSPATH` variable is not set in your `.cshrc` file, enter only the path to the IFC files, as shown here:
    setenv CLASSPATH $HOME/ifc11/classes
For these changes to take effect in the current command-tool or shell environment, you must source your `.cshrc` file or enter the `setenv` command at the shell prompt. This action updates the current environment settings. If you are using the Bourne shell as your main UNIX shell, you can set the `CLASSPATH` in your `.profile` file. Do this by editing the `.profile` file in your home directory and updating the statements that set and export the `CLASSPATH` variable's path, as shown here:
    CLASSPATH=java/classes;java/lib/classes.zip;$HOME/ifc11/classes
    export CLASSPATH
For these changes to take effect in the current command-tool or shell environment, you must set the `CLASSPATH` variable and export it from the command line, like this:
    CLASSPATH=$HOME/ifc11/classes;export CLASSPATH
> NOTE: If you use the command-line Java tools in the current command tool, sourcing `.cshrc` or exporting `CLASSPATH` will work. However, if you want to run IFC applets outside the context of the current command tool, you should log out and then log back in. ### Quick Setup for Windows 95 The IFC archive file you probably want to use for Windows 95 systems is the one that is zipped. After you obtain the IFC archive, check to make sure that the size of the file you downloaded matches the file size listed on the Web site. If the file sizes are not identical, your file may have been corrupted during download. In that case, delete the file from your system and download the IFC archive again. To install the IFC from the MS-DOS prompt, follow these steps: 1. Open a DOS window. 2. Change to the directory that contains the IFC archive file. 3. Move the archive file to the location you want to install it. To install the IFC 1.1 in `c:\ifc11`, for example, use the following command:
>     move ifc60.zip c:\
4. Unzip the archive file with an unzip utility that supports long filenames. Be sure to extract the directory structure. After installing the IFC, update your `AUTOEXEC.BAT` file so that your computer can find the IFC class files. To do this, type the following at the DOS prompt:
    EDIT C:\AUTOEXEC.BAT
Now you have to let your system know where you installed the IFC class files. To do this, use the `CLASSPATH` environment variable. `CLASSPATH` tells the Java interpreter and compiler where to look for your class files. When you installed Java, you probably assigned a value to the `CLASSPATH` variable. Do not remove this value; simply add another file path for the IFC classes. If you installed the IFC under the `C:\` directory, update the line that sets the `CLASSPATH` variable as follows:
    SET CLASSPATH=C:\JAVA\LIB\CLASSES.ZIP;C:\ifc11\classes;
> When you are finished, save and close your `AUTOEXEC.BAT` file. If you reboot your computer, you ensure that the working environment is set for all future sessions. ### Quick Setup for Windows NT The IFC archive file you probably want to use for Windows NT systems is the one compressed using `zip`. After you obtain the IFC archive, check to make sure that the size of the file you downloaded matches the file size listed on the Web site. If the file sizes are not identical, your file may have been corrupted during download. In that case, delete the file from your system and download the IFC archive again. To install the IFC from the MS-DOS prompt, follow these steps: 1. Open a DOS window. 2. Change to the directory that contains the IFC archive file. 3. Move the archive file to the location you want to install it. To install the IFC 1.1 in `c:\ifc11`, for example, use the following command:
>     move ifc60.zip c:\
4. Unzip the archive file with an unzip utility that supports long filenames. Be sure to extract the directory structure. After installing the IFC, create a user environment variable called `CLASSPATH`. `CLASSPATH` tells the Java interpreter and compiler where to look for your class files. When you installed Java, you probably assigned a value to the `CLASSPATH` variable. Do not remove this value; simply add another file path for the IFC classes. If the `CLASSPATH` variable already exists on your system, double-click the System icon in the Control Panel, select the user environment variable, and then update it as follows:
    C:\JAVA\LIB\CLASSES.ZIP;C:\ifc11\classes;
If the `CLASSPATH` variable does not exist on your system, double-click the System icon in the Control Panel and create the user environment variable with the following value:
    C:\JAVA\LIB\CLASSES.ZIP;C:\ifc11\classes;
NOTE: If you did not install the IFC to the recommended location, you must enter the proper file path. ### Quick Setup for Macintosh The IFC archive file you probably want to use for Macintosh systems is the one that is zipped. After you obtain the IFC archive, check that the size of the file you downloaded matches the file size listed on the Web site. If the file sizes are not identical, your file may have been corrupted during download. In that case, delete the file from your system and download the IFC archive again. NOTE: Hopefully, Netscape has woken up by the time you read this and has realized that Mac users prefer Mac compression formats. If so, you should use StuffIt to decompress the Macintosh binary file and `DeHQX` or `BinHex4` to decompress the `hqx` file. If you used `Fetch` or `Anarchie` to download the files, the archive file was automatically decompressed when the download finished. If not, you can use StuffIt Expander or DropStuff with Expander Enhancer to decompress the `tar` or `zip` file. Before you can install the IFC, you must decompress the IFC archive file with an unzip utility. For IFC 1.1, this action creates a folder called `ifc11` under the installation folder. Next, create the following folder in the System folder: Preferences | Netscape | Java | `netscape-classes`. Then copy the contents of the `ifc11/classes` folder into the `netscape-classes` folder. If the copy is successful, the `netscape-classes` folders should have folders called `netscape | application` and `netscape | util`. NOTE: If you installed Netscape Communicator version 4.0 or later, these folders should already exist. In this case, all you have to do is check for the existence of the folders. ## Creating IFC-Enhanced Applets and Applications In Java 1.1, there is no way for system classes to look up an applet's class by name. This presents a slight problem for IFC developers and requires a workaround. The way you implement the workaround depends on whether you are developing a standalone application or an applet. NOTE: To compile IFC-enhanced programs, you must install the Java Development Kit and the IFC. If you have not already done so, do it now. To run IFC-enhanced programs, you must have the IFC and a Java-compatible browser. ### Working with Applications Applications are standalone programs that do not run within a browser. When you develop an application that uses the IFC, start by creating a separate folder for the application. In this folder, create a file called `NetscapeApplet.java` with the contents shown in Listing 56.1. You can also find this code on the CD-ROM that accompanies this book. #### Listing 56.1. NetscapeApplet.java source code.
    import netscape.application.*;
    import netscape.util.*;
    public class NetscapeApplet extends FoundationApplet {
        public Class classForName(String className)
            throws ClassNotFoundException {
            return Class.forName(className);
        }
    }
Then create the `.java` source files for your application in the same directory. Be sure that the main class is a subclass of the `Application` class and that you import `netscape.application` and `netscape.util`. Here are the statements that accomplish these goals:
    import netscape.application.*;
    import netscape.util.*;
    public class myApplication extends Application {
       //main class definitions here
    }
When you compile the application, compile all the `.java` files at the same time using the following command:
    javac *.java
You can now run your application using the Java interpreter. TIP: If the program does not compile, double-check the file paths set in the `CLASSPATH` variable. ### Working with Applets Applets are programs run within a browser. When you develop an applet that uses the IFC, start by following the steps outlined in the preceding section. If the applet compiles successfully, you can then create an HTML page to run the applet. Unlike traditional applets, IFC-enhanced applets are not executed by their class name. Instead, you execute the applet by invoking the `NetscapeApplet` class with a parameter called `ApplicationClass`. The `ApplicationClass` parameter tells the IFC the name of the main class file for your application:
    <APPLET CODE="NetscapeApplet">
    <PARAM NAME="ApplicationClass" VALUE="AppName">
    </APPLET>
In this syntax, AppName is the actual name of the main class file for the applet. ## Testing the Installation Listing 56.2 shows the code for a Web page with a script you can use to test the installation of the Netscape Internet Foundation Classes. This script uses the `releaseName()` function of `netscape.application.Application` to test for the presence of the IFC. Load this page in a browser on your development or user platform to test the installation of the IFC. If a problem occurs, you will see an error similar to the one shown in Figure 56.3. You can also find this code on the CD-ROM that accompanies this book. TIP: In the IFC check script, be sure to match the IFC version reference with the actual version you installed on your system. The script currently checks for IFC version 1.0. Keep in mind that the Netscape IFC is integrated into Netscape Navigator 4.0. This script is designed to be used with browsers other than Netscape Navigator 4.0. #### Listing 56.2. Check your installation with this script.
    <HTML>
    <HEAD>
    <TITLE>Check IFC Installation</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    browserName = navigator.appName
    window.onerror = errorMessage
    function errorMessage() {
       if (confirm("It appears you do not have the current version
       of Netscape IFC installed. \n\nWould you like to download the
       current release of IFC now?")) {
          window.open('http://developer.netscape.com/library/ifc/../index.html', '')
       }
       return true
    }function CheckIFC() {
       if (browserName.lastIndexOf('Netscape') != -1) {
          var app = netscape.application.Application
          var release = app.releaseName()
          if (release != "IFC 1.0") {
             errorMessage()
             return
       }
       window.confirm("Version: " + release + "\n\n
           of the IFC is properly installed on your system.");
       } else if (browserName.lastIndexOf('Microsoft Internet Explorer') != -1) {
          window.confirm("Verification does not work for Internet Explorer 3.0")
       } else {
          window.alert("Unable to determine if IFC has been installed properly.")   }
    }// -->
    </SCRIPT>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF">
    <DIV ALIGN="CENTER">
    <H1>Check Netscape IFC Installation</H1>
    <P ALIGN=LEFT>You can check the IFC installation by clicking on the
     button below. If the IFC <EM>is</EM> installed properly, you will
     see a message telling you what version you currently have installed
     on your system. If the IFC <EM>is not</EM> installed properly, you
     will see an error message and have the opportunity to download the
     IFC from the IFC Developer Central.</P>
    <FORM>
    <INPUT TYPE="button" VALUE="Check IFC Installation" onClick="CheckIFC()">
    </FORM>
    </DIV>
    </BODY>
    </HTML>
Figure 56.3. The IFC is not installed properly on this system. ## Summary The programmers at Netscape saw a window of opportunity to develop Java's user interface to its full potential--and they carried it 49 yards for a touchdown. As you have seen, the Netscape Internet Foundation Classes are not a replacement for Java classes; rather, the IFC is layered on top of Java's Abstract Windowing Toolkit and is designed to compliment it. To learn more about Netscape IFC, you may want to look for the Netscape ONE Developer's Guide (published by Sams Publishing). In this guide, you will find a detailed walk-through of all the frameworks in the IFC as well as applets and applications that use Netscape IFC components.
| ©Copyright, Macmillan Computer Publishing. All rights reserved.