Thursday, November 25, 2010

Metawidget: Collections Support Part 2 (More Advanced)

In my last post I looked at some quirks of using the ICEfaces PanelTabSet with Metawidget. Specifically, because PanelTabSet stores the selectedIndex of the current tab inside the component, the component cannot succesfully be destroyed and recreated.

To work around this we looked at using Metawidget's COMPONENT_ATTRIBUTE_NOT_RECREATABLE to preserve the selectedIndex at the expense of some dynamicism. We further looked at using JSF's binding attribute to try and claw back some of that dynamicism.

However, a different approach entirely would be to stop PanelTabSet storing selectedIndex internally! It turns out ICEfaces provides an API for this. The only question is where else to store selectedIndex, but keeping it inside the current request seems to work nicely. So:

  1. Forget all about COMPONENT_ATTRIBUTE_NOT_RECREATABLE and the binding attribute (and related mMetawidget inside the managed bean)

  2. Change the line in the IceFacesTabsWidgetBuilder that says...

    panelTabSet.getAttributes().put( UIMetawidget.COMPONENT_ATTRIBUTE_NOT_RECREATABLE, true ); say...

    panelTabSet.setValueExpression( "selectedIndex", expressionFactory.createValueExpression( elContext, "#{param['tab-" + attributes.get( NAME ) + "']}", Object.class ) );
So now you get fully dynamic tabs at the same time as tab switching working! This technique only applies in those cases where the UIComponent thoughtfully exposes such an API, but in such cases it's very useful.

Hope that helps!


Simone said...

You are simply great Richard!

I'll try also this solution as soon as possible!

I'm quite new to the world of Iceface and JSF and these "tricks" I'm learning from you are very useful!

Thanks a lot!

NB: I've added the ability to add and remove tabs dynamically and keeping the inner object class on the fly. If you think it could be useful I could post it. Here or on the other forum?


Richard said...


Ideally you would blog it on your own blog (or, if necessary, start a blog). I'm always grateful for external blogs as a way to promote Metawidget for me.

But yes, I would be very interested in seeing what you have added.



Anonymous said...

Hi Richard,

Is it possible to change the metagwidget layout dynamically? Such as, switching icefaces PanelTablayout to section or reversely?



Richard said...


Yes. I have blogged an answer here:

Hope that helps,