Clean the value of invisible items

Here is a code that you can use to clean the values of some elements if you have a structure of questions that are showing/hiding based on selection.

The approach is to have a main component that contains the tree of questions. The component id will be provided.

There is one issue regarding the inheritance of visible parameters in ADF 11, looks like if you have a panelGroupLayout that is visible=false, the components inside are not having visible=false. (No comment on that).

Hope you find it useful. The cleaning of the value is just for some of the components, check the clean method.
public static void cleanInvisibleItems(String componentId) {
UIComponent component = findComponentInRoot(componentId);
if (component != null) {
List<UIComponent> list = component.getChildren();
parseTree(list);
}
}

public static UIComponent findComponentInRoot(String id) {
UIComponent component = null;

FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) {
UIComponent root = facesContext.getViewRoot();
component = findComponent(root, id);
}
return component;
}

// find a UIComponent inside a UIComponent
public static UIComponent findComponent(UIComponent base, String id) {
if (id.equals(base.getId()))
return base;

UIComponent children = null;
UIComponent result = null;
Iterator childrens = base.getFacetsAndChildren();
while (childrens.hasNext() && (result == null)) {
children = (UIComponent)childrens.next();
if (id.equals(children.getId())) {
result = children;
break;
}
result = findComponent(children, id);
if (result != null) {
break;
}
}
return result;
}

private static void parseTree(List<UIComponent> list) {

List<UIComponent> levelChildren = new ArrayList<UIComponent>();
for (UIComponent elem : list) {
boolean toClean = false;
if (elem instanceof RichPanelGroupLayout) {
RichPanelGroupLayout comp0 = (RichPanelGroupLayout)elem;
if (!comp0.isVisible()) {
toClean = true;
clean(elem);
}
}
if (!toClean) {
List<UIComponent> children = elem.getChildren();
addTolistChildren(levelChildren, children);
parseTree(levelChildren);
}

}

}

private static void addTolistChildren(List<UIComponent> levelChildren, List<UIComponent> children) {
for (UIComponent child : children) {
levelChildren.add(child);
}
}

private static void clean(UIComponent elem) {
List<UIComponent> children = elem.getChildren();
for (UIComponent child : children) {
clean(child);
if (child instanceof RichSelectOneRadio) {
RichSelectOneRadio comp = (RichSelectOneRadio)child;
comp.setValue(null);
} else if (child instanceof RichSelectManyCheckbox) {
RichSelectManyCheckbox comp = (RichSelectManyCheckbox)child;
comp.setValue(null);
} else if (child instanceof RichSelectBooleanCheckbox) {
RichSelectBooleanCheckbox comp = (RichSelectBooleanCheckbox)child;
comp.setValue(null);
} else if (child instanceof RichSelectManyListbox) {
RichSelectManyListbox comp = (RichSelectManyListbox)child;
comp.setValue(null);
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s