Scripting the techexplorer Hypermedia Browser

Creating a Java LaTeX editor applet


The feature described is only available in the Professional Edition of techexplorer.

Both Internet Explorer and Netscape provide different ways of accessing the functionality of web browser objects such as plug-ins, ActiveX controls, Java applets, and JavaScript. From Netscape's point of view, web browser objects are wrapped by the netscape.javascript.JSObject class. Consequently, wrapped objects can be accessed through the methods defined by netscape.javascript.JSObject. Internet Explorer provides the com.ms.wfc.html packeg for accessing Dynamic HTML.

Recall from the Getting Started section that web browsers create an object hierarchy for every HTML document. Java applets, JavaScript and techexplorer can interact by using properties defined by the various web browser objects. Java applets for Netscape can use the netscape.javascript.JSObject class to obtain handles to these objects while Java applets for Internet Explorer can use com.ms.wfc.html. Indeed, the methods defined by these classes allows Java applets to use the functionality of both JavaScript, techexplorer and other web objects. To facilitate the development of cross web browser web applications that make use of the techexplorer plug-in and ActiveX control, we provide the ibm.techexplorer.util.DhObject and ibm.techexplorer.techexplorer classes to encapsulate web browser differences.

In this section we detail how this is accomplished by stepping through the development of a Java Latex editor. The Java Latex editor will make use of the techexplorer plug-in when invoked by Netscape and the ActiveX control when embedded in Internet Explorer. Next, we discuss the process of compiling and executing Java applets that use techexplorer. We conclude by letting you test drive the Java Latex editor.

The Java LaTeX editor example explained

The Java Editor applet uses the ibm.techexplorer.techexplorer and ibm.techexplorer.util.DhObject classes to communicate with techexplorer and the web browser environment. Let's start by examining the following code fragment:

import java.awt.*;
import java.applet.Applet;

import ibm.techexplorer.techexplorer;
import ibm.techexplorer.control.techexplorerControl;
import ibm.techexplorer.axtchexp.AxTchExpRaw;
import ibm.techexplorer.util.DhObject;

public class Editor extends Applet {
   
   TextArea sourceLaTeXArea;
   Button   sendDocumentBtn,getDocumentBtn,aboutBtn;

   private techexplorer techexplorer = null;
   private DhObject dHTML = null;

   public void setPlugin( techexplorer obj )
   {
       if ( obj instanceof techexplorer )
           techexplorer = obj;
	   dHTML = new DhObject((Applet) this);
   }

   public void setControl( AxTchExpRaw obj, Object dhtmlobj )
   {
       if ( obj instanceof AxTchExpRaw )
           techexplorer = new techexplorerControl( obj );
	   dHTML = new DhObject(dhtmlobj);
   }

   public void init(){
       ...
   }

   public void start() {      
   }

   public boolean action(Event e, Object arg){
       ...
   }
The import statement makes the java.awt.*, java.applet.Applet, ibm.techexplorer.techexplorer, ibm.techexplorer.control.techexplorerControl, ibm.techexplorer.axtchexp.AxTchExpRaw and ibm.techexplorer.util.DhObject Java classes available to the current class under an abbreviated name. Note that the * (star) form of the import statement makes all classes in a package available by their class name.

Java applets, unlike Java applications, do not have a main entry point. Instead, a Java applet overrides several methods defined by the Applet Java class. The Editor applet overrides the start and init methods, called when an applet becomes visible, in order to initialize the GUI.

In order for a Java applet to comunicate with techexplorer, the applet must first obtain a handle to a ibm.techexplorer.techexplorer object that will provide access to a partiuclar instanace of a techexplorer document. Java applets can access the web browser object model by using an instance of ibm.techexplorer.util.DhObject. In this example, JavaScript is used to initialize these variables in a cross web browser way. In particular, setPlugin is called when the applet is running in Netscape and setControl for Internet Explorer. The following JavaScipt and HTML code will properly initialize the Editor applet when the document is loaded.

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
    function begin()
    {
        var app = document.Editor;

        if ( document.EditorPlugin )
            app.setPlugin( document.EditorPlugin );
        if ( document.EditorControl )
            app.setControl( document.EditorControl, this );
    }
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" ONLOAD="begin()">
    ...

<OBJECT NAME="EditorControl"
    CLASSID="clsid:5AFAB315-AD87-11D3-98BB-002035EFB1A4"
    WIDTH=400 HEIGHT=100>
    <PARAM NAME="DataType" VALUE="0">
    <PARAM NAME="Data" VALUE="\pagecolor{white}\Large\color{black}IBM techexplorer\newline Technical Publishing for the Internet">

    <EMBED NAME="EditorPlugin"
        TYPE="application/x-techexplorer"
        TEXDATA="\pagecolor{white}\Large\color{black}IBM techexplorer\newline Technical Publishing for the Internet"
        WIDTH=400 HEIGHT=100 PLUGINSPAGE="http://www.software.ibm.com/techexplorer/" >
        <NOEMBED> 
            <P>IBM techexplorer Hypermedia Browser not installed! Please visit the
            <A HREF="http://www.software.ibm.com/techexplorer/">techexplorer home page</A>
            for more information.
        </NOEMBED>
    </EMBED>
</OBJECT>

    ...

<APPLET NAME="Editor" 
  ARCHIVE="NpExamples.jar"
  CODE="Editor.class" CODEBASE="Java"
  WIDTH=406 HEIGHT=220 MAYSCRIPT>
  <PARAM NAME="cabbase" VALUE="AxExamples.cab">
</APPLET>

    ...
</BODY>
</HTML>

Note that the ONLOAD attribute on the BODY tag specifies that the begin() method should be called when the page has been loaded. The begin() method will call the appropriate Editor initialization method based on whether the techexplorer plug-in or ActiveX control is being used. The <OBJECT> tag will ensure that the ActiveX control version of techexplorer will be used when the HTML page is loaded in Internet Explorer. Using an <EMBED> tag inside the <OBJECT> tag will ensure that the techexplorer plug-in is invoked when the HTML page is loaded in Netscape.

Now that the Editor applet can access techexplorer and the web browser environment, we can examine the implementations of the init() and action() member functions.

...
public class Editor extends Applet {
   ...

   public void init() {
      GridBagLayout gridbag = new GridBagLayout();
      GridBagConstraints c = new GridBagConstraints();
      setLayout(gridbag);

      sourceLaTeXArea = new TextArea(  );
      c.gridwidth = 2; c.fill = GridBagConstraints.BOTH; 
      gridbag.setConstraints(sourceLaTeXArea,c); 
      add(sourceLaTeXArea);

      sendDocumentBtn = new Button();
      sendDocumentBtn.setLabel("Click to send document");
      c.gridwidth = 1; c.weightx = 1; c.gridy = 1; c.gridx = 0;
      gridbag.setConstraints(sendDocumentBtn,c);
      add( sendDocumentBtn );

      getDocumentBtn = new Button();
      getDocumentBtn.setLabel("Click to get document");
      c.gridy = 1; c.gridx = 1;
      gridbag.setConstraints(getDocumentBtn,c);
      add( getDocumentBtn );

      aboutBtn = new Button();
      aboutBtn.setLabel("About ...");
      c.gridwidth = 2; c.weightx = 1; c.gridy = 2; c.gridx = 0;
      gridbag.setConstraints(aboutBtn,c);
      add( aboutBtn );
   }

   public void start() {      
   }

   public boolean action(Event e, Object arg) {
     if (e.target == sendDocumentBtn) 
        techexplorer.reloadFromTeXString( sourceLaTeXArea.getText() );
     if (e.target == getDocumentBtn) 
        sourceLaTeXArea.setText( (String) techexplorer.getTeXString() );
     if (e.target == aboutBtn)
     {
         Object args[] = new Object[1];
         args[0] = new String( "Java LaTeX Text Editor 1.0" );
         dHTML.call("alert", args);
     }
     return true;
   }

The init() function is called when the applet is first loaded into the browser and is used to perform the initialization needed by the Editor class. Notice that we retain references to the Java text area and buttons for use in the action() method. The Editor applet uses the action() method to respond to button clicks.

If the source of the click is sendDocumentBtn, the techexplorer display is refreshed with the contents of the sourceLaTeXArea text area. The techexplorer display area is refreshed by calling the techexplorer.reloadFromTeXString() function.

If the getDocumentBtn is clicked, the contents of the sourceLaTeXArea text area are replaced with the LaTeX source that is retrieved by the techexplorer.getTeXString() function. Notice that both the techexplorer.reloadFromTeXString() and techexplorer.getTeXString() functions operate on the techexplorer instance specified in the NAME attribute of the <EMBED> or <OBJECT> tag as appropriate.

When the aboutBtn is clicked, the cross browser Dynamic HTML object (ibm.techexplorer.util.DhObject) is used to call the JavaScript alert() function to display a text string.

Click here for the whole program. Since some browsers do not support Java 1.1, we've made the Editor applet Java 1.0 compliant.

Compiling techexplorer enabled Java Applets for Netscape and Internet Explorer

Since techexplorer provides cross web browser classes, the same Java code can be used to create applets that interact with techexplorer for both Netscape and Internet Explorer. For cross browser compatibility, each Java applet that makes use of techexplorer should be compiled and distributed in two archives. The Java jar archive format can be used for Netscape and the Windows cab format for Internet Explorer. The <APPLET> tag can then be used to specify both the Netscape jar and Internet Explorer cab files as described below.

<APPLET NAME="YourJavaAppletName" 
  ARCHIVE="YourJavaAppletName.jar"
  CODE="YourJavaAppletName.class" 
  WIDTH=100 HEIGHT=100 MAYSCRIPT>
  <PARAM NAME="cabbase" VALUE="YourJavaAppletName.cab">
</APPLET>

Note that before a Java Applet can interact with JavaScript, the MAYSCRIPT attribute must be included in the <APPLET> tag. This is a security precaution to assure that the Web page author explicitly grants JavaScript connectivity permission to the Java applet.

Netscape

The java compiler must know where to find the definition of the Netscape and techexplorer classes. The corresponding class definitions are located under the Netscape directory. On Windows 95/NT and on UNIX systems, the path to the Netscape directory depends on where you choose to install Navigator.

Netscape Communicator 4.0 stores its class files in java40.jar. The Professional Edition of techexplorer archives its plug-in class files in NpTchExp.zip. On Windows 95/NT, the paths for these files are usually:

C:\Program Files\Netscape\Communicator\Program\Java\Classes\java40.jar
C:\Program Files\Netscape\Communicator\Program\Plugins\NpTchExp.zip
When using the SUN Microsystems Java Development Kit for compiling Java code, the CLASSPATH environment variable specifies a list of directories and zip files that the compiler uses for class definitions. Alternatively, the -classpath option can be used as a javac compiler setting to explicitly set a particular path as follows:
javac -classpath ".;C:\Program Files\Netscape\Communicator\Program\Java\Classes\java40.jar; 
                    C:\Program Files\Netscape\Communicator\Program\Plugins\NpTchExp.zip" 
                    YourJavaAppletName.java
A Java archive file can be created as follows:
jar -cvf YourJavaAppletName.jar YourJavaAppletName.class

Internet Explorer

IBM techexplorer enabled Java applets that interact with Internet Explorer must be compiled with the Microsoft SDK for Java. In addition, the the ActiveX control version of techexplorer must be installed with the appropriate Java class files. Internet Explorer stores all the installed Java classes in:

C:\WINDOWS\DOWNLOADED PROGRAM FILES\

The Professional Edition of techexplorer will automatically install the Java class files unless this option was declined during the installation process.

When using Microsoft SDK for Java for compiling Java code, there is no need to specify the CLASSPATH. The jvc compiler will look for registered Java classes in the registry. To compile your Java applet with any additional class definitions use the following:

jvc /cp:p <ADDITIONAL PATH> YourJavaAppletName.java
A Java cabinet file can be created as follows:
cabarc N AxYourJavaAppletName.cab YourJavaAppletName.class
In order for Internet Explorer to recognize you applet, you need to create a test certificate and sign the cabinet file. This can be done as follows:
makecert -sk AxYourJavaAppletName -r -n "CN=YOUR COMAPNY" AxYourJavaAppletName.cer
cert2spc AxYourJavaAppletName.cer AxYourJavaAppletName.spc
signcode -j JavaSign.dll -jp Low -spc AxYourJavaAppletName.spc -k AxYourJavaAppletName AxYourJavaAppletName.cab

Try out the Java LaTeX applet!

<P>IBM techexplorer Hypermedia Browser not installed! Please visit the <A HREF="http://www.software.ibm.com/techexplorer/">techexplorer home page</A> for more information.


Click here to to view the previous section. Click here to to view the next section.

IBM techexplorer Hypermedia Browser is a trademark of the IBM Corporation. Send comments and questions to techexpl@us.ibm.com.