dev@jsftemplating.java.net

Re: JSFTemplating: Is it possible to write JSFT component that wraps <f:selectItems />

From: Ken Paulsen <Ken.Paulsen_at_Sun.COM>
Date: Tue, 25 Sep 2007 10:01:15 -0700

Hi Leonid,

Yes you can create a real JSF component using JSFT that does this....
however, JSFT also provides an another option as well. If you create a
real component, you'll end up creating a UIComponent class that
implements the functionality you described below (most likely extending
from the selectOneMenu component). JSFT will allow you to write a
template for the Renderer that does what you need (if you need this at
all.. you might just use the existing JSF renderer if the API/layout
hasn't changed).

The other option JSFT provides is to use a Factory to create the
UIComponents the way you described below. I prefer this option based on
the requirements you described below. This allows you to do whatever
you want w/ the UIComponents as you create them. You can pass in any
options you'd like, such as "sort" or "filter" criteria. The result
would look something like:

    <myjsft:mergeSelectOneMenu id="selectAirport"
value="#{myBean.selectedAirport}" sort="alpha desc">
        <f:selectItems value="#{airportController.germany}">
        <f:selectItems value="{airportController.austria}" />
    </myjsft:mergeSelectOneMenu>

Or:

    <myjsft:mergeSelectOneMenu id="selectUSState"
value="#{myBean.selectedState}" filterout="PR VI">
        <f:selectItems value="#{geoController.USStatesAndTerritories}" />
    </myjsft:mergeSelectOneMenu>

You can see a basic example of a factory (that does nothing custom) here:

    
https://jsftemplating.dev.java.net/source/browse/jsftemplating/src/java/com/sun/jsftemplating/component/factory/ri/SelectOneMenuFactory.java?rev=1.3&view=markup

You'd want to start with something like this. The "LayoutComponent"
object that is passed in has access to the options that are passed in
from the template file. You can call:
"descriptor.getEvaluatedOption(...)" to get the value (i.e. to get the
value of the "filterout" option). You can then manipulate the
selectOneMenu UIComponent to contain the selectItems that you want.

To see a more complicated example of doing this, see:

    
https://jsftemplating.dev.java.net/source/browse/jsftemplating/src/java/com/sun/jsftemplating/component/factory/sun/DropDownFactory.java?rev=HEAD&view=markup

This example shows how the factory adds the "labels" and "values"
properties to the "sun:dropDown" tag so that you don't need to have
selectItems at all for the Woodstock version of the drop down menu
component. The values are expected to be List objects, both properties
are optional, if only "Labels" are supplied, then the "value" of the
option will == the Label. It doesn't do filtering or sorting... but if
you had a bean where you retrieved these List values from that did
filtering or sorting (or better yet, a handler that did filtering or
sorting that executed during a before Create)... then this class would
do what you're are trying to do w/o modification.

I hope this gives you some ideas!


Good luck!

Ken


Leonid Lamburt wrote:
> Hi,
>
> is it possible to create JSFT components that merge several
> <f:selectItems /> lists or do some kind
> of filtering on <f:selectItems /> lists?
>
> example1:
> Here I want a unified (alpha sorted) list of airports.
> <h:selectOneMenu id="selectAirport" value="#{myBean.selectedAirport}">
> <myjsft:merge sort="alpha desc">
> <f:selectItems value="#{airportController.germany}" />
> <f:selectItems value="{airportController.austria}" />
> </myjsft:merge>
> </h:selectOneMenu>
>
>
>
> example2:
> Here I'd like to filter out US territories.
> <h:selectOneMenu id="selectUSState" value="#{myBean.selectedState}">
> <myjsft:merge filterout="PR VI">
> <f:selectItems value="#{geoController.USStatesAndTerritories}" />
> </myjsft:merge>
> </h:selectOneMenu>
>
>
> So, does JSF leaves room for the creation of such components?
>
> Thanks.
>
> ------------------------------------------------------------------------
> Gear up for Halo® 3 and get a $25 Best Buy gift card. It's our way of
> saying thanks for using Windows Live^(TM). Get it now!
> <http://gethalo3gear.com?ocid=SeptemberWLHalo3_WLHMTxt_1>