3.3 How to Create a GUI-based Browser

This JNDI solution describes how to create the functionality of a GUI-based browser that accepts commands from an input stream and executes these commands. A demo NSIBrowser utility, with accompanying source code, is provided to show the functionality of this browser solution.

3.3.1 Creating a GUI Browser

The GUI Browser implementation consists of two major components (NSIBrowser class and NSIBrowserFrame class) with other supporting interfaces and classes.

NSIBrowser Class

The NSIBrowser class contains the main method for the NSI Browser (GUI) application. Sample source code is provided by the NSIBrowser.java file. The primary purpose of the NSIBrowser class is to construct and start up the browser frame. The specific tasks included in this class are as follows:

  1. Sets up the configuration file (NSIBrowser.ini) information and property names (initial context, host, list classes and list bindings).
  2. Sets up the starting main method used for the standalone NSIBrowser application. The main method takes as input the command line arguments and initializes an NSIBrowser object (browser). It then calls the browser.init method.
  3. The browser.init method is a required part of an applet and is responsible for constructing and displaying the program’s window (frame). It first initializes a mainFrame object from the NSIBrowserFrame object, and then it sets the size of the browser mainFrame. It then checks for a NSIbrowser configuration file by calling the readConfigFile method, following which the NSIBrowser properties and initial context are set in the mainFrame.
  4. The readConfigFile method checks for an NSI browser configuration file and uses it, if it exists, by reading in the configuration file information and updating the system properties with any changes needed (only previously undefined properties). It merges the configuration file with the system properties and checks to make sure the initial context property is set.
  5. A saveConfigFile method is provided for saving the updated configuration information (system properties) to the configuration file.

NSIBrowserFrame Class

The NSIBrowserFrame class provides for displaying, processing, and updating the program's one or more browser window(s) and its components. Sample source code is provided by the NSIBrowserFrame.java file. The components of the main window include the following:

  • Title Bar - Displays the application’s name and version.
  • Menu Bar - Provides structure of main- and sub-menus.
  • Current Path Area - Shows the current path.
  • Name List Area - Shows the object available at the current path.
  • Initial Context Area - Displays the initial context setting.

The NSIBrowser constructor creates the browser’s main window (frame) and all of its components. In addition to the main window components listed above, the constructor does the following:

  • Constructs and adds the file menu.
  • Constructs and adds the search menu.
  • Constructs and adds the options menu.
  • Constructs and adds the help menu.
  • Sets the window’s menu bar to the one just constructed.
  • Constructs and adds the name list display area.
  • Adds the current path line.
  • Adds the status line.
  • Makes sure notification is given for window events.
  • Eliminates wasted space before the frame gets displayed by calling a pack method.

The following methods are provided for processing and updating the program's browser windows.

actionPerformed Method

The actionPerformed(ActionEvent) method is the action handler for the main window (frame). It handles any menu item selections, including closing the window and terminating the application, and it handles event buttons and events in the listbox.

WindowListener functions

The following WindowListener methods are implemented:

  • The windowClosed(WindowEvent) method closes the current window.
  • The windowDeiconified (WindowEvent) method deiconifies the current window.
  • The windowIconified (WindowEvent) method iconifies the current window.
  • The windowActivated (WindowEvent) method activates the current window.
  • The windowDeactivated (WindowEvent) method deactivates the current window.
  • The windowOpened (WindowEvent) method opens the current window.
  • The windowClosing (WindowEvent) method closes the current window.

setProperties Method

The setProperties(Properties) method set the properties for the browser frame based on the Properties parameter input.

updateList Method

The updateList method updates the name list component using the initial context and the current name using the initCtx and currName variables.

resetInitCtx Method

The resetInitCtx method updates the initial context variable (initCtx) using the new initial context as specified in the system properties.

showAttributes Method

The showAttributes method displays the attributes (if any) for the currently selected item.

lookupContext Method

The lookupContext method looks up a context and begins a new browser frame for that context.

startNewContextFrame Method

The startNewContextFrame(String, Context) method starts a new browser frame for a given context based on the input parameters (title name and starting context).

callSpecialHandler Method

The callSpecialHandler method handles ’special’ button requests. A ’special’ request indicates that the user wants to interact with the ’real’ object. Since this can happen in so many different ways, a list of property entries is used to find a special handler for the object selected, based on its class name. This allows anyone interested to extend this browser to do just about anything.

Other Supporting Interfaces and Classes

The NSIBrowser class and the NSIBrowserFrame class use the following additional interfaces and classes:

  • FileSpecialHandler class - implements the NSISpecialHandler class to create a special handler for a FileDirContext object. This interface is used only by the NSIBrowserFrame class.
  • NSIAttributeFrame class - is responsible for displaying a list of attributes for a given DirContext. This interface is used only by the NSIBrowserFrame class.
  • NSIForm class - provides a common way for entry forms to be created. Neither the NSIBrowser class nor the NSIBrowserFrame class uses this class.
  • NSIInitialContextFrame class - handles setting the initial context parameter for JNDI. This interface is used by both the NSIBrowser class and the NSIBrowserFrame class.
  • NSILookupFrame class - handles looking up an object for JNDI. This interface is used only by the NSIBrowserFrame class.
  • NSIMessageBox class - displays a dialog box with a title, optional message and OK button. This interface is used only by the NSIBrowserFrame class.
  • NSISpecialHandler class - defines an interface for special handlers. A special handler is designed to handle the specifics of a particular object type. This object type is defined by object class and the binding is stored as a property in the browser’s configuration file or system properties. This interface is used only by the NSIBrowserFrame class.
  • QuickSort class - sorts a partial or an entire array. This interface is used only by the NSIBrowserFrame class.
  • Sortable class - sorts two objects input as parameters. This interface is used only by the NSIBrowserFrame class.
  • SortableString class - sorts two strings input as parameters. This interface is used only by the NSIBrowserFrame class.
  • NSIBrowser.ini file - Contains the initial environment settings. This configuration file can be edited to use JNDI providers other than those supplied by Novell by setting the environment key 'java.naming.factory.initial' to the class name of the desired initial context factory.

3.3.2 Executing the NSIBrowser Utility

NSIBrowser is a GUI-based browser that allows you to explore JNDI providers. It is designed for users who have a basic knowledge of the JNDI Context and DirContext interfaces. Basic JNDI Context and DirContext methods can be called. Some possible uses for NSIBrowser are

  • As an exploratory tool to discover how different JNDI providers work.
  • As a debugging tool, allowing you to save debugging time by providing calls to basic NJCL Context and DirContext methods. To test your code, run NSIBrowser and execute the command you desire.

To run the NSIBrowser demo utility, follow the procedures below.

Setting up NSIBrowser Locally

  1. Go to the c:\novell\Ndk\samples\njcl_sample\nsibrowser directory (default download).
  2. Compile NSIBrowser by entering the following command on the command line:
       javac *.java
       
  3. Run NSIBrowser by entering the following command on the command line:
       java NSIBrowser
       

Implementing Your Own Special Handlers

  1. Create a class that implements the NSISpecialHandler interface.
  2. Add the handler to the NSIBrowser.ini file using the following syntax:
       jnos.browser.handler.<classname>=<handler>
       

    where <classname> is the full class name of the class you want to handle and <handler> is the full class name of your custom handler to which you want to bind the class. For example, the FileSpecialHandler looks like this (although it is hard coded into the sample):

       jnos.browser.handler.com.novell.service.file.nw.naming.fileDirContext=FileSpecial Handler
       

Changing the Initial Environment

Edit the shell.ini file to change the initial environment. To use the JNDI providers other than those supplied by Novell, set the environment key 'java.naming.factory.initial' to the class name of the desired initial context factory.

3.3.3 NSIBrowser Source Code

These samples demonstrate how to create the functionality of a GUI-based browser that accepts commands from an input stream and executes these commands.