Event Handling

Some types of widgets can emit events. For example, the action widget produces ActionEvents and the field widget produces ValueChangedEvents. Next to these events, there are also processing phase events, fired in between the various phases of the processing of a request.

Handling events can be done in two ways:

When are events processed? (Request processing phases)

To answer the question "When are events processed?", we have to look a bit deeper into how a form request is handled. This is separated in a couple of phases, more specifically the following ones:

Defining event handlers in the form definition

Event handlers can be specified as part of the form definition, as child of the various wd:on-xxx elements, such as wd:on-action for the action widget.

Event handlers can be written in either javascript or java. The form definition syntax is as follows:

<wd:on-xxxx>
  <javascript>
    ... some inline javascript code ...
  </javascript>
  <java class="..."/>
</wd:on-xxxx>

You can specify as many <javascript> and/or <java> event listeners as you want.

Javascript event listeners

Objects available in the Javascript snippet:

Java event listeners

The Java class specified in the class attribute on the java element should implement a certain interface. Which interface depends on the type of widget. These are mentioned in the WoodyWidgetReference.

Note: In the future, the Java event listener classes will also be treated as Avalon components, so that they get access to e.g. the Logger and the ServiceManager, but this was not yet implemented at the time of this writing (October 31, 2003).

Handling events using the !FormHandler

To handle events using a FormHandler, write a class implementing the org.apache.cocoon.woody.event.FormHandler interface. Alternatively you can extend from the abstract class org.apache.cocoon.woody.event.AbstractFormHandler, which will split ActionEvents and ValueChangedEvents to two different methods. See the Javadocs of these interfaces and classes for more details.

Once you created the FormHandler, register it on a form instance by calling the method set!FormHandler(FormHandler formHandler) on it.

Note: If you're doing this from flowscript, you can access the Java Form instance by calling the method getWidget() on the flowscript form object.

eg:

        var f1h  = new Packages.org.apache.cocoon.woody.event.Form1Handler();
        form.getWidget().setFormHandler( f1h );

WoodyEventHandling (last edited 2009-09-20 23:41:57 by localhost)