background image

Handling Events for Custom Components

<< The encodeEnd Method | Creating the Component Tag Handler >>
<< The encodeEnd Method | Creating the Component Tag Handler >>

Handling Events for Custom Components

Identifying the Renderer Type
During the render response phase, the JavaServer Faces implementation calls the
getRendererType
method of the component's tag handler to determine which renderer to
invoke, if there is one.
The getRendererType method of AreaTag must return the type associated with AreaRenderer.
You identify this type when you register AreaRenderer with the render kit, as described in
"Registering a Custom Renderer with a Render Kit" on page 455
. Here is the getRendererType
method from the AreaTag class:
public String getRendererType() { return (
"DemoArea");}
"Creating the Component Tag Handler" on page 430
explains more about the getRendererType
method.
Handling Events for Custom Components
As explained in
"Implementing an Event Listener" on page 395
, events are automatically
queued on standard components that fire events. A custom component, on the other hand,
must manually queue events from its decode method if it fires events.
"Performing Decoding" on page 424
explains how to queue an event on MapComponent using its
decode
method. This section explains how to write the class representing the event of clicking
on the map and how to write the method that processes this event.
As explained in
"Understanding the JSP Page" on page 414
, the actionListener attribute of the
map
tag points to the chooseLocaleFromMap method of the bean LocaleBean. This method
processes the event of clicking the image map. Here is the chooseLocaleFromMap method of
LocaleBean
:
public void chooseLocaleFromMap(ActionEvent actionEvent) {
AreaSelectedEvent event = (AreaSelectedEvent) actionEvent;
String current = event.getMapComponent().getCurrent();
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().setLocale((Locale)
locales.get(current));
}
When the JavaServer Faces implementation calls this method, it passes in an ActionEvent
object that represents the event generated by clicking on the image map. Next, it casts it to an
AreaSelectedEvent
object (see
tut-install/javaeetutorial5/examples/web/bookstore6/src/java/com/sun/bookstore6/listeners/AreaSelectedE
Then this method gets the MapComponent associated with the event. It then gets the value of the
MapComponent
object's current attribute, which indicates the currently selected area. The
Handling Events for Custom Components
Chapter 13 · Creating Custom UI Components
429