This is something I've been fighting with too. I've just been doing the
backingBean.getDomainObjectAsSelectItems() kind of method that returns
either a SelectItem[] or a List. The biggest thing I don't like about
that is that the iteration over the collection is done twice, once to
convert the domain objects into select items and then again to display
the select items. It's probably not too big a deal because using a
<select> or radio buttons or whatever wouldn't be a good idea for too
long a list of domain objects, but it's one area that should be more
optimized.
I looked at using either the TransformedList or the LazyList from
Commons Collections, but neither seems to do what I want.
TransformedList only transforms things as they are added to the list not
as they are gotten. LazyList doesn't provide anyway to parameterize the
creation of the elements as they are accessed. Does anyone know of
another package out there that provides for transforming elements in a
List as they are accessed?
I wouldn't think it would be too tough to implement. You're basically
wrapping a List with another that has custom get() and iterator()
methods. You'd probably want to have something like a caching list that
would cache modifications. If sometihng is not in that list you check
the wrapped list and do the transform and put it in the cache and return
it. The hardest part is the iterator(), I'm not at all sure what that
would look like. I guess it would basically wrap the two iterators from
the wrapped list and the cache list. When a i.next() is called check if
the cached list iterator has a next, if not check the wrapped list
iterator and if it has a next then get it, transform it, add it to the
cached list and return it. I'm pretty sure you can do this with a list,
but I'm not sure about other collection types (like Sets, which is what
Hibernate likes to use so much). Also, you'd probably want to require
that the wrapped list be unmodifiable.
Anyways, just some thoughts that I've been kicking around about
converting collections of domain objects into a list or array of
SelectItems.
Rich
Martin Marinschek wrote:
I think this is indeed a great idea, Rick!
if you want to provide a new tag, extend from the existing SelectItems
tag - it should be quite easy to get up to speed with this component.
regards,
Martin
On 9/8/05, Rick Reumann <[EMAIL PROTECTED]> wrote:
On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
However, as you say, there's always room for
improvement.
Yup. And I understand JSF/MyFaces is still relatively young.
I do think a tag to support this would be nice.
As an interim solution I'll probably create a method that returns a
SelectItems array...
SelectItems[] createSelectItemsFromCollection( Collection col, String
valueProp, String labelProp ) {
then using the reflection API, I should be able to return an array of
SelectItems. This method could then be used for *any* Collection
within the application.