Hi guys!

I wrote a simple component that can set the focus in any form control
across multiple submissions, this is useful to control form
navigation.

How to use:

In jsp (before close h:form):

<s:focus id="focus" component="txtId" />

in managed bean:

private HtmlFocus  focus;
focus   = (HtmlFocus)form.findComponent("focus");
focus.setComponent("txtNome");


--
Yours truly (Atenciosamente),

Rogério
/**
 * Copyright 2004-2005 The Apache Software Foundation.
 *
 * 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.apache.myfaces.custom.focus;

import javax.faces.component.UIComponentBase;

/**
 * @author Rogerio Pereira Araujo (latest modification by $Author$)
 * @version $Revision$ $Date$
 */
public class HtmlFocus extends UIComponentBase
{
    public static final String COMPONENT_TYPE = "org.apache.myfaces.Focus";
    public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.Focus";
    public static final String COMPONENT_FAMILY = "javax.faces.Output";
    
	private String _component = null;
	
    public String getFamily()
    {
        return COMPONENT_FAMILY;
    }    
    
    public void setComponent(String component) {
    	
       this._component = component;
       
    }
    
    public String getComponent() {
    	
    	return _component;
    	
    }
      
}
 
/**
 * Copyright 2004-2005 The Apache Software Foundation.
 *
 * 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.apache.myfaces.custom.focus;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import org.apache.myfaces.renderkit.RendererUtils;
import org.apache.myfaces.renderkit.html.HTML;

/**
 * @author Rogerio Pereira Araujo (latest modification by $Author$)
 * @version $Revision$ $Date$
 */
public class HtmlFocusRenderer extends Renderer
{

    public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
            throws IOException
    {
        RendererUtils.checkParamValidity(facesContext, uiComponent,
                HtmlFocus.class);

        HtmlFocus focus = (HtmlFocus) uiComponent;
        
        String component = focus.getComponent();
        if(component == null) 
        	component = (String) focus.getAttributes().get("component");

        if(component != null) 
        {
	        String clientId = focus.findComponent(component).getClientId(facesContext);
	        
	        ResponseWriter writer = facesContext.getResponseWriter();
	
	        writer.startElement(HTML.SCRIPT_ELEM, uiComponent);
	        writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
	        writer.writeText("document.getElementById('" + clientId + "').focus()", null);
	        writer.endElement(HTML.SCRIPT_ELEM);
        }   
    }

}
/**
 * Copyright 2004-2005 The Apache Software Foundation.
 *
 * 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.apache.myfaces.custom.focus;

import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.webapp.UIComponentTag;
import javax.faces.el.ValueBinding;
import javax.faces.context.FacesContext;

/**
 * @author Rogerio Pereira Araujo (latest modification by $Author$)
 * @version $Revision$ $Date$
 */
public class HtmlFocusTag extends UIComponentTag 
{

	private String _component = null;

    public String getComponentType() {

        return HtmlFocus.COMPONENT_TYPE;

    }

    public String getRendererType() {

        return HtmlFocus.DEFAULT_RENDERER_TYPE;

    }


    public void release() {

        super.release();
        _component=null;

    }
     
    /**
	 * overrides setProperties() form UIComponentTag.
	 */
    protected void setProperties(UIComponent component) {

       super.setProperties(component);

       if (_component != null) 
       { 
         if (isValueReference(_component))
         {
           FacesContext context = FacesContext.getCurrentInstance();
           Application app = context.getApplication();
           ValueBinding vb = app.createValueBinding(_component);
           component.setValueBinding("component", vb);                  
         }
         else 
           component.getAttributes().put("component", _component);
       } 
    }
   
    public void setComponent(String component) {
    	
       this._component = component;
       
    }
    
    public String getComponent() {
    	
    	return _component;
    	
    }
    
}
 
	<component>
   		<component-type>org.apache.myfaces.Focus</component-type>
   		<component-class>org.apache.myfaces.custom.focus.HtmlFocus</component-class>
  	</component>
	<render-kit>
    		<render-kit-id>HTML_BASIC</render-kit-id>
       		<renderer>
        		<component-family>javax.faces.Output</component-family>
        		<renderer-type>org.apache.myfaces.Focus</renderer-type>
        		<renderer-class>org.apache.myfaces.custom.focus.HtmlFocusRenderer</renderer-class>
      		</renderer>
	</render-kit>   

Attachment: sandbox-test.tld
Description: Binary data

Reply via email to