Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2004-2012 ICEsoft Technologies Canada Corp.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the
  * License. You may obtain a copy of the License at
  *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS
 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.icefaces.util;
Utility API for managing the element focus in the browser.
public class FocusController {
    private static final Pattern ClientIdPattern = Pattern.compile("^(([\\w\\_]*)\\" + UINamingContainer.getSeparatorChar(FacesContext.getCurrentInstance()) + "([\\w\\_]*))*$");

    
Acquire the ID of the currently focused element in the browser.

Parameters:
context the FacesContext
Returns:
the element ID
    public static String getReceivedFocus(FacesContext context) {
        ExternalContext externalContext = context.getExternalContext();
        Map map = externalContext.getRequestParameterMap();
        String focusedElement = (Stringmap.get("ice.focus");
        return focusedElement != null && .matcher(focusedElement).matches() && !focusedElement.equals("null") && !focusedElement.equals("undefined") ? focusedElement : null;
    }

    
Set the element ID that should received focus during next update.

Parameters:
context the FacesContext
id the element ID
    public static void setFocus(FacesContext contextString id) {
        if (id != null && !"".equals(id)) {
            context.getExternalContext().getRequestMap().put(FocusController.class.getName(), id);
        }
    }

    
Get the element ID the will receive focus during next update.

Parameters:
context the FacesContext
Returns:
the element ID
    public static String getFocus(FacesContext context) {
        return (Stringcontext.getExternalContext().getRequestMap().get(FocusController.class.getName());
    }

    
Test if focus is defined for a certain element.

Parameters:
context the FacesContext
Returns:
return true if focus is defined
    public static boolean isFocusSet(FacesContext context) {
        return context.getExternalContext().getRequestMap().containsKey(FocusController.class.getName());
    }
New to GrepCode? Check out our FAQ X