Object restoredAddList[] = (Object[]) state.get(CLIENTIDS_TO_ADD_NAME);
if (restoredAddList != null && restoredAddList.length > 0) {
// Restore the list of added components
List<ComponentStruct> addList = new ArrayList<ComponentStruct>(restoredAddList.length);
for (Object aRestoredAddList : restoredAddList) {
ComponentStruct cur = new ComponentStruct();
cur.restoreState(context, aRestoredAddList);
addList.add(cur);
}
// restore the components themselves
for (ComponentStruct cur : addList) {
final ComponentStruct finalCur = cur;
// Find the parent
viewRoot.visitTree(visitContext, new VisitCallback() {
public VisitResult visit(VisitContext context, UIComponent target) {
VisitResult result = VisitResult.ACCEPT;
if (finalCur.parentClientId.equals(target.getClientId(context.getFacesContext()))) {
StateHolderSaver saver = (StateHolderSaver) state.get(finalCur.clientId);
UIComponent toAdd = (UIComponent) saver.restore(context.getFacesContext());
int idx = finalCur.indexOfChildInParent;
if (idx == -1) {
// add facet to the parent
target.getFacets().put(finalCur.facetName, toAdd);
} else {
// add the child to the parent at correct index
try {
target.getChildren().add(finalCur.indexOfChildInParent, toAdd);
} catch (IndexOutOfBoundsException ioobe) {
// the indexing within the parent list is off during the restore.
// This is most likely due to a transient component added during
// RENDER_REPONSE phase.
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE,
"Unable to insert child with client ID {0} into parent with client ID {1} into list at index {2}.",
new Object[]{finalCur.clientId,
finalCur.parentClientId,
finalCur.indexOfChildInParent});
}
target.getChildren().add(toAdd);
}
}
// Add back to dynamic adds list
Map<String, ComponentStruct> dynamicAdds = stateContext.getDynamicAdds();
assert(null != dynamicAdds);
String clientId = toAdd.getClientId(context.getFacesContext());
if (!dynamicAdds.containsKey(clientId)) {
ComponentStruct toAddCS = new ComponentStruct();
toAddCS.absorbComponent(context.getFacesContext(), toAdd);
dynamicAdds.put(clientId, toAddCS);
}
}
return result;