In the past few weeks in SDN, there have been a number of questions on handling HTMLB events. One of the biggest frustrations was with our
examples. This is a typical example:
<!code> DATA: evt TYPE REF TO if_htmlb_data.
<!code> evt = cl_htmlb_manager=>get_event_ex( request ).
<!code> IF evt IS NOT INITIAL AND evt->event_name = 'button' AND evt->event_type = 'click'.
<!code> ...
<!code> ENDIF.
<!code>
The first questions were always: Where can you obtain a list of events that are available? What are these magic strings? What happens if we write
them in upper/lower case?
The second problem was that of event dispatching. For the very old HTMLB library, we supported an interface if_htmlb_event that could be
implemented by an event handler class. However, this work was never done for the XHTMLB or PHTMLB libraries. Then a few days ago, we saw an example of
this technique being actively used with the newer libraries. So definitely it was time to clean this up.
Last, there was the question in the forum about a technique to build an OO framework for the event handling. If we have a button with onClick =
“save”, we would actually like to have the event dispatching done directly onto a method called “save”. After fiddling a little with the code, this
suddenly becomes relatively easy.
In the last two weeks, we have reworked the entire event handling code of the HTMLB family of libraries to address these three points. All of these
improvements will be shipped in the next service pack (620SP43 & 640SP05) that should be available in September 2004. This weblog will give an
overview of the three different techniques on how to handle HTMLB events. It is based on the new work we did. Some parts of this work in older service
packs; but especially for the event dispatching, the above service packs are required.
Test Program
is used to show the current index. Only a small part of the layout
code is shown here. The
complete source code is available.
<!code> <xhtmlb:buttonGroup id = "btngrp" onClick = "TablePager">
<!code> <xhtmlb:buttonGroupItem key = "prev_page"
<!code> text = "Previous Page"
<!code> design = "PREVIOUS"
<!code> disabled = "<%=vIndex_prev_disabled%>" />
<!code> <xhtmlb:buttonGroupItem key = "next_page"
<!code> text = "Next Page"
<!code> design = "NEXT"
<!code> disabled = "<%=vIndex_next_disabled%>" />
<!code> </xhtmlb:buttonGroup>
<!code>
The output for the complete test program is shown below. In this display, the first visible row is currently one, and thus the button to navigate
to the previous page is disabled.
!https://weblogs.sdn.sap.com/weblogs/images/164/BP_HHE_001.GIF|height=0 width=545 height=270 |width=0 width=545 height=270 |src=https://weblogs.sdn.sap.com/weblogs/images/164/BP_HHE_001.GIF|border=0 width=545 height=270 !
Approach One: Manually Handling All HTMLB Events
The first approach to HTMLB event handling is to retrieve the event, and then simply investigate what type of event it is. This type of coding is
usually done in the OnInputProcessing handler of BSP pages.
The incoming event is retrieved with the call cl_htmlb_manager=>get_event_ex. In all cases, only this new method must be used. The older
get_event method is obsolete and not supported for the XHTMLB and PHTMLB libraries. Note that this method can be called more than once, and will
always return the same event.
The get_event_ex method will return an event object that implements at least the if_htmlb_data interface. This interface has a number of
interesting parameters that can be examined to see what type of event has been received. See also “
BSP In-Depth: Using the HTMLB Event System” for an in-depth discussion of the different event parameters.
<!code> DATA: event TYPE REF TO if_htmlb_data.
<!code> event = cl_htmlb_manager=>get_event_ex( request ).
<!code>
<!code> IF event IS NOT INITIAL
<!code> AND event->event_name = xhtmlb_events=>buttongroup
<!code> AND event->event_type = xhtmlb_events=>buttongroup_click.
<!code> CASE event->event_defined.
<!code> WHEN 'prev_page'.
<!code> vindex = vindex - vsize.
<!code> IF vindex < 1.
<!code> vindex = 1.
<!code> ENDIF.
<!code> WHEN 'next_page'.
<!code> vindex = vindex + vsize.
<!code> IF vindex >= LINES( sflight ).
<!code> vindex = LINES( sflight ) - vsize + 1.
<!code> ENDIF.
<!code> ENDCASE.
<!code> ENDIF.
<!code>
This approach of event handling is very fast to program, especially on a BSP page. The disadvantage of this approach is that the code quickly
explodes once events for many controls must be handled.
Approach Two: Dispatching Events via IF_HTMLB_EVENTS Interface
The HTMLB rendering libraries also contain a technique to dispatch events to a handler class. For this, use the
cl_htmlb_manager=>dispatch_event_ex method. One of the parameters is a handler class that will accept the incoming event and process it. Typically,
this can be a separate developed class, a controller class, or even the application class.
In this example, we will use a handler class that has been developed separately. On the BSP page, in the OnInputProcessing method, the event
handling code now reduces to a few lines. All that is required is an instance of the handler class, and then the dispatcher is called.
<!code> DATA: handler TYPE REF TO CL_SDN_HANDLING_HTMLB_EVENTS.
<!code> CREATE OBJECT handler.
<!code> cl_htmlb_manager=>dispatch_event_ex( request = request
<!code> page_context = page_context
<!code> event_handler = handler ).
<!code>
The benefit of this approach is that the event handling code is placed into a separate class, where the full strength of the workbench can be used.
It reduces clutter in BSP pages. In addition, if new controls are added, no further plumbing code is required on the BSP page. All events will be
dispatched by this one call.
The important question is how will the handler class know what events are available, and what parameters each event handling method must have. For
this, an interface if_htmlb_events is defined. This interface contains all the possible events that can be fired by the HTMLB library. Each method
contains the correct parameters with which it will be called. Similar interfaces exist: if_xhtmlb_events and if_phtmlb_events for the other two
libraries.
!https://weblogs.sdn.sap.com/weblogs/images/164/BP_HHE_003.GIF|height=0 width=402 height=238 |width=0 width=402 height=238 |src=https://weblogs.sdn.sap.com/weblogs/images/164/BP_HHE_003.GIF|border=0 width=402 height=238 !</body>