Thursday, March 17, 2011

How to use a calendar in a Form

Introduction

How to implement a Calendar object in an Oracle Form is a fairly common question in the Oracle Technology Network Forms Forum. There are a couple of very nice demo’s available on the Internet as well as Oracle’s own demo in the Forms Code Samples. However, because it is getting harder and harder to find the demo’s on Oracle’s site, I decided to write this document.

Disclaimer

I want to be very clear – I did not write either of these calendars. I am simply writing this document as a way of making information that is available in separate documents available in a single document. Please check the references section for information on where I obtained the source code for these calendars.

Between the two solutions (Forms versus Java) I prefer the Forms version solely because it is a little faster (based solely on my experience) than the Java version and the Java version does not conform to the look and feel of a Forms application.

How this document is organized

This document will take you step-by-step through implementing both a Forms based calendar (adapted from Oracle’s supplied demo’s) as well as a Java based calendar (see Resources at the end of this document).
I will list the steps to use the Forms calendar first and then list the step to use the Java version second. This demo is organized such that there are two separate Date items in the Form; the first using the Forms Calendar and the second using the Java calendar. This allows you to judge for yourself which one is more suitable for your needs.
This document will walk you step-by-step through the creation of an Oracle Form that implements both calendars – even though I have included all the source code.
Forms Version

This demo was written using Oracle Developers Suite 10g R1 (10.1.2.0.2) with no patches applied. This makes it usable by any version of Forms 10g.

Objects included in this demo

CALENDAR.olb
FJCalendar.jar
jcalendar-1.3.2.jar
looks-2.0.1.jar
FRM_JAVA_CALENDAR.fmb
How to implement a Calendar object in an Oracle Form.doc
(See the Calendar.zip file in the attachments section below)
Java Requirements!

The Java Runtime Environment (JRE) needed to run this demo is the Sun JRE version 1.5.0_22, but it is highly recommended that you use the latest Sun JRE (currently 1.6.0_22). If you want to configure Forms Builder to use the Sun JRE, but don’t know how too, see my article: "Forms How to – Configure Forms Builder to use Sun JRE.”

If you need a version of the Java calendar that runs with the Oracle Jinitiator please the references section for the URL where I got the Java calendar.

Setup

Copy the supplied CALENDAR.olb to a directory listed in your FORMS_PATH.
The .jar files listed above must be copied to the \forms\server\java folder and you must modify the default.env CLASSPATH to include the full path to the FJCalendar.jar.
(For example: C:\DevSuiteHome10g\forms\java\FJCalendar.jar)
The .jar file must be listed in the ARCHIVE parameter in the formsweb.cfg.
(For example: archive=frmall.jar,FJCalendar.jar
Let’s get started!

First, you will need to have the Oracle Developer Suite 10g R2 (10.1.2.0.2) installed and configured to run forms locally. If you are unable to run a form from the Forms Builder – then you will need an application server available where you can deploy and test your form. I prefer running a Form from the Forms Builder as it allows me to incrementally develop a form as well as run the Forms Debugger if I run into trouble with a particular section of code. This document assumes you have completed the necessary configuration to run a form from the Forms Builder. If you need assistance configuring Forms Builder to run forms locally, see my article: “"Forms: How to Configure Forms Builder to run forms locally.”

Step-by-Step

Start the Forms Builder and create a new Forms Module.
Log into a database – doesn’t matter which database because the form we’re creating does not have any base table objects.
Open the CALENDAR.olb and double-click on the CALENDAR node in the Object Libraries.

This will open the CALENDAR object library pane.
From the CALENDAR object library pane, click on CALENDAR and drag and drop it on the Object Groups node of your new form.

You will be prompted to Subclass, Copy or cancel the operation. It is recommended that you subclass the objects, but you can copy them if you prefer.
If you subclass – the CALENDAR.olb must be located in a directory listed in your FORMS_PATH (as mentioned above).
Once copied or subclassed, you should see the following objects in the Forms Object Navigator (note; my example has the little red arrow on each object indicating that this object is subclassed).

Now lets add the rest of the objects needed for this demo. I use a control block in this demo so there is no need to create any tables to support this demo.
Go to the Windows node of the object navigator and open the property pallet of the WINDOW1 object and set the following properties: (WINDOW1 is created by default by the Forms Builder)
Name: MAIN
Width: 380
Height: 225
Now, select the Module node (very top) and set the following properties:
Name: FRM_JAVA_CALENDAR
Title: Forms and Java Calendar Demo
Console Window: MAIN
Add a new Canvas object to the Canvases node and then open the property pallet for the canvas and set the following properties:
Name: CALENDAR
Window: MAIN
Width: 380
Height: 225
Create a new Data Block and select “Build a new block manually”.
Ensure this block appears first in the list before the DATE_CONTROL_BLOCK and DATE_BUTTON_BLOCK blocks.
Open the property pallet for the new block and set the following properties:
Name: CONTROL
Database Data Block: No
Click on the Items node of the CONTROL block and Add 5 new items.
Click on the first item and set the following properties:
Name: FORMS_DATE
Data Type: DATE
Canvas: CALENDAR
X Position: 90
Y Position: 45
Prompt: Forms Dat
Select the second item and set the following properties:
Name: JAVA_DATE
Data Type: DATE
Canvas: CALENDAR
X Position: 90
Y Position: 60
Prompt: Java Date
Select the third item and set the following properties:
Name: BTN_FORMS_DATE
Item Type: Push Button
Label: Forms Date
Canvas: CALENDAR
X Position: 160
Y Position: 45
Select the fourth item and set the following properties:
Name: BTN_JAVA_DATE
Item Type: Push Button
Label: Java Date
Canvas: CALENDAR
X Position: 160
Y Position: 60
Select the fifth item and set the following properties:
Name: BEAN_AREA
Item Type: Bean Area
Implementation Class: oracle.forms.fd.JCalendar
Canvas: CALENDAR
X Position: 20
Y Position: 30
Width: 0
Height:
Note: It is important that your Bean Area be zero size and visible or the Java Calendar will not display properly.

At this point, your Form should look like the following:


Now we are ready to start writing the code to display and capture the dates from the calendars.
First, we need to compile all code in the Form. I prefer keyboard short-cuts, but you can use the menu if you prefer.
Program Menu => Compile PL/SQL => All
OR
Ctrl + Shift + K
Select BTN_FORMS_DATE item and add a When-Button-Pressed trigger with the following code:


DECLARE
cur_x_pos INTEGER := 0;
cur_y_pos INTEGER := 0;
new_x_pos INTEGER := 0;
new_y_pos INTEGER := 0;
item_height INTEGER := 0;
my_date VARCHAR2(25);
BEGIN
cur_x_pos := Get_Item_Property('CONTROL.FORMS_DATE',x_pos);
cur_y_pos := Get_Item_Property('CONTROL.FORMS_DATE',y_pos);
item_height := Get_Item_Property('CONTROL.FORMS_DATE',height);

new_x_pos := cur_x_pos;
new_y_pos := cur_y_pos + item_height + 2;

Date_Lov.get_Date(display_date => sysdate
,return_item => 'CONTROL.FORMS_DATE'
,v_x_pos => new_x_pos
,v_y_pos => new_y_pos
,v_title => 'FORMS DATE PICKER'
,v_ok => 'OK'
,v_cancel => 'Cancel'
,V_highlight => FALSE
,v_autoconfirm => FALSE
,v_autoskip => FALSE
);
END;

Select the BTN_JAVA_DATE item and add a When-Button-Pressed trigger with the following code:
BEGIN
Set_Custom_Property('CONTROL.BEAN_AREA',1,'SHOW_CALENDAR','50,50');
END;

Lastly, we need to add a When-Custom-Item-Event trigger to CONTROL.BEAN_AREA with the following code:

DECLARE
event_name VARCHAR2(30) := :system.custom_item_event;
event_vals ParamList;
event_val_type NUMBER;
jb_date VARCHAR2(256); --Complete Date
jb_day VARCHAR2(256); --Day Number (1-31)
jb_month VARCHAR2(256); --Month Number (1-12)
jb_year VARCHAR2(256); --Year number (YYYY)
BEGIN
IF ( event_name = 'CALENDAR_EVENT' ) THEN
event_vals := Get_Parameter_List(:SYSTEM.Custom_Item_Event_Parameters);
Get_Parameter_Attr(event_vals, 'CALENDAR_EVENT_DATE', event_val_type, jb_date);
Get_Parameter_Attr(event_vals, 'CALENDAR_EVENT_DAY', event_val_type, jb_day);
Get_Parameter_Attr(event_vals, 'CALENDAR_EVENT_MONTH', event_val_type, jb_month);
Get_Parameter_Attr(event_vals, 'CALENDAR_EVENT_YEAR', event_val_type, jb_year);
:CONTROL.JAVA_DATE := jb_month||'/'||jb_day||'/'||jb_year;
Synchronize;
END IF;
END;

We’re now ready to test the form. If you have configured Forms Builder to run forms locally, make sure you start the OC4J Instance first, and then Click on the Run Icon () in the toolbar or press Ctrl + R.

Your form should look like the following:



Forms Date Picker:





Java Date Picker:




Final Results:








Reference:

A Java Swing Calendar, published by Francios Degrelle; December 21, 2007 (http://forms.pjc.bean.over-blog.com/article-14848846.html)
Oracle Corporation; Forms 10g Demo Files (http://www.oracle.com/technetwork/developer-tools/forms/overview/index.html)
Č
ċďv.1
CALENDAR.olb (68k)Craig Butts, Apr 17, 2012, 2:16 PM
ċďv.2
Calendar.zip (558k)Craig Butts, Apr 6, 2011, 2:14 PM