Chapter 7 Developing Applications with PowerJ and EAServer


About the development process

What you can build

This chapter describes scenarios for developing the following kinds of applications:

Applets, applications, components, and Web server extensions There are several types of Java programs: applets, applications, components, and Web server extensions. PowerJ generates behind-the-scenes code for each of these types so that you don't have to write as much code for the mechanics of the program. You can concentrate on program-specific code.

Applets An applet is a Java program that requires a host program, such as a browser, to run. An applet is usually part of an HTML page and is downloaded when it is needed. You don't have to deploy applets to individual computers. Because they are downloaded as needed, applets should be small so users don't get impatient waiting for the applet to start.

Because applets are downloaded, they are subject to restrictions so they can't harm the user's system. For example, applets cannot access the local file system and can make connections only back to the server they came from.

Your applet class will extend the standard class java.applet.Applet, which provides default implementations for the init, start, stop, and paint methods. In your applet source file, PowerJ generates code to override the init method.

Applications A Java application behaves like any other program. In Java terminology, an application is a Java program that does not require a host server or browser to run. The user sees windows and menus and interacts with controls. The application can connect with a middle-tier or database server.

Since applications must be locally installed rather than downloaded each time they are run, they can be larger than applets. The user's class path environment variable must include the directories containing the application's code files.

A standalone application has a main method that runs when the application starts. It includes a main form and can include other forms, including dialog boxes and frames with menus.

Components Components are standardized, reusable pieces of software that are hosted in another program. You can install them in servers that are designed to host components like Jaguar, or include them in Java applications.

Business-logic components consist of methods that implement business rules and other application logic. These components can be included in a client application, but are more typically hosted in a component server like Jaguar in a distributed application.

User-interface components are used in client applications to enhance the user interface. Typically, user-interface components extend standard user interface classes. For example, a custom list box component might provide custom sorting methods, or a text box or check box might have data awareness.

In PowerJ, you can add components to the component palette and include them in your applications. You can put components on your forms, customize them, and use their methods through the Reference Card and drag-and-drop programming. For business-logic components on a Jaguar server, when you add a component to PowerJ, a proxy for it is added to the palette. You can then use distributed components just as you would local components.

In PowerJ, it's easy to create JavaBeans components. A Bean might be an encapsulated user-interface component, such as an enhanced button with custom functionality, or it might be a business-logic component that includes methods and events. You can use the class, method, property, and event wizards to create the Java classes for the component. You can also write code that allows the JavaBeans user to modify properties at design time. There are specific standards and conventions for creating Beans.

Web server extensions You can create Web server extensions that follow the Java Servlet API. You can deploy servlets in Jaguar CTS or other Web servers that support Java Servlets. For Web servers that do not directly support the Servlet API, PowerJ provides a DLL that translates from CGI, NSAPI, or ISAPI to the Servlet API.

How you build it

You use PowerJ to build most of the pieces of the applications described here. Some of the activities for building a Java application are listed below. Depending on which application architecture you choose, some of these activities may apply to creating the client and others to creating the server.

  1. Create a workspace, then create a target, which is the type of program you want to build. A workspace can include several targets, where each target is a part of a larger application.
  2. Create the user interface by creating one or more forms and adding controls and nonvisual components.
  3. Optionally, add menus by creating a frame (a type of form), adding a MenuBar object to the frame, and using the Menu editor to design menus.
  4. Optionally, access database data by adding transaction and query objects to the form. Make the visual components of the form bound to the query object.
  5. Code application-processing logic. You can place this code in different locations:
    The PowerJ Reference Card and drag-and-drop programming make it easy to look up classes, properties, and methods and insert appropriate code in your program.

Creating workspaces, targets, and classes

What you do

First, create a workspace. A workspace contains targets, and targets contain forms and other classes.

Targets

A target is an application, applet, class, or collection that you create with PowerJ. Types of targets include:

You can run a target program anytime during a PowerJ session. PowerJ builds the target by compiling PowerJ files into Java class files and displaying the program's user interface. Depending on the target type and your current run options, your application may be displayed in the applet viewer, in a Web browser, or as a standalone program.

Not all targets can be run. For example, you cannot run a set of Java class files that make up a class library, because it is not a complete application.

A WebApplication target can be used to tie other targets and files together into a single manageable package. It lets you organize, maintain, and publish all the files of an application for an intranet or the Internet.Similarly, archive file targets (ZIP, JAR, and CAB) can be used to collect other targets and files into a single archive file.

Each target has its own folder; this avoids naming conflicts when source files have the same name.

Workspaces

All work in PowerJ is done within a workspace, which is a collection of one or more targets. Your workspace can include all the targets involved in the complete application.

PowerJ creates a workspace definition file for a workspace. The file is a summary of the workspace and lists all the targets that belong to the workspace.

The Workspace view displays the targets in the current workspace. A target's Build Options property sheet lets you specify options for building the target.


Classes

Each Java file in the target is a Java class. You can expand the targets in the left pane of the Workspace view to show classes as well as the properties, methods, and data members of individual classes. A pop-up menu lets you add new properties, methods, and events. In the right pane, you can further define your classes and attributes, and view and edit Java code.


Writing code

From the Workspace view, you can use the code page or open the code editor for a class or an individual method or event. In the editor options, you can choose to view all the code for the class or one method at a time. You can hide or view the code that PowerJ generates.


Designing the user interface

You create the user interface in a PowerJ form. A form is an application window on which you place buttons, text boxes, and other elements of the user interface.


Any program with a graphical user interface has a main form, which is the first window displayed by the application. When you create an applet or application target, a main form is created for you.

Your application may require additional forms, which you can add by selecting File | New from the menu bar or clicking the corresponding toolbar button.

Construct the user interface by selecting components on the component palette and dropping them onto the form. In the code editor for the class, you write code for component events.


Designing menus

You add menus to your application with a MenuBar object. A MenuBar object represents all the menus displayed by the form. MenuBar objects can be added only to forms that are based on the Frame class.

To add a menu to your application, you create a form whose type is Frame and drop a MenuBar object onto the form. You can add individual menus and menu items in the Menu Editor, accessed by selecting Edit Menu from the form's pop-up menu.


Accessing data

In PowerJ, you can use the transaction object to simplify connecting to many types of data sources. You can use either the DataWindow, Java Edition (also known as the DataWindow JavaBeans component) or the query object for SQL statements and data retrieval. You use the query object if you want to use data-bound controls instead of the DataWindow. Both the DataWindow JavaBeans component and the query object can use the transaction object, or they can process result sets without a database connection.

The Database component palette includes icons for adding transaction and query objects to a form. You create instances of the Transaction and Query classes by selecting them in the palette and dropping their icons onto a form.

To display retrieved data, you can use either data-bound controls with the query object, or the DataWindow JavaBeans component. On the Standard component palette, many of the visual controls (such as check boxes, text boxes, and labels) can be connected to a data source and column so that they display the data in the current row of a query object's result set. You make the connection to the query on the Database page of the control's property sheet.

Coding application logic

What you do

You can add your application logic in event handlers, methods, or external classes. In choosing where to add this code, keep in mind that effective encapsulation will make your classes more reliable and reusable. Don't try to accomplish everything in a single method.

Events

At design time, PowerJ makes it easy for you to write code that gets executed when events are triggered. PowerJ takes care of the infrastructure for events, such as event sources and listeners. You just write event-handler code that you want to be run when the event occurs.

If you want to set up a new event handler in your code, then your code must set up the event infrastructure.

Methods

When you add a new method, PowerJ inserts the method declaration and opens the code window so you can write the method's code.

When writing code, you can use the PowerJ Reference Card to look up methods and properties of classes and components and then insert the method calls into your code.

Classes

In Java, a class provides the definition of an object; an object is an instantiation of a class. An application with a user interface includes forms, which are classes with specific support for the design environment. You can also have classes that are not forms. For these classes, you work in the Workspace view and code editor. You add methods to the class to contain the application logic.

You can add a class to a target and use the Workspace view to add properties, methods, and events to the class. This is called a managed class because PowerJ has records of the functions it contains.

You can also add a Java source file to a target. You can edit the source in the code editor, but the Workspace view does not display the methods and properties of the class. The extension for a source file is .java.

PowerJ uses an enhanced file format for saving forms and managed classes. Files for forms have the extension .wxf; files for managed classes have the extension .wxc. When you build your application, PowerJ generates Java source files (with the extension .java) that are then compiled into binary files (with the extension .class). The binary files contain bytecodes that can be interpreted by a Java VM. These files are usually called class files.

 


Copyright © 2000 Sybase, Inc. All rights reserved.