Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.richfaces.service;
 
 import  javax.faces.application.FacesMessage;
 import  javax.faces.component.UIComponent;
 import  javax.faces.context.FacesContext;
 
 import  org.ajax4jsf.javascript.JSLiteral;
 import  org.richfaces.application.ServiceTracker;
 import  org.richfaces.focus.FocusManager;
import  org.richfaces.javascript.JavaScriptService;
import  org.richfaces.log.Logger;
import  org.richfaces.log.RichfacesLogger;
public class FocusManagerImpl implements FocusManager {
    private static final String SCRIPT = "$(document.getElementById('%s')).focus();";
    private static final Logger LOG = RichfacesLogger.APPLICATION.getLogger();
    @Override
    public void focus(String componentId) {
        FacesContext context = FacesContext.getCurrentInstance();
        if (context == null) {
            throw new IllegalStateException(FocusManager.class.getSimpleName()
                    + " can't be used without FacesContext available");
        }
        if (componentId == null) {
            setContextAttribute(contextnull);
        } else {
            UIComponent currentComponent = UIComponent.getCurrentComponent(context);
            if (currentComponent == null) {
                currentComponent = context.getViewRoot();
            }
            currentComponent.findComponent(componentId);
            UIComponent component = RendererUtils.getInstance().findComponentFor(currentComponentcomponentId);
            if (component == null) {
                String message = FocusManager.class.getSimpleName() + ": Component with ID '" + componentId + "' was not found";
                .warn(message);
                context.addMessage(nullnew FacesMessage(message));
            } else {
                String clientId = component.getClientId(context);
                setContextAttribute(contextclientId);
                JavaScriptService javaScriptService = ServiceTracker.getService(context, JavaScriptService.class);
                javaScriptService.addPageReadyScript(contextnew JSLiteral(String.format(clientId)));
            }
        }
    }
    private void setContextAttribute(FacesContext contextString clientId) {
        context.getAttributes().put(FocusManager.FOCUS_CONTEXT_ATTRIBUTE, clientId);
    }
New to GrepCode? Check out our FAQ X