David Botterill's Weblog

« Previous day (Jul 22, 2006) | Main | Next day (Jul 24, 2006) »

20060723 Sunday July 23, 2006

Generically Setting UI Component Properties in Java Studio Creator

I recently saw a forum question about setting page component properties generically instead of specifying each property name.  I've done similar things using Swing and Visual Basic and this is also possible with Java Studio Creator.

As with any type Java Studio Creator development you MUST be ware of the application model.  Please see the article  The Java Studio Creator 2 Application Model for details on the application model.  The following solution will only work in the case of  navigating from a JavaServer Faces page to a JavaServer Faces page.  This will NOT work going from a non-JavaServer Faces page to a JavaServer Faces page such as navigating to the Creator application for the first time.  If you refer the section "Page Bean Life Cycle" in the article mentioned above, you'll see the "Update Model Values" phase is not invoked when first navigating to the Creator page.  Because of this, the components model is not complete and renders the following code useless.  This means you would have to assume an initial value for the components on the first page.

Here's the solution.

    public void prerender() {
if(mySpecialCondition) {
setFields(this.getPage1());
}
}

private void setFields(UIComponent inComponent) {
/**
* If this component has children, set the properties then recurse through each child
* to see if they have children to be set.
*/
List allComponents = inComponent.getChildren();
if(null == allComponents) return;
Iterator listIterator = allComponents.iterator();
UIComponent currentComponent = null;
while(listIterator.hasNext()) {
currentComponent = (UIComponent)listIterator.next();
if(null != currentComponent) {
if(currentComponent instanceof TextField) {
((TextField)currentComponent).setReadOnly(true);
} else if(currentComponent instanceof TextArea) {
((TextArea)currentComponent).setReadOnly(true);
} else if(currentComponent instanceof Button) {
((Button)currentComponent).setVisible(false);
}
/**
* Recurse through all the components
*/
this.setFields(currentComponent);
}

}

}
Of course you would need to change the set of components and properties to fit your needs.  Also the scope of the components to be changed could be controlled by using a container (Layout) such as a group panel.

Enjoy!
-David
Posted by david ( Jul 23 2006, 02:49:18 PM MDT ) Permalink Comments [3] del.icio.us | digg | technorati