 | Level: Intermediate Jeff Gunther (jeff.gunther@intalgent.com), General Manager, Intalgent Technologies
05 Jun 2003 With the Standard Widget Toolkit (SWT), you can develop a
stand-alone Java™
application that feels and operates like a native application. If you've spent any
time developing Java client-side applications for Windows®, you've probably wanted to
integrate some native Windows components into your applications. SWT, part of the
developing Eclipse Project, fulfills this need by giving you a way to easily leverage
and integrate ActiveX controls within a stand-alone SWT
application.
For years, the Java programming language has dominated the development landscape of
server-based applications. In the early days of the language's development, it was
touted as a solution for developing cross-platform desktop applications.
After end users and the
developer community demanded something more than the Abstract Window Toolkit (AWT),
Sun Microsystems introduced the Java Foundation Classes (JFC), also known as Swing,
to entice developers to reconsider the Java language for developing GUI
applications. While Swing does provide a mature and proven model for developing
client-side applications, it still misses the visual appeal and operation of native applications.
Before the development and introduction of the SWT,
industry pundits and developers alike had pronounced the Java language dead for
the development of desktop applications. SWT is beginning to breathe new life into
the opportunity of client-side Java by giving developers a framework to create
applications that mimic the full fidelity of native applications. Unlike its
predecessors (AWT and JFC, for example), SWT provides developers a
platform-independent API that is tightly integrated with the operating system's
native windowing environment.
While the Eclipse Project is traditionally thought as
of an open source integrated development environment (IDE) for Java technology, the project's
scope is much broader. On the front page of Eclipse's Web site, Eclipse is
described as "... a kind of universal tool platform -- an open extensible IDE for
anything and nothing in particular."
While Eclipse was used during the development of the sample application provided in
this article, we'll focus on demonstrating how to use ActiveX controls within the
context of a stand-alone application. For more information and background on the
Eclipse Project, see the Resources.
PDF viewer application
Before we review the code and implementation details of how to integrate ActiveX
controls into an SWT application, let's review the purpose and structure of the
included sample application. The application is a simple PDF viewer that allows users
to view and interact with a PDF file using Adobe's Acrobat Reader control. A user can
browse his hard drive, locate a file, and view PDF documents within an SWT
application, as shown in Figure 1.
Figure 1. Sample
application: PDF viewer running on Microsoft Windows XP
Environment requirements
You can download the sample PDF viewer application (see Resources). To test the PDF viewer application, your environment
must meet the following minimum requirements:
Although attention has been given to make the Ant build script cross-platform, the PDF
viewer application has been tested and verified only on Microsoft® Windows XP. Let's get
started by installing and setting up the sample application.
Installing and building the PDF viewer application
To install and build the PDF viewer application, complete the following:
- Download the source
code package.
- Unzip the
pdfviewer.zip file into a temporary directory.
- Execute the following command within the newly created directory:
ant clean.
- Execute the following command within the newly created directory:
ant.
If your environment meets the requirements and is properly configured, you should see
something similar to Listing 1
Listing 1. Successful build
Buildfile: build.xml
init:
[mkdir] Created dir: D:\PDFViewer\dist
compile-common:
compile-module:
[echo] Compiling ...
[mkdir] Created dir: D:\PDFViewer\build
[mkdir] Created dir: D:\PDFViewer\build\classes
[javac] Compiling 5 source files to D:\PDFViewer\build\classes
package-common:
[jar] Building jar: D:\PDFViewer\dist\pdfviewer.jar
default:
BUILD SUCCESSFUL
|
Using an ActiveX control
Now that we've covered the basics for the PDF viewer application, let's review how to
integrate and use an ActiveX control within an SWT application. Since our purpose here
is to cover ActiveX integration, we won't review how to create a stand-alone SWT
application or how to use ActiveX controls in Microsoft Windows.
Listing 2 outlines the basic structure of the PDFViewer class and demonstrates how an ActiveX control is
integrated into an SWT application.
Listing 2. PDFViewer
class
1 public class PDFViewer extendsApplicationWindow
2 {
3 private OleControlSite site;
4 private OleAutomation auto;
5
6 public PDFViewer()
7 {
8 super(null);
9 this.addMenuBar();
10 }
11
12 protected ControlcreateContents(Composite parent)
13 {
14 Shell shell = this.getShell();
15 shell.setText("PDFViewer");
16 shell.setSize(500, 450);
17
18 OleFrame frame = new OleFrame(shell, SWT.NONE);
19
20 try
21 {
22 site = new OleControlSite(frame,SWT.NONE, "PDF.PdfCtrl.5");
23 auto = new OleAutomation(site);
24
25 ...
|
Let's step through this code snippet:
- Whenever the application starts, a new instance of the
PDFViewer class is created.
- As shown on lines 3 and 4, we need to define two objects:
OleControlSite and OleAutomation. The
OleControlSite object variable handles the interaction
with the ActiveX control. The OleAutomation object
allows you to access properties and execute commands that are provided by the ActiveX control.
- On line 18, an
OleFrame object is defined. This object
is the container for the ActiveX control and manages the control's lifecycle.
- Line 22 instantiates the
site variable using two
arguments: the first argument contains a reference to an OleFrame, and the second argument contains a string that identifies
the ActiveX control. A program identifier, found within the Windows Registry,
uniquely identifies the control within Microsoft Windows.
- Line 23 instantiates the
site variable using a
reference to an OleControlSite variable.
Selecting a PDF document
Before the user can view a PDF document, he needs to select a PDF document
from his workstation. The code below illustrates the objects and methods necessary
to open a file dialog to the end user.
Listing 3. OpenAction class
0 ?
1 public class OpenAction extends Action
2 {
3 ApplicationWindow window;
4
5 public OpenAction(ApplicationWindow w)
6 {
7 window = w;
8 this.setText("&Open...");
9
10 this.setToolTipText("Open a PDF document.");
11 }
12
13 public void run()
14 {
15 FileDialog dialog = new FileDialog(window.getShell());
16 dialog.open();
17
18 String file = dialog.getFilterPath()+"\\"+dialog.getFileName();
19
20 PDFViewer viewer = ((PDFViewer)window);
21
22 viewer.loadFile(file);
23
24 }
25 } |
Let's review this code snippet:
- Whenever a user selects Open from a menu bar, the
run method on line 13 executes.
- As shown on lines 15 and 16, a
FileDialog object is
created. As displayed in Figure 2, this object displays a
native file dialog box to the user.
- Line 18 constructs a string that contains the location of the PDF document on the user's workstation.
- Line 20 casts the
window object to a PDFViewer object.
- Line 22 executes the
loadFile method and passes in the
location for the selected PDF document. We'll review the loadFile method in more detail in the next couple of sections.
Figure 2. The Open
File dialog
Listing an ActiveX's commands
Before we cover how the PDF document is displayed within the viewer application,
it's helpful to understand what commands are actually available within the Adobe
Acrobat Reader Active X (PDF.PdfCtrl.5) component. The
sample code package includes a class developed by IBM®. This class lists
all the public commands that an ActiveX control exposes.
To enumerate the commands for the PDF.PdfCtrl.5 control,
execute the following command within the PDFViewer directory: ant activex. If your environment is properly configured, you should
see something similar to Listing 4.
Listing 4. OpenAction class
...
[java]Functions for PDF.PdfCtrl.5 :
[java]METHOD (id = 2) :
[java] Signature : boolean LoadFile([] String fileName)
[java] Description : null
[java] Help File : null
[java]METHOD (id = 3) :
[java] Signature : void setShowToolbar([] boolean On)
[java] Description : null
[java] Help File : null
[java]METHOD (id = 4) :
[java] Signature : void gotoFirstPage()
[java] Description : null
[java] Help File : null
... |
To view the commands for any other ActiveX control, simply edit the build.xml file that is included with the source code and replace the
class's argument with some other ActiveX's program identifier. The Ant target below
illustrates the location to insert the control's program identifier.
Listing 5. Editing the build.xml file
...
<target name="activex" depends="package-common">
<java classname="Main" fork="true">
<jvmarg value="-Djava.library.path=${eclipse.dir}windows"/>
<classpath>
<pathelement location="${dist.dir}/pdfviewer.jar"/>
<fileset dir="${eclipse.dir}" includes="*.jar"/>
<fileset dir="${eclipse.dir}windows" includes="*.jar"/>
</classpath>
<arg value="<insert some program identifier>"/>
</java>
</target>
...
|
The first command that was output from the class was LoadFile. Let's explore how to
this command is utilized within the PDFViewer application.
Executing an ActiveX command
Once the user selects a PDF document, the PDFViewer
class needs to execute a command that it has exposed in the ActiveX control. The
code Listing 6 illustrates the objects and methods
necessary to have the ActiveX control load the user's selected PDF document.
Listing 6. PDFViewer class
0 ...
1 public void showPdfControl()
2 {
3 site.doVerb(OLE.OLEIVERB_SHOW);
4 }
5
6 public void loadFile(String file)
7 {
8 showPdfControl();
9
10 int[] rgdispid = auto.getIDsOfNames(newString[]{"LoadFile"});
11 int dispIdMember = rgdispid[0];
12
13 Variant[] rgvarg = new Variant[1];
14 rgvarg[0] = new Variant(file);
15
16 Variant pVarResult =auto.invoke(dispIdMember,rgvarg);
17 }
18...
|
Let's review this code snippet:
- After a user selects a PDF file, the
loadFile method on line 6 is executed.
- Line 8 calls the
showPdfControl method. Before an
ActiveX control is visible within the application, the control must be activated
using the doVerb command.
- Lines 10 through 16 invoke the
LoadFile command within
the ActiveX control.
Running the PDF viewer application
To test out and run the PDF viewer application, complete the following steps:
- Execute the following command within the directory where you unpacked the source
code:
ant run.
- Upon execution of the Ant script, the PDF viewer application should appear.
- Select Open from the File menu to select a PDF file from your workstation.
The selected PDF document is rendered.
Summary
The SWT library gives Java developers a powerful mechanism to integrate Microsoft
Windows ActiveX controls into highly interactive client-side applications. With SWT's
support for ActiveX controls and OLE documents, it's exciting to explore ways to create
great-looking applications that integrate native widgets and components.
Download | Name | Size | Download method |
|---|
| os-activex/pdfviewer.zip | | HTTP |
Resources Learn
Get products and technologies
About the author  | |  | Jeff Gunther is the General Manager and founder of Intalgent Technologies, an emerging provider of software products and solutions using the Java 2 Enterprise Edition and Lotus Notes/Domino platforms. Jeff has been a part of the Internet industry since its early, "pre-Mosaic" days. He has professional experience in all aspects of the software life cycle including specific software development expertise with Java/J2EE, DHTML, XML/XSLT, database design, and handheld devices. You can contact him at jeff.gunther@intalgent.com
|
Rate this page
|  |