Custom event encoders need to implement the oracle.forms.beans.Encoder
interface in the same way that data type encoders do. However, the actual requirements
for an event encoder are simpler because the event object only has to be converted
from Java into a string form that PL/SQL can handle. The reverse operation is
not required. The best option for building a custom event encoder is to subclass
oracle.forms.beans.EventEncoder
.
Event encoders are simply a specialized data type converter, so the constructor
is the same as for a normal data type converter. In our example here, the encoder for javax.swing.event.HyperlinkEvent
is created which could be
raised by a JavaBean containing a editor widget.
The constructor for your class should call the constructor in EventEncoder
superclass, passing the Class
of the class being encoded as an
argument:
public class HyperlinkEventEncoder extends EventEncoder { public HyperlinkEventEncoder() { super(HyperlinkEvent.class); //Do any other stuff here
} ...
As well as the constructor, the other method that an event encoder must have
is a toString()
method. The value returned by the toString()
will be the value that is exposed through the :SYSTEM.CUSTOM_ITEM_EVENT
variable in the Form. So by convention we make this value a simple uppercase
string with words separated by underscores. In this case the Hyperlink
event could represent three different actions so the toString()
logic will create one of three different event names based on the value of getType()
which is a method provided by the HyperlinkEvent
Class to determine
this.
public String toString(Object value) { HyperlinkEvent he = (HyperlinkEvent)value; String type = "HYPERLINK_EVENT"; if ( he != null ) { if ( he.getEventType() == HyperlinkEventType.ENTERED) { type = "HYPERLINK_ENTERED"; } else if ( he.getEventType() == HyperlinkEventType.EXITED) { type = "HYPERLINK_EXITED"; } else if ( he.getEventType() == HyperlinkEventType.ACTIVATED) { type = "HYPERLINK_ACTIVATED"; } } return type; }
Finally, with an event encoder we have the option of sending additional information
to the Form in the DATA
parameter passing in the :SYSTEM.CUSTOM_ITEM_EVENT_PARAMETERS
parameter list. This is accomplished by creating a getParameters()
method that accepts the event object and also returns a string. The enhanced
JavaBean support will populate the DATA
parameter with this information.
The default implementation of the getParameters()
method is to
return an empty string, so you only need to create this method if you need to
pass additional information. In our example here, we want to pass the URL of
the hyperlink if, and only if, the event type is HYPERLINK_ACTIVATED
.
Public String getParameters(Object value) { HyperlinkEvent he = (HyperlinkEvent)value; String url= ""; if (( he != null ) && (he.getEventType() == HyperlinkEventType.ACTIVATED)) { url = he.getURL().toString(); } return url; }
Writing a Custom Data Type Encoder