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
Comments:

Thanks, David! But there is still ussue how to setup component property when first time enter page. My first idea was find a way reenter page. Can I do it with .requestComplete() .redirect("samePage") methods in page's Destroy() callback method? Or, may be, You can suppose other idea?

Posted by achervov on September 13, 2006 at 10:29 PM MDT #

Unfortunately, the component tree is not filled in until update model is called the first time. The only way around this that I can think of is to have a "pre" page for the page the does a "redirect" in the init to the real page. This would be a JSF page to JSF page which should cause the update model phase on the real page to be invoked. -David

Posted by David Botterill on September 16, 2006 at 06:23 PM MDT #

Hi David, this method won't work. The reason is when you are navigating to the JSF page first time even from the JSF page UIViewRoot object for the new page is created but not filled in neither in beforeRenderResponse() nor prerender() method. For instance, putting the following line (this.getPage1().getChildren();) in the beforeRenderResponse() or prerender() will always return empty list when it is first time the required page get requested. UIViewRoot gets filled in ViewHandlerImpl class (line 311 - JSC 2.1). But this method is called after after prerender() method but before afterRenderResponse() one. Same time it is not possible to set components attributes in afterRenderResponse() method because rendering process is already finished and these changes will be applied only during restore view phase. To make this happen user have to generate postback to the same page again. Kind regards, Alex

Posted by Alexander on September 25, 2006 at 04:40 AM MDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

David Botterill

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today