Previous Page TOC Next Page



Appendix D


Visual J++


Visual J++ is the newest addition to Microsoft's series of Visual Development Tools. Though its name may not exactly have pleased some Java aficionados, the performance of the Visual J++ compiler has certainly brought Visual J++ in the limelight within the Java community. In this appendix we shall have a high-level overview of Visual J++, attempting to address basic as well as advanced issues.

Introduction


Java is a platform-independent, object-oriented, multithreaded and secure programming language. Java programs are compiled into a series of byte codes that are not machine specific. These byte codes are interpreted and executed within the Java Virtual Machine (VM) on a specific platform. The Java VM specification can be implemented on any platform, thus enabling the same Java programs to run on any platform. Microsoft Internet Explorer 3.0 provides a Java VM under 32-bit Windows platforms. Internet Explorer also includes a Just-In-Time (JIT) compiler which compiles the byte codes into machine specific code achieving much faster execution of Java applets.

Java programs are typically developed in one of two forms:

  1. 1. Applets: These are embedded in Web pages using the <APPLET> tag, and are downloaded and run by the browser on the user's local machine.
  2. 2. Applications: These are standalone executables which can be executed independently of the browser.

Java ushers in a new paradigm for software development, and offers a vast potential than just developing fancy applets for use in Web pages. Java is similar to C++, and much more powerful than Visual Basic. It doesn't support multiple inheritance and pointer arithmetic, and offers a clean and robust environment including garbage collection and exception handling features.

ActiveX is a technology based on the Component Object Model (COM) foundation. COM is a foundation for component software, that specifies interfaces between component objects within an application or between applications, within a network or across networks (with Distributed COM, or DCOM).

There are innumerable software components supporting COM in the form of ActiveX Controls, Object Linking and Embedding (OLE) Automation Servers, which have been developed using popular tools like Visual C++, Visual Basic, and Borland's Delphi.

Using Visual J++, you can develop Java applets and programs that can make use of these software components. OLE Automation Objects such as those included within Microsoft Office and Lotus Notes, can be driven from within Java programs. High speed database access can be achieved using Data Access Objects (DAO) and Remote Data Objects (RDO).

You can also develop COM objects in Java, including ActiveX Controls. These Java programs can then be called locally or even remotely by other COM objects, which may be developed in a different programming language like C++. With built-in garbage collection and the ready functionality provided by the Microsoft Java VM implementation, Java makes COM programming simpler in several ways.

Thus Visual J++ gives developers a way to integrate the existing technologies with Java to make powerful Internet and Intranet applications, rather than forsake the ready functionality provided by widely available ActiveX software components.

Visual J++ Environment


Microsoft Visual J++ 1.0 is a development environment for creating Java applications. Visual J++ uses the Developer Studio interface familiar to Visual C++ developers. Developer Studio has a highly customizable interface with toolbars and dockable windows that can be moved, resized, hidden and arranged according to your preferences.

Your development work is focused in a Project Workspace which maintains the target options, build settings, resources and source files that comprise your project. The Project Workspace View lets you switch between three views:

Figure D.1 shows Visual J++ with the Introduction to Visual J++ InfoView Topic displayed.

Figure D.1. The Visual J++ environment.

Visual J++ comes with an Applet Wizard which provides a quick and easy way to create Java applets with pre-built support for multithreading, animation and event handling. The Applet Wizard can automatically create Java applets which can also run as standalone applications.

Let's develop a simple Applet using the Applet Wizard, before we look at other Visual J++ features. Later we shall have a glimpse of how we can pass information from an ActiveX Control to a Java applet embedded in the same HTML page.

Developing a simple applet using Applet Wizard


While you are in the Developer Studio, select File|New from the File menu and Developer Studio pops up a dialog box as shown in Figure D.2.

Figure D.2. The File New dialog box.

Select Project Workspace and press OK.

The New Project Workspace Dialog appears as shown in Figure D.3.

Figure D.3. The New Project Workspace dialog box.

Select Java Applet Wizard from the Type list and type myApplet in the Name text box. You can see the Java Virtual Machine as the Platform already selected for you. You can select the location of your project files by entering a valid pathname in the Location TextBox or by selecting an appropriate location using the Browse button. After you are done, press Create. This starts the Applet Wizard Steps sequence.

The Applet Wizard Step 1 is shown in Figure D.4.

Figure D.4. Applet Wizard Step 1.

You can make your applet support execution as a standalone application. The Applet Wizard can add standalone application functionality by providing an implementation for a main method within the applet derived class. The main method implemented in the applet derived class is ignored by a browser, whereas is the controlling point of execution for a standalone Java application. That is not all, however. Standalone applications do not by themselves have a graphical interface as applets in an HTML document. The Applet Wizard adds a graphical interface to the standalone implementation by adding a class derived from the Java Frame class. The code to handle these different states of execution is automatically provided by the Applet Wizard.

Note that myApplet defaults as the name of the desired class, and choose whether you wish to have explanatory and TODO comments added to the source code generated by the Applet Wizard. Click Next to proceed to Step 2.

You can request a sample HTML file with your embedded applet in Step 2 of the Applet Wizard as shown in Figure D.5.

Figure D.5. Applet Wizard Step 2.

Applet Wizard lets you specify an initial size for your applet. It adds an appropriate resize() call in the initialization method for the applet. Select the height to be 320 and width to be 100 pixels. Click Next to proceed to Step 3.

Step 3 offers powerful features that can be added to your applet including multithreading, animation, and event-handling support as shown in Figure D.6.

Figure D.6. Applet Wizard Step 3.

Applet Wizard can create a multithreaded applet which defines a Thread object in your applet class, initializing it during applet initialization. The code for starting and stopping the Thread object is automatically added. If you choose support for animation, Applet Wizard supplies default images for the animation which you can replace with your own. You can also create skeleton event handlers for events you wish to respond to, by selecting them from the list at the bottom.

It was never as easy to create a multithreaded Java Animation applet!

For our sample applet, we shall a use a simple multithreaded applet without animation and event handlers.

What's ahead? Click Next to go to Step 4 which appears as shown in Figure D.7.

Figure D.7. Applet Wizard Step 4.

An HTML author can pass parameters to the applet embedded inside the HTML page using <PARAM> tags in the HTML document. Applets can retrieve the parameter values using the getParameter() method of the Applet class. Applet Wizard automates this process by letting you define the applet parameters before it creates the applet.

Enter Message as the Name and Message to display as the Description of the parameter by clicking in the appropriate fields. Observe that Applet Wizard automatically assigns the String Type to the parameter and a member variable m_Message to hold the parameter value.

Applet Wizard automatically generates code to retrieve the parameter value in the m_Message private variable. It also provides an implementation for the getParameterInfo() method, where the Description field specified in Step 4 is returned by the method.

Click the Next button to go to Step 5 of the Wizard. This last step is where you add general information about the applet, as shown in Figure D.8.

Figure D.8. Applet Wizard Step 5.

The applet's getAppInfo() method returns the information specified here. This is where you declare to the world that you are the author of this applet, and the legal statements would usually follow.

When you click Finish, the Applet Wizard gives a summary outline of all choices you have opted for during the Wizard step sequence as shown in Figure D.9.

Figure D.9. Applet Wizard summary.

Click OK to generate your applet source file and a sample HTML file in the directory you specified. Developer Studio also generates the files for the Project Workspace and the Build and Compiler settings.

You can switch to Class View to see the myApplet class. Expand the myApplet class by clicking on the plus(+) sign next to it, and you can see a list of the methods and member variables in the myApplet class. Right click on the myApplet class and select Go To Definition from the pop-up menu. The Java source file myApplet.java opens in the source editor. Figure D.10 shows the Visual J++ environment with the Project Workspace View in Class View mode and the myApplet.java source file opened in the source editor on the right.

Figure D.10. Visual J++ Project Workspace Class View and Source Editor.

We shall add a public member variable message of type String to the myApplet class, which will contain a message that is displayed by the applet. Then we shall embed an ActiveX TextBox Control and a HTML button within our sample HTML page. When the HTML button is pressed, we shall retrieve the text in the ActiveX TextBox Control and pass it to our myApplet Java applet. Before we see how this is done, let us develop myApplet to display text from a String variable.

Right click on the myApplet class in Class View and select Add Member Variable. The Add Variable Dialog appears as shown in Figure D.11.

Figure 11. The Add Variable dialog box.

Enter String as the variable type, message as the variable name and select public from the drop-down list of Access Modifiers. As you type in the values and select the scope of the variable, the Add Variable Dialog shows the full declaration that will be added to our source file. Clicking OK adds the variable message with the following declaration in the myApplet class:


public String message;

Next, go to the definition of the myApplet paint() method by right-clicking on it and selecting Go To Definition in the Class View expanded list. Comment or delete the default g.drawString statement supplied by Applet Wizard and enter the following Java source code:


// myApplet Paint Handler

//-------------------------------------

 public void paint(Graphics g)

 {

    g.clearRect(0,0,319,99);

    g.drawRect(0,0,319,99);

    g.drawString(message,5,15);

 }

This is a simple paint() handler that clears the applet region, draws a rectangle and then displays the String in the message variable within the rectangle.

Now lets add a public method for updating the applet area when the button on the HTML page is clicked.

From the right-click pop-up menu of the myApplet class in Class View, select Add Method to bring the Add Method Dialog shown in Figure D.12.

Figure D.12. The Add Method dialog box.

Specify a return type of void for the public method named ShowMessage. Once again, observe the full declaration to be inserted in the source file at the bottom of the dialog.

You see the new method added to the list of methods under myApplet in the Class View. Add the following statement to the body of the method:


public void ShowMessage()

{

   repaint();

}

Add the following statement to the init() method to initialize our message variable to the value passed as a parameter:


message = m_Message;

Finally, comment out or remove the repaint() call in the run() method to prevent continuous updating of the applet area.

Save the file via File|Save from the File menu, and select Build myApplet from the Build menu. The Output window appears and displays the progress of the compilation.

Open the myApplet.html file and add "Default String" to the value attribute of the <PARAM> tag so that the applet tag is as shown below:


<applet

    code=myApplet.class

    id=myApplet

    width=320

    height=100 >

    <param name=Message value="Default String">

</applet>

Save the myApplet.html file by selecting File|Save from the File menu. Now you can run our applet by selecting Execute myApplet from the Build menu. Visual J++ fires up Internet Explorer 3.0 with the myApplet.html page displaying our myApplet Java applet.

Java Applet and ActiveX Control sample


You can open myApplet.html in ActiveX Control Pad to insert the ActiveX TextBox Control. For a detailed discussion of ActiveX Control Pad, refer to Chapter 11, "Using ActiveX Control Pad." Alternatively, you can add the following text directly in the myApplet.html file:


<OBJECT ID="TextBox1" WIDTH=292 HEIGHT=30

     CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002F3">

        <PARAM NAME="VariousPropertyBits" VALUE="746604571">

        <PARAM NAME="Size" VALUE="6165;635">

        <PARAM NAME="Value" VALUE="Type your message here">

        <PARAM NAME="FontCharSet" VALUE="0">

        <PARAM NAME="FontPitchAndFamily" VALUE="2">

        <PARAM NAME="FontWeight" VALUE="0">

    </OBJECT>

Next, add a HTML Form button to myApplet.html like this:


<INPUT  TYPE=button VALUE="Show Message" NAME="ShowButton">

The connectivity between the Java applet and the ActiveX Control is achieved using VBScript. We shall add an event handler for the HTML button using VBScript. Add the following VBScript code to the myApplet.html file:


<SCRIPT language="VBScript">

<!--

 Sub ShowButton_OnClick

   document.myApplet.message = TextBox1.text

   document.myApplet.ShowMessage

 End sub

-->

</SCRIPT>

The Java applet is referenced from within VBScript by preceding the applet ID with the 'document.' prefix. The above script sets the message public member variable of myApplet to the text of the TextBox1 ActiveX Control. It then calls the public method ShowMessage of myApplet to repaint the applet region on the HTML page.

The final HTML listing should now look like Listing D.1.

Listing D.1. The final myApplet.html listing.


<html>

<SCRIPT language="VBScript">

<!--

 Sub ShowButton_OnClick

   document.myApplet.message = TextBox1.text

   document.myApplet.ShowMessage

 End sub

-->

</SCRIPT>

<head>

<title>myApplet</title>

</head>

<body>

<hr>

<applet

    code=myApplet.class

    id=myApplet

    width=320

    height=100 >

    <param name=Message value="Default String">

</applet>

<P>

<INPUT  TYPE=button VALUE="Show Message" NAME="ShowButton">

<OBJECT ID="TextBox1" WIDTH=292 HEIGHT=30

     CLASSID="CLSID:8BD21D10-EC42-11CE-9E0D-00AA006002F3">

        <PARAM NAME="VariousPropertyBits" VALUE="746604571">

        <PARAM NAME="Size" VALUE="6165;635">

        <PARAM NAME="Value" VALUE="Type your message here">

        <PARAM NAME="FontCharSet" VALUE="0">

        <PARAM NAME="FontPitchAndFamily" VALUE="2">

        <PARAM NAME="FontWeight" VALUE="0">

    </OBJECT>

<hr>

<a href="myApplet.java">The source.</a>

</body>

</html>

Save the myApplet.html file and view it in Internet Explorer by either selecting Execute myApplet from the Build menu or pressing the Refresh button of the browser if it is still running. Enter any text into the ActiveX TextBox Control and click the ShowMessage button. The Java applet displays the text in response!

Figure D.13 shows the output in Internet Explorer with some sample text.

Figure D.13. Output of HTML page with Java applet and ActiveX Control.

Visual J++ Power Programming


Now that we have had a glimpse of the exciting potential of integration of Java applets with ActiveX Controls, here's a brief look at some features of Visual J++, which can put you in the forefront of the Java development momentum.

Visual J++ comes with a Resource Editor for editing resources such as menus, dialogs, toolbars and icons. You can design new resources from scratch or reuse ones you have previously developed for your Visual C++ projects. Resources are saved as templates which are then used in your Java programs using the Resource Wizard. Resources used in Java are limited to using features of the controls and components supported by the Java Abstract Window Toolkit (AWT).

The Resource Wizard is a very powerful tool to convert resource templates into compiled Java classes which can then be imported for use in your Java programs.

The Visual Debugger is a rich environment for debugging Java programs with disassembly, stepping and breakpoint support at the byte code level. You can watch variables, track changes with automatic highlighting, and change values while debugging. DataTips show the values of variables or expressions on which you linger the mouse pointer. You can also debug multiple applets running in the browser simultaneously in the Debugger.

Figure D.14 shows the myApplet Java applet being debugged in the Visual Debugger.

Figure D.14. Visual Debugging of myApplet with byte code disassembly.

The figure shows the byte code disassembly in the main window, the Variables window at the bottom left and the Watch window at the bottom right. You can control execution from the Debug Toolbar. You can even Step Out of a method!

Source-Code Control with Visual SourceSafe is integrated within Developer Studio. You can work seamlessly with your Visual SourceSafe Database as the SourceSafe commands are available from right within the Visual J++ development environment.

The Graphics Editor lets you create, edit and save GIF and JPEG graphic files for use with your Java programs or HTML pages. It comes with an assortment of painting tools on a palette much like those in standard painting programs.

Remote Data Objects (RDO) and Data Access Objects (DAO) provide high-speed database access to your Java applications. RDO enables you to create and control components on a remote database. Thus Visual J++ gives you instant access to most existing databases using the ubiquitous Open Database Connectivity (ODBC) interface.

Third Party Products shipped along with the Visual J++ 1.0 CD include: the Java Generic Library (JGL) from ObjectSpace Inc., the premier Java library for algorithms and container libraries; Liquid Motion from DimensionX; Jamba from Aimtech Corporation, tools for multimedia-enabling Web sites; and Mail Wizard from Neural Applications Corporation for adding Internet mailing capabilities to your Java applications.

Using Java with Component Object Model (COM) Software Objects


The most exciting aspect of the Java Virtual Machine implementation and the Visual J++ compiler is the symbiotic nature of two technologies that have evolved independently of each other. We have seen how Java applets can be scripted by VBScript, and how the Java VM in conjunction with VBScript can enable ActiveX Controls to communicate with a Java applet.

The Java Virtual Machine exposes a Java applet as an ActiveX Control to VBScript and other COM Objects. This means that Java applets can be used by any application that makes use of ActiveX Controls such as Visual Basic, Delphi, Access and PowerBuilder applications.

What about the other way around?

Using ActiveX Controls and COM Objects from Java


It turns out that you can use an ActiveX Control from within a Java applet that resides on the same HTML page. The HTML file can pass the ActiveX Control as a parameter to the Java applet. However, the COM interfaces exposed by ActiveX Controls are not useable by Java programs directly. The COM objects and interfaces that are described in the Object's Type Library have to be converted to Java classes that enable Java programs to access them as if they were Java classes.

Visual J++ comes with the Java Type Library Wizard which automates this process. The Type Library Wizard lets you create Java classes from the type library information for various COM objects stored in the registry. Type Libraries store information about the programming model of a COM component such as its classes, interfaces etc. In order to use those COM objects in Java, the type library information is converted into Java classes, which can then be imported and used by Java programs. These special class files are distinguished from normal Java class files by a special tag recognized by the Java VM.

The Type Library Wizard invoked from the Tools menu is shown in Figure D.15.

Figure D.15. The Visual J++ Java Type Library Wizard.

The Type Library Wizard lists all the Type Libraries installed on your system with a blank check box beside each. You can select the Type Library that describes the COM class you wish to use in your Java program. When you click OK, the Type Library Wizard generates Java Class files which act as wrappers for the COM classes.

If you are aware of the security restrictions placed on downloadable Java applets, you must be wondering how a Java applet can make a direct call into an ActiveX Control or a COM Object. Java applets run within a restricted environment that isolates it from the system to prevent any harm. A Java applet using a COM Object's functionality accesses resources outside this restricted environment. The security issues are resolved by distinguishing all COM classes as either trusted or untrusted.

Classes loaded from the local CLASSPATH or digitally signed are considered as trusted and are permitted to use COM services. All other classes such as those downloaded from a network, are considered untrusted and are restricted to their secure execution environment, referred to as a sandbox. If you wish to create trusted applets which can access COM services, you have to package your classes in a digitally signed .CAB (cabinet) file. The Cabinet Development Kit and the Code Signing Kit are included with Visual J++.

If you are using COM objects from Java, the OLE Object View tool can come especially handy.

The OLE Object View Tool is a powerful utility which lets you inspect the details of the OLE objects installed on your system. Developers of OLE/COM objects can use the OLE Object View Tool to test their OLE Objects. If you are using a programmable control or Automation Server from your Java programs, OLE Object View can display method attributes and parameter names for all the OLE Automation Servers installed on your system. This proves indispensable if you do not have comprehensive or accurate documentation of the interfaces exposed by the OLE Server. Figure D.16 shows the main screen of the OLE Object View Tool. (The OLE Object View Output may vary from system to system depending on which OLE Objects are installed on a particular system).

Figure D.16. OLE Object View Tool main screen.

The OLE 2.0 Objects installed on your system are displayed on the list at the left, with different icons distinguishing OLE Objects with differing functionality such as OLE Automation Servers, OLE Controls, insertable and container objects, Type Libraries, etc. The OLE Object View also includes a Type Library Browser shown in Figure D.17.

Figure D.17. OLE Object View Type Library browser.

If you develop a Java applet using COM services on an HTML page, you have to ensure that the user's version of Internet Explorer fully supports Java and COM integration. You should specify the following tag on the HTML page containing your applet:


<OBJECT

CLASSID="clsid:08B0E5C0-4FCB-11CF-AAA5-00401C608500"

CODEBASE="http://www.microsoft.com/java/IE30Java.cab#Version=1,0,0,1">

</OBJECT>

If the Java support in the version of Internet Explorer is outdated, Internet Explorer automatically downloads and installs the updated version.

Developing COM Objects in Java


The Java VM facilitates development of COM objects in Java since it automatically provides an implementation of the base COM Interface IUnknown. Reference counting, a mechanism with which an object keeps track of how many clients are using it, is automatically handled by the garbage collection in Java. This makes development of COM Objects in Java easier in several respects in contrast with C++. We shall have a brief overview of the steps required to implement a COM object in Java. You can find comprehensive details in Visual J++ Books Online.

Developing COM Objects in Java requires the following steps:

  1. 1. Describing the interfaces exposed by the COM class in Object Description Language (ODL).
  2. 2. Using MkTypLib or the Microsoft Interface Definition Language (MIDL) compiler to generate a Type Library from the ODL
  3. 3. Using JavaTLB command-line Tool to generate Java class wrapper files from the Type Library
  4. 4. Implementing the Java Class in .java source files
  5. 5. Using the JavaReg tool to register the Java class as a COM class. The JavaReg tool creates special registry entries for COM objects implemented in Java. This ensures that when COM services implemented in Java are requested, the Java VM can be launched to load the appropriate Java class.

Note that the COM client is not aware of the language in which the particular COM service is implemented. Also note that if you distribute your COM object developed in Java, you need to provide a way to register your COM class on the user's system.

Conclusion


Visual J++ 1.0 is an impressive environment in its first version. It is the only development environment today that lets you take advantage of existing ActiveX Components and offers a rich set of features that make your Java development efforts highly productive and efficient.

Previous Page Page Top TOC Next Page