A script-enabled browser is required for this page to function properly.

Including a JavaBean and Custom Controls

The following example shows how to include a JavaBean in a Forms application. This particular JavaBean (called sunw.demo.juggler.Juggler) is an animated graphic for which the animation speed can be adjusted by the end user through a choice of controls on the display.

In the Form:

In the form, you create a Bean Area item, and then have its Implementation Class property identify the implementation class that is the defining container or wrapper for the JavaBean itself. In this example, the bean area item is called JUGGLER_BEAN, and its full Implementation Class name is oracle.forms.pjc.demo.JugglerWrapper.

In the form, there are also various control items for the end user: a freeze/restart button, faster and slower buttons for the animation speed, and also a slider-type speed control. (This slider is implemented by a separate JavaBean and JavaBean wrapper.)

By using PL/SQL-based triggers and Built-ins in the form, the example gets and sets the properties defined within the wrapper classes. By registering the events, methods, and properties of the actual JavaBean as properties of the wrapper class, it is possible to control the JavaBean programmatically by simply setting the appropriate property in the wrapper. In the form, the properties of the JavaBean are accessed by using the SET_CUSTOM_PROPERTY Built-in. JavaBean events are captured by using the When-Custom-Item-Event trigger.

The Container/Wrapper for the Juggler JavaBean

Remember that for a JavaBean to communicate at runtime with a form, the wrapper’s class definition must extend the VBean class. The VBean class definition itself is supplied as part of the WebForms product package. (VBean also extends IView.)


package oracle.forms.pjc.demo;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import oracle.forms.properties.ID;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.VBean;
public class JugglerWrapper
 extends VBean
 implements MouseListener
{
 private Component mComp;
 private IHandler mHandler;

// Java Bean EVENTS registered as properties of 'wrapper' implementation 
// class
 private static final ID 
  MOUSEPRESSED = ID.registerProperty("MousePressed");
 private static final ID 
  MOUSEENTERED = ID.registerProperty("MouseEntered");
 private static final ID 
  MOUSEEXITED = ID.registerProperty("MouseExited");
// Java Bean PROPERTIES registered as properties of 'wrapper' 
  implementation
// class
 private static final ID 
  X_POS = ID.registerProperty("MouseX");
 private static final ID 
  Y_POS = ID.registerProperty("MouseY");
 private static final ID 
  ARATE = ID.registerProperty("AnimationRate");

// Java Bean METHODS registered as properties of 'wrapper'  
// implementation class
 private static final ID 
  STOP = ID.registerProperty("StopJuggling");
 private static final ID 
  START = ID.registerProperty("StartJuggling");
 private static final ID 
  SETRATE = ID.registerProperty("SetAnimationRate");
 private static final ID 
  GETRATE = ID.registerProperty("GetAnimationRate");
 public JugglerWrapper()
 {
 super();
 try
 {
 ClassLoader cl = getClass().getClassLoader();
 Object obj = Beans.instantiate(cl, "sunw.demo.juggler.Juggler");
 mComp = (Component) obj;
 mComp.setVisible(true);
 mComp.setEnabled(true);
 add("Center", 
  mComp);
 if ( mComp instanceof Applet 
  )
 {
 Applet apl = (Applet) Beans.getInstanceOf(mComp, Applet.class);
 apl.start();
 }
 }
 catch ( Exception e )
 {
 }
 }

 public void init(IHandler 
  handler)
 {
 super.init(handler);
 mHandler = handler;
 mComp.addMouseListener(this);
 }

 public void destroy()
 {
 if ( mComp instanceof Applet 
  )
 {
 Applet apl = (Applet) Beans.getInstanceOf(mComp, Applet.class);
 apl.destroy();
 apl.stop();
 }
 }

 public boolean setProperty(ID 
  pid, Object value)
 {
 if ( pid == STOP )
 {
 if ( ((Integer)value).intValue()  == 1)
 {
 ((sunw.demo.juggler.Juggler)mComp).stopJuggling();
 }
 return true;
 }
 else if ( pid == START )
 {
 if ( ((Integer)value).intValue()  == 1)
 {
 ((sunw.demo.juggler.Juggler)mComp).startJuggling();
 }
 return true;
 }
 else if ( pid == SETRATE 
  )
 {
 ((sunw.demo.juggler.Juggler)mComp).setAnimationRate(
 ((Integer)value).intValue());
 return true;
 }
 return super.setProperty(pid, value);
 }
 public void mouseClicked(MouseEvent event)
 {
 }
 public void mouseEntered(MouseEvent event)
 {
 CustomEvent ce = new CustomEvent(mHandler, MOUSEENTERED);
 dispatchCustomEvent(ce);
 }
 public void mouseExited(MouseEvent event)
 {
 CustomEvent ce = new CustomEvent(mHandler, MOUSEEXITED);
 dispatchCustomEvent(ce);
 }
 public void mousePressed(MouseEvent event)
 {
 Integer xobj = new Integer(event.getX());
 Integer yobj = new Integer(event.getY());
 try
 {
 mHandler.setProperty(Y_POS, yobj);
 mHandler.setProperty(X_POS, xobj);
 }
 catch (Exception e)
 {
 }
 CustomEvent ce = new CustomEvent(mHandler, MOUSEPRESSED);
 dispatchCustomEvent(ce);
 }
 public void mouseReleased(MouseEvent event)
 {
 }
}

Receiving Information from the JavaBean via the When-Custom-Item-Event Trigger

In this example, the form has a When-Custom-Item-Event trigger attached to the bean area item for the Juggler Bean. When that JavaBean’s container/wrapper dispatches a custom event, this trigger is executed. The trigger runs the PL/SQL procedure called Juggler_event_trap (shown below):

Note that the procedure in the form looks at the System.Custom_Item_Event and System.Custom_Item_Event_Parameters fields to find out the event name and to obtain the values that were set in the container/wrapper.

Note also that the event name and the X and Y mouse coordinate values that are being passed are registered properties in the container/wrapper.

  
PROCEDURE Juggler_event_trap IS
 BeanHdl Item;
  BeanValListHdl ParamList;
  paramType Number;
  EventName VarChar2(20);
  CurrentValue number(4);
  XPos Number(4);
  YPos Number(4);
Begin
  BeanValListHdl := get_parameter_list(:SYSTEM.Custom_Item_Event_Parameters);  EventName 
  := :SYSTEM.Custom_Item_Event;
  :event_name := EventName;
  if (EventName = 'MousePressed') then
   get_parameter_attr(BeanValListHdl,'MouseX',ParamType, CurrentValue);
   XPos := CurrentValue;
   get_parameter_attr(BeanValListHdl,'MouseY',ParamType, CurrentValue);
   YPos := CurrentValue;
   message('If you click at '||to_Char(XPos)||'/'||to_Char(YPos)||' 
  again I may drop these beans');
 end if;
End;

A JavaBean Slider Control

In this example, there are also control items in the form’s user interface that allow the end user to vary the animation speed of the juggler JavaBean.

One of these controls is a Bean Area item called EWT_SLIDER. Its full implementation Class property is oracle.forms.pjc.demo.EwtSliderWrapper, which is the container/wrapper shown below:

  
/**
** Simple JavaBean Wrapper. It allows the EWT Slider control 
** component to be embedded within the form. The slider allows 
** the end user to control the juggler bean’s animation speed.
*/
package oracle.forms.pjc.demo;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import oracle.ewt.slider.Slider;
import oracle.ewt.meter.BoundedRangeModelImpl;
import oracle.forms.properties.ID;
import oracle.forms.handler.IHandler;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.VBean;
public class EwtSliderWrapper extends VBean
   implements PropertyChangeListener
{
 private Component mComponent;
 private IHandler mHandler;
 private BoundedRangeModelImpl mSliderRange;

 // Java Bean EVENTS registered as properties of 'wrapper' implementation class
 private static final ID 
  VALUECHANGED = ID.registerProperty("ValueChanged");

 // Java Bean PROPERTIES registered as properties of 'wrapper' implementation
 // class
 private static final ID 
  VALUE = ID.registerProperty("Value");
 private static final ID 
  MAXVALUE = ID.registerProperty("MaxValue");
 private static final ID 
  MINVALUE = ID.registerProperty("MinValue");

 // Java Bean METHODS registered as properties of 'wrapper' implementation class
 private static final ID 
  SETVALUE = ID.registerProperty("SetValue");
 private static final ID 
  GETVALUE = ID.registerProperty("GetValue");
 private static final ID 
  GETMAXVALUE = ID.registerProperty("GetMaximum");
 private static final ID 
  GETMINVALUE = ID.registerProperty("GetMinimum");
 public void init(IHandler handler)
 {
 super.init(handler);
 mHandler = handler;
 mSliderRange = new BoundedRangeModelImpl(0,300,125);
 mComponent = new Slider(mSliderRange);
 mSliderRange.addPropertyChangeListener(this);
 mComponent.setEnabled(true);
 add("Center", mComponent);
 }
 public boolean setProperty(ID pid, Object value)
 {
 return super.setProperty(pid, value);
 }
 public void propertyChange(PropertyChangeEvent event)
 {
 String PropName = event.getPropertyName();
 if (PropName.equals("value"))
 {
 Integer SliderValue = new Integer(mSliderRange.getValue());
 try
   {
    mHandler.setProperty(VALUE, SliderValue);
   }
 catch (Exception e)
 {
 }
 CustomEvent ce = new CustomEvent(mHandler, VALUECHANGED);
 dispatchCustomEvent(ce);
 }
 }
}

In the form, a When-Custom-Item-Event trigger is attached to the EWT_Slider Bean Area item. When the end-user moves the slide on the display, the above JavaBean container becomes aware through its PropertyChangeListener. The container dispatches a custom event, which results in this trigger being fired in the form. The trigger executes the following PL/SQL procedure, called Slider_Event_Trap.

After this procedure picks up the changed values from the System.Custom_Item_Event_Parameters, it uses the Set_Custom_Property Built-in to pass those changed values along to the Juggler JavaBean.

  
PROCEDURE Slider_Event_Trap IS
 BeanHdl Item;
  BeanValListHdl ParamList;
  paramType Number;
  EventName VarChar2(20);
  CurrentValue Number(4);
  NewAnimationRate Number(4);
Begin
  -- Update data items and Display fields with current radius 
  information
  BeanValListHdl := get_parameter_list(:SYSTEM.Custom_Item_Event_Parameters);
  EventName := :SYSTEM.Custom_Item_Event;
  :event_name := EventName;
  if (EventName = 'ValueChanged') then
   get_parameter_attr(BeanValListHdl,'Value',ParamType, CurrentValue);
   NewAnimationRate := (300 - CurrentValue);
   :Animation_Rate := NewAnimationRate;
 set_custom_property('Juggler_Bean', 
  ALL_ROWS, 'SetAnimationRate', NewAnimationRate);
  elsif (EventName = 'mouseReleased') then
   get_parameter_attr(BeanValListHdl,'Value',ParamType, CurrentValue);
 set_custom_property('Juggler_Bean', 
  ALL_ROWS, 'SetAnimationRate', CurrentValue);
 end if;
End;

Other Java Buttons in the form

The form also has several custom user interface components. These are created by extending the exisiting Forms VButton class, as follows:

  
/**
** Here, the 'Oracle Look & Feel' buttons are enhanced by subclassing the
** standard EWT PushButton Class and calling one of the available methods.
** Use of both setLeftmost() & setRightmost() function causes the Left & Right
** edges of the button to be rounded (allows for the creation of a stand-alone
** rounded style button)
*/
import oracle.forms.ui.VButton;
import oracle.ewt.button.PushButton;
public class SingleButton
 extends VButton
{
 public SingleButton()
 {
 super();
 setLeftmost(true);
 setRightmost(true);
 }
}
/**
** Here, the 'Oracle Look & Feel' buttons are enhanced by subclassing the
** standard EWT PushButton Class and calling one of the available 
** methods. Use of the setLeftmost() function causes the left edge of 
** the button to be rounded (for use at the left end of a group of buttons) 
*/
import oracle.forms.ui.VButton;
import oracle.ewt.button.PushButton;
public class LeftButton
 extends VButton
{
 public LeftButton()
 {
 super();
 setLeftmost(true);
 }
}
(The RightButton definition, which is similar to the above LeftButton, is not shown here.)

Other Java-related Triggers in the form

There are also several When-Button-Pressed triggers in the form. The following When-Button-Pressed trigger, for example, is attached to the Start-Stop control item button in the form. It is used to execute the following procedure, which freezes or restarts the animated action of the juggler display.

Notice that the StartJuggling and StopJuggling properties referenced in the Set_Custom_Property Built-ins are also registered properties in the container/wrapper for the Juggler JavaBean.

  
PROCEDURE Start_Stop_Juggling IS
BtnHdl Item;
BtnLabel VarChar2(10);
BeanHdl Item;
BEGIN
  BtnHdl := find_item('START_STOP_BTN');
  if NOT id_null(BtnHdl) then
   BtnLabel := get_item_property(BtnHdl, LABEL);
   BeanHdl := find_item('JUGGLER_BEAN');
   if NOT id_Null(BeanHdl) then
   if BtnLabel = 'Start Me!' then
   set_custom_property(BeanHdl, ALL_ROWS, 'StartJuggling', 1);
   set_item_property (BtnHdl, LABEL, 'Stop Me!');
 elsif BtnLabel = 'Stop Me!' 
  then
   set_custom_property(BeanHdl, ALL_ROWS, 'StopJuggling', 1);
   set_item_property (BtnHdl, LABEL, 'Start Me!');
 end if;
 end if;
 end if;
END;

Item Type Property

Implementation Class