Tuesday October 03, 2006
Generically Setting UI Component Properties in Java Studio Creator - Part 2
This is a follow on to the my blog "
Generically
Setting UI Component Properties in Java Studio Creator".
Based on comments from Alex and other
forum posts, I need to
update the information I gave about generically setting component
properties. My first blog technique will work for a
"postback" situation, that is if you have a button to do some action
then show the same page again. In JSF 1.1, the UIViewRoot
component tree is not built until after the render response.
This means neither Creator nor any other ViewHandler or
PhaseListener can catch the UIViewRoot at the right time on a page that
has not yet been rendered.
I asked a couple JSF experts,
Jayashri Visvanathan
and Matt Bohm about this issue and they proposed another solution. This blog covers that solution.
If you're new to Java Studio Creator, get used to "binding".
Use property binding whenever you possibly can. The JSF
life cycle was designed to support "binding" and it will help you in
cases like this where you feel you need to go around the life cycle.
The solution involves creating properties on the Session Bean to
cover the properties you want to set for multiple components.
As an example, suppose you have a page with multiple "Text Field"s. The page might look like this.
In this example, we want to initialize all the text fields to be
disabled until a certain condition like a user logging in. To
accomplish this we need to create a property on the Session bean to
bind to. So add a property to the Session bean called "
textFieldDisabled". To do this, right-click on the "Session Bean" in the Project window and choose "Add->Property".
Next, add a property of type
boolean called "
textFieldDisabled".
Now that we have a property to bind to, we need to bind all the Text
Fields to this property. To select all the Text Fields, drag a
box around the Text Fields like this.
Now you can change the "Disabled"
property binding for all the selected Text Fields. From the
Property window, click on the "..." next to the "disabled" property.
The property binding dialog will be brought up and you need to
choose "SessionBean1.textFieldDisabled".
Not you can manipulate the same property on all the selected components
from the backing bean code. Open the "Java" view for Page1 and go
to the "prerender" method. Where you actually put the logic to
change the component properties will depend on what you are basing the
change on. If the condition for changing the properties may
changed before each render, you should put the logic in "prerender".
If the condition only needs to be checked once, you can put the
logic in the "init" method. For this example, add the following
code to the "prerender" method.
public void prerender() {
/**
* Some logic to check for read only. For now, set to true.
*/
boolean readOnly = true;
if(readOnly) {
this.getSessionBean1().setTextFieldDisabled(true);
}
}
Running the web application will show a web page that looks like this.
Notice the text fields are gray because they are disabled.
Points to Remember
- In JSF 1.1 UIViewRoot component tree is not available unless you are doing a "postback" to the same page.
- You can bind multiple components to the same property to achieve dynamic initialization.
- You can select multiple components in the Designer by dragging a box around the components.
- You can change the same property for multiple components by
selecting multiple components and changing the property in the Property
window.
Thanks go to Alex for patience and willingness to work with us on this issue.
Cheers!
-David
Posted by david
( Oct 03 2006, 11:22:27 AM MDT )
Permalink

|

|