Scripting the techexplorer Hypermedia Browser

Creating a LaTeX editor via a Java 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 Java class. Consequently, wrapped objects can be accessed through the methods defined by netscape.javascript.JSObject. Internet Explorer provides the com.ms.wfc.html Java package 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 allow Java applets to use the functionality of both JavaScript and techexplorer plus the functionality of other web objects. To facilitate the development of cross-browser web applications that make use of the techexplorer plug-in and the 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 statements make 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 that are called by the browser environment when an applet becomes visible. init is called only once when the applet is first invoked and is used much like a constructor. start is called automatically after the method has completed execution. It is also called anytime the HTML page is redrawn, for example, after the page is redisplayed after being hidden. This implies that the start method can be called repeatedly, whereas init is only called once.

In order for a Java applet to communicate with techexplorer, the applet must first obtain a handle to an ibm.techexplorer.techexplorer object that will provide access to a particular instance 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-browser way. In particular, setPlugin is called when the applet is running in Netscape and setControl for Internet Explorer. The following JavaScript 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 of the BODY tag specifies that the JavaScript 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 text is sent from the editor to the techexplorer display. That is, the techexplorer display is refreshed with the contents of the sourceLaTeXArea text area. The techexplorer.reloadFromTeXString() method refreshes the display area.

If the getDocumentBtn is clicked, text is loaded into the editor from the techexplorer display area. In this case, the content of the sourceLaTeXArea text area is 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 by using the Java 1.0 method names and event handling mechanism.

Compiling techexplorer enabled Java Applets for Netscape and Internet Explorer

Since techexplorer provides cross-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/98/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/98/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 javac tool 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 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 an applet, a test certificate must be created and the cabinet file signed. This can be done as follows:
makecert -sk AxYourJavaAppletName -r -n "CN=YOUR COMPANY" 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.
Visit the official techexplorer home page at http://www.software.ibm.com/techexplorer/.