protected void doEncodeEnd(ResponseWriter writer, FacesContext context, UIComponent component)
throws IOException {
if (!(component instanceof AbstractFocus)) {
return;
}
AbstractFocus uiFocus = (AbstractFocus) component;
final String clientId = getUtils().clientId(context, component);
checkValidity(context, uiFocus);
Integer priority = uiFocus.getPriority();
String targetClientId = uiFocus.getTargetClientId();
if (targetClientId == null || "".equals(targetClientId)) {
String targetComponentId = uiFocus.getTargetComponentId(context);
String suffix = uiFocus.getSuffix();
if (targetComponentId == null || "".equals(targetComponentId)) {
return;
}
UIComponent forcomp = getUtils().findComponentFor(component, targetComponentId);
if (forcomp == null) {
throw new FacesException("No component with id=" + targetComponentId + " found!");
}
targetClientId = forcomp.getClientId(context);
AbstractFocusModifier modifier = AbstractFocus.findModifier(forcomp);
if (modifier != null) {
final String modifiedTargetClientId = modifier.getTargetClientId();
if (modifiedTargetClientId != null && !modifiedTargetClientId.equals("")) {
targetClientId = modifiedTargetClientId;
} else {
suffix = modifier.getSuffix();
}
}
if (priority == null) {
priority = uiFocus.calculatePriority(forcomp);
}
if (suffix != null && !"".equals(suffix)) {
targetClientId += suffix;
}
}
if (targetClientId == null || targetClientId.equals("")) {
return;
}
if (priority == null) {
priority = AbstractFocus.DEFAULT_PRIORITY;
}
writer.startElement(HtmlConstants.SCRIPT_ELEM, null);
writer.writeAttribute(HtmlConstants.TYPE_ATTR, "text/javascript", "type");
writer.writeAttribute(HtmlConstants.ID_ATTRIBUTE, clientId, HtmlConstants.ID_ATTRIBUTE);
if (AbstractFocus.TIMING_ON_LOAD.equals(uiFocus.getTiming())) {
writer.write(new JSFunction("RichFaces.ui.Focus.setFocus", targetClientId, priority).toScript());
writer.write(";");
} else {
writer.write(new JSFunction("RichFaces.ui.Focus.setFocus", targetClientId, priority, clientId,
uiFocus.getTiming()).toScript());
writer.write(";");
}
if (uiFocus.getName() != null && !uiFocus.getName().trim().equals("")) {
final JSFunctionDefinition definition = new JSFunctionDefinition().addToBody(new JSFunction("RichFaces.ui.Focus.focusStored", clientId));
definition.setName(uiFocus.getName());
writer.write(definition.toScript());
writer.write(";");
}
writer.endElement(HtmlConstants.SCRIPT_ELEM);
}