Sei sulla pagina 1di 7

LIKE OPEN SOURCE FORMS ADF SWING APPLICATIONS WITH JDEVELOPER 11G

Sten Vesterli, Scott/Tiger A/S


Oracle Forms is at heart a client/server development tool. Even though a Forms application can be deployed in a three-tier architecture, it remains a two-tier application as seen from the developer. This means that the only necessary architectural decision is whether to place code on the database or in the client. With Java, on the other hand, a developer can build true multi-tier applications. And for each tier, there are several options. Because JDeveloper supports all these options, JDeveloper is a much more complicated tool than Forms Builder. This paper explains how to build an ADF Swing application based on existing tables. This is the easiest way to build an application with a rich user interface like that offered by Oracle Forms, even though it is different from the approach in most of Oracle's developer guides and online material.

An Emp/Dept ADF Swing application


All ADF applications consist of a middle tier (the model) and a user interface tier (the view). Oracle Forms does not make such a split visible to you as the developer. To create the demo Java Swing applications, choose Java Desktop Application (ADF) when creating an application. This will create two projects: One for the model (the ADF components) and one for the client.

Creating ADF components


To create all the ADF business components you need, choose File > New, Business Tier > ADF Business Components > Business Components From Tables. Entity Objects First, you create Entity Objects. These map directly to tables and take care of some of the necessary conversions for you to access relational data from Java objects. You can add validation logic in the Entity Objects quite a few validations are declarative and don't need any code at all. The wizard will also create Associations corresponding to the foreign key relationships between your tables. View Objects You then create View Objects. These represent groupings of data you need in your application. A View Object can gather data from several Entity Objects, and data from an Entity Object can be used in multiple View Objects. View Objects exist as updateable or as read-only, and are created in two separate steps in the wizard. Naturally, there is additional overhead in creating an updateable view object, so you should use read-only View Objects whenever possible. The wizard will also create default View Links to reflect the associations between the underlying Entity Objects. Application Modules Lastly, you create Application Modules. These gather together the View Objects you have created, and there is generally one Application Module per screen in your application. An application module can be executed from within JDeveloper with the built-in test application (right-click, Run). This allows you to browse and change actual data in the underlying tables.

Creating the User Interface


The ADF components are the foundation of an application, but do not determine the client technology. Oracle is mainly pushing ADF Faces, which is a web browser based approach, but I find that an ADF Swing application is more like Forms and an easier migration for Forms developers.

www.odtug.com

ODTUG Kaleidoscope 2008

Like Open Source Forms-ADF

Vesterli

A wizard-built user interface To create a user interface with the absolute minimum of effort, you choose your View project in the application and choose File > New > Client Tier > ADF Swing > Form (not Empty Form). This will start the ADF Swing Form wizard as shown in Figure 1.

Figure 1. The ADF Swing Form Wizard


This wizard allows you to define a complete application. While this is not the way you would go about building production applications, and you may not understand all the steps, using this wizard to build an application will give you a running application to examine.

Nothing is fixed
If youve built a single-table application on DEPT using the ADF Swing Form Wizard, youll get something like FormDeptView1.java and PanelDeptView1.java in the application navigator. JDeveloper contains has a so-called structure pane (by default in the lower half of the left column of the screen) showing the structure of the currently selected object see figure 2. Note the two tabs Source and Design at the bottom of the pane these allow you to select to see the structure of your Java code or the structure of your user interface.

www.odtug.com

ODTUG Kaleidoscope 2008

Like Open Source Forms-ADF

Vesterli

Figure 2. The Structure Pane showing Design


Notice that the UI consist of panels that in turn can contain other panels (think of stacked canvases in Forms). But whereas you in Forms would place an item at the exact x,y coordinates where you wanted it, in Java, you delegate the exact layout to a Layout Manager. You see from Figure 2 that each panel can have its own layout manager. In Forms, items stay where you put them no matter if the user resizes the window or change the screen resolution. But in Java, your Layout Manager will decide where to put your items, according to specific rule and properties. This allows the application to handle different screen sizes flexibly, but can be very complex. There are many different layouts, each with their own properties. Fortunately, most database applications can manage with just one of them: The FormLayout (you see it used for the dataPanel in Figure 2). This is a grid-type layout, where components are placed in rows and columns, and can span multiple columns if desired. If you click on an item on your screen, youll find that even though it looks like you can just grab a handle and pull it, the Layout Manager still enforces the defined rules so your item might snap back where it was. Fortunately, there is a better way using the Constraints pane (see Figure 3).

www.odtug.com

ODTUG Kaleidoscope 2008

Like Open Source Forms-ADF

Vesterli

Figure 3. The Constraints Pane for a component


When you have an item selected, you can examine the rules that control its location in the constraints pane. Here, you can change position, span, etc. If you are familiar with HTML tables, youll find the Form Layout similar. If you need to change the grid itself, right-click in the grid. Then choose to add or delete rows or columns from the context menu.

What to do where
In Oracle Forms, there was not always a clear distinction between data and presentation. In some places, these were separate (LOVs and their underlying Record Groups), but in other places they were mixed together (POST-QUERY triggers to populate lookup items). In an ADF Swing application, this distinction is explicit: You have a data model layer (in the Model project) and the actual user interface (in the View project), and you need to place your code in the right place. Data validation should be done in the Entity Objects. To add validation, double-click on an entity object and choose Validators. Then either right-click on an attribute or on the Entity node to add an entity (row) level validation. See Figure 4. This allows you to define your rules in one place so that they take effect no matter which view object access data.

www.odtug.com

ODTUG Kaleidoscope 2008

Like Open Source Forms-ADF

Vesterli

Figure 4. Adding a Validator


Lookup items should be included in the View Objects. There is no need to code POST-QUERY style lookups. To include the department name from DEPT in an EMP-based View objects, simply choose Entity Objects on the left and add the Dept entity object. See Figure 5.

Figure 5. Adding an extra Entity Object to a View Object


JDeveloper will already have discovered the foreign key relationship if you look under Query, you will find that the join is added automatically. Under Attributes, you can click Add from Entity to add attributes from the just added Dept Entity Object You define the value set for a list of values (corresponding to a Forms record group) as a View Object in the Model project User Interface actions must be coded in the user interface (the View project).

Everything is code
When you look at the code that JDeveloper has produced, you will notice that there is both a Design and a Source tab at the bottom of the window. This different from Forms:

www.odtug.com

ODTUG Kaleidoscope 2008

Like Open Source Forms-ADF

Vesterli

In Forms, an application contained code and a layout, stored in the FMB file in some proprietary format. At runtime, the Forms Runtime would interpret the FMB/FMX file and produce the output.

In Java, an application only consists of code. At runtime, the code is executed, and every item in the application is an object created by code. If youve built a single-table application on DEPT using the ADF Swing Form Wizard, youll have something like FormDeptView1.java and PanelDeptView1.java. If you choose the Source tab in the Structure pane, you can see a graphical overview of the source code JDeveloper has built for you. See Figure 6.

Figure 6. The Structure Pane showing Source


If you choose the Source tab in the main window, you can see the source code that builds this application. Some of the stuff that defines the layout is unintelligible to anybody but a Java Swing expert. For example, in PanelDeptView1, you find stuff like:
private FormLayout panelLayout = new FormLayout("3dlu,d:g,3dlu,d:g,3dlu", "3dlu,d,3dlu,d,3dlu,d,3dlu"); dataPanel.add(mDeptno, new CellConstraints("4, 2 ,1,1,default," + CellConstraints.FILL));

This is why you should use dialog boxes like the Constraints pane to control your forms layout Other parts are clearly legible, at least once you get the hang of how to read Java. For example, the first part of the code that build a dialog box to prompt the user to save a pending transaction:
if (panelBinding.isTransactionDirty()) { JButton commitButton = new JButton("Commit"); commitButton.setMnemonic('C'); JButton rollBackButton = new JButton("Rollback"); rollBackButton.setMnemonic('R'); Object[] options = { commitButton, rollBackButton }; final JOptionPane optionPane = new JOptionPane("How do you want to close the transaction?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null, options, options[0]);

www.odtug.com

ODTUG Kaleidoscope 2008

Like Open Source Forms-ADF

Vesterli

This is a lot more power than was available in Forms. In Forms, the Forms Runtime itself was a black box, and if you wanted anything that the Forms Runtime didn't offer, you were out of luck (until the advent of Pluggable Java Components, anyway). In Java, your user interface is built as the application runs. This means that you can change everything you want.

Seeing is believing
If you wish to try your hand building ADF Swing applications, you can simply step through the wizards as described above. OTN has a number of JDeveloper ADF tutorials and demos there is only a little on ADF Swing, but the material on ADF middle tier components is useful as well. You can also find a couple of demos showing the development of a simple EMP/DEPT ADF Swing application on www.vesterli.com.

www.odtug.com

ODTUG Kaleidoscope 2008

Potrebbero piacerti anche