Tony
Tony

Reputation: 2406

How to find composite component in JSF component tree?

I implement a simple method, that iterate JSF components tree and sets the components to disabled. (So the user cannot change the values). But this method doesn't function for composite components. How can I detect a composite component at least? Then I can try to set the special attribute to disabled.

Upvotes: 3

Views: 911

Answers (1)

BalusC
BalusC

Reputation: 1108632

The UIComponent class has a isCompositeComponent() helper method for exactly this purpose.

So, this should just do:

for (UIComponent child : component.getChildren()) {
    if (UIComponent.isCompositeComponent(child)) {
        // It's a composite child!
    }
}

For the interested in "under the covers" workings, here's the implementation source code from Mojarra 2.1.25:

public static boolean isCompositeComponent(UIComponent component) {

    if (component == null) {
        throw new NullPointerException();
    }
    boolean result = false;
    if (null != component.isCompositeComponent) {
        result = component.isCompositeComponent.booleanValue();
    } else {
        result = component.isCompositeComponent =
                (component.getAttributes().containsKey(
                           Resource.COMPONENT_RESOURCE_KEY));
    }
    return result;

}

It's thus identified by the presence of a component attribute with the name as definied by Resource.COMPONENT_RESOURCE_KEY which has the value of "javax.faces.application.Resource.ComponentResource".

Upvotes: 4

Related Questions